mirror of
https://github.com/hansjone/dsh-im-ops.git
synced 2026-10-09 01:43:14 +08:00
28 lines
955 B
JavaScript
28 lines
955 B
JavaScript
import * as React from 'react';
|
|
|
|
/** Keep every full-snapshot response ordered behind the latest client mutation. */
|
|
export function useWorkspaceSnapshotFence() {
|
|
const state = React.useRef({ version: 0, pendingMutations: 0 });
|
|
return React.useMemo(() => Object.freeze({
|
|
beginStatus() {
|
|
return state.current.pendingMutations === 0 ? state.current.version : null;
|
|
},
|
|
canCommitStatus(version) {
|
|
return version !== null
|
|
&& state.current.pendingMutations === 0
|
|
&& state.current.version === version;
|
|
},
|
|
beginMutation() {
|
|
state.current.pendingMutations += 1;
|
|
state.current.version += 1;
|
|
return state.current.version;
|
|
},
|
|
canCommitMutation(version) {
|
|
return state.current.version === version;
|
|
},
|
|
endMutation() {
|
|
state.current.pendingMutations = Math.max(0, state.current.pendingMutations - 1);
|
|
return state.current.pendingMutations === 0;
|
|
},
|
|
}), []);
|
|
}
|