-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #874 from vizhub-core/561-Saving/Saved-Status
561 saving/saved status
- Loading branch information
Showing
8 changed files
with
147 additions
and
10 deletions.
There are no files selected for viewing
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 { | ||
useState, | ||
useEffect, | ||
useCallback, | ||
useRef, | ||
} from 'react'; | ||
import { ShareDBDoc, VZCodeContent } from '../../types'; | ||
|
||
// import { runDelay } from '../constants'; | ||
// TODO understand what this part is for | ||
const runDelay = 1000; | ||
|
||
export const usePending = ( | ||
shareDBDoc: ShareDBDoc<VZCodeContent>, | ||
) => { | ||
const lastOpTimerRef = useRef<NodeJS.Timeout>(); | ||
|
||
// TODO rename this to isPending, | ||
// and also rename connected to isConnected. | ||
const [pending, setPending] = useState<boolean>(false); | ||
|
||
const resetOnNoPending = useCallback(() => { | ||
if (!shareDBDoc) return; | ||
|
||
// This happens AFTER the changes have been | ||
// acknowledged by the server. | ||
shareDBDoc.whenNothingPending(() => { | ||
setPending(false); | ||
}); | ||
}, [shareDBDoc, setPending]); | ||
|
||
useEffect(() => { | ||
if (!shareDBDoc) return; | ||
|
||
// This happens when you type, but BEFORE | ||
// the changes are sent to the server. | ||
shareDBDoc.on('before op', () => { | ||
setPending(true); | ||
|
||
clearTimeout(lastOpTimerRef.current); | ||
|
||
lastOpTimerRef.current = setTimeout(() => { | ||
// should be triggered only once, cause upcoming ops unschedule previous resetOnNoPending | ||
resetOnNoPending(); | ||
}, runDelay); | ||
}); | ||
}, [shareDBDoc, resetOnNoPending]); | ||
|
||
return pending; | ||
}; |
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