-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04ef3f0
commit 14c3d95
Showing
7 changed files
with
93 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { SerializedGraph } from 'graphology-types'; | ||
import { EdgeAttributes, GraphAttributes, LocalStorageKey, NodeAttributes } from '../constants/constants'; | ||
import { createHash } from './helpers'; | ||
|
||
export const SAVE_MANAGER_CHANNEL_NAME = 'rmt-save-manager'; | ||
export enum SaveManagerEventType { | ||
SAVE_CHANGED = 'SAVE_CHANGED', | ||
} | ||
export interface SaveManagerEvent { | ||
type: SaveManagerEventType; | ||
key: LocalStorageKey.PARAM; | ||
from: 'rmp' | 'rmt'; | ||
} | ||
|
||
export const saveManagerChannel = new BroadcastChannel(SAVE_MANAGER_CHANNEL_NAME); | ||
|
||
export const notifyRMTSaveChange = () => { | ||
saveManagerChannel.postMessage({ | ||
type: SaveManagerEventType.SAVE_CHANGED, | ||
key: LocalStorageKey.PARAM, | ||
from: 'rmp', | ||
} as SaveManagerEvent); | ||
}; | ||
|
||
/** | ||
* Remember the previous hash of the graph state. | ||
* Notify the rmt only if the graph changes. | ||
*/ | ||
let previousGraphHash: string | undefined; | ||
|
||
/** | ||
* Slightly reduce the server load by notifying a sequence of updates once. | ||
*/ | ||
let notifyRMTSaveTimeout: number | undefined; | ||
|
||
const SAVE_UPDATE_TIMEOUT_MS = 60 * 1000; // 60s | ||
|
||
export const onRMTSaveUpdate = async (graph: SerializedGraph<NodeAttributes, EdgeAttributes, GraphAttributes>) => { | ||
if (notifyRMTSaveTimeout) { | ||
return; | ||
} | ||
const graphHash = await createHash(JSON.stringify(graph)); | ||
if (previousGraphHash && previousGraphHash !== graphHash) { | ||
notifyRMTSaveTimeout = window.setTimeout(() => { | ||
notifyRMTSaveChange(); | ||
notifyRMTSaveTimeout = undefined; | ||
}, SAVE_UPDATE_TIMEOUT_MS); | ||
} | ||
previousGraphHash = graphHash; | ||
}; |