Skip to content

Commit 8ff1131

Browse files
authored
fix: add visibility timeout for db watcher (#679)
1 parent 91849a9 commit 8ff1131

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

packages/root-cms/ui/hooks/useDraft.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ type Subscribers = Record<string, Set<SubscriberCallback>>;
4040
type SubscriberCallback = (newValue: any) => void;
4141
type UnsubscribeCallback = () => void;
4242

43+
/**
44+
* Number of seconds to wait before disabling db watchers once the browser tab
45+
* loses visibility.
46+
*/
47+
const VISIBILITY_TIMEOUT = 30;
48+
4349
export class DraftController extends EventListener {
4450
readonly projectId: string;
4551
readonly docId: string;
@@ -388,9 +394,17 @@ export function useDraft(docId: string): UseDraftHook {
388394
controller.start();
389395

390396
const onVisibilityChange = () => {
397+
let visibilityTimeoutId: number | undefined;
398+
391399
if (document.hidden || document.visibilityState !== 'visible') {
392-
controller.stop();
400+
visibilityTimeoutId = window.setTimeout(() => {
401+
controller.stop();
402+
}, VISIBILITY_TIMEOUT * 1000);
393403
} else {
404+
if (visibilityTimeoutId) {
405+
clearTimeout(visibilityTimeoutId);
406+
visibilityTimeoutId = undefined;
407+
}
394408
if (!controller.started) {
395409
setLoading(true);
396410
controller.start();

0 commit comments

Comments
 (0)