File size: 1,100 Bytes
dbb1bf9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | export interface ObservableCloudPrefsFlushSuccessOptions {
syncVersion: unknown;
myGeneration: number;
getAuthGeneration: () => number;
getSyncVersion: () => number;
setSyncVersion: (syncVersion: number) => void;
clearSettledDirtyKeys: () => void;
setLastSyncAt: (timestampMs: number) => void;
isIdle: () => boolean;
setSynced: () => void;
now?: () => number;
}
/**
* Apply an observable keepalive flush response after a tab is hidden.
* Real unloads usually do not run this path; tab switches do, and the echoed
* syncVersion must be adopted so the next alive-tab save does not hit 409.
*/
export function applyObservableCloudPrefsFlushSuccess(
opts: ObservableCloudPrefsFlushSuccessOptions,
): boolean {
if (typeof opts.syncVersion !== 'number') return false;
if (opts.getAuthGeneration() !== opts.myGeneration) return false;
if (opts.syncVersion <= opts.getSyncVersion()) return false;
opts.setSyncVersion(opts.syncVersion);
opts.clearSettledDirtyKeys();
opts.setLastSyncAt((opts.now ?? Date.now)());
if (opts.isIdle()) opts.setSynced();
return true;
}
|