perf: reuse progressive live snapshot during hydration (Closes #1405)
This commit is contained in:
1 parent
1822004f3d
commit
54cb078d1a
7 files changed
+247
-6
No files matched your search
@@ -166,9 +166,47 @@ function createContextPoller({
|
||||
return refresh({ force: true });
|
||||
}
|
||||
|
||||
function adopt(snapshot) {
|
||||
if (
|
||||
stopped || activeRequest || retainedSnapshot || !snapshot ||
|
||||
typeof snapshot !== 'object' ||
|
||||
!Object.prototype.hasOwnProperty.call(snapshot, 'context')
|
||||
) return false;
|
||||
retainedSnapshot = { ...snapshot };
|
||||
revisions = { ...(snapshot.revisions || {}) };
|
||||
failureStreak = 0;
|
||||
lastSuccessAt = Date.now();
|
||||
nextDelayMs = snapshotDelay(snapshot);
|
||||
const changedSections = ['context', 'events', 'notifications'].filter(
|
||||
section => Object.prototype.hasOwnProperty.call(snapshot, section)
|
||||
);
|
||||
onSnapshot(retainedSnapshot, changedSections);
|
||||
schedule(nextDelayMs);
|
||||
return true;
|
||||
}
|
||||
|
||||
async function adoptPending(snapshotPromise) {
|
||||
if (!snapshotPromise || typeof snapshotPromise.then !== 'function') return false;
|
||||
let deadline = null;
|
||||
const timeout = new Promise(resolve => {
|
||||
deadline = setDeadlineTimer(() => resolve(null), timeoutMs);
|
||||
});
|
||||
try {
|
||||
const snapshot = await Promise.race([
|
||||
Promise.resolve(snapshotPromise).catch(() => null),
|
||||
timeout,
|
||||
]);
|
||||
return adopt(snapshot);
|
||||
} finally {
|
||||
clearDeadlineTimer(deadline);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
start: refresh,
|
||||
refresh,
|
||||
adopt,
|
||||
adoptPending,
|
||||
setVisible,
|
||||
getState() {
|
||||
return {
|
||||
|
||||
@@ -8441,9 +8441,12 @@
|
||||
if (!deviceSetup) await (await ensureDeviceSetup()).open(event);
|
||||
});
|
||||
pushControllerReady.then(ensureDeviceSetup).catch(console.warn);
|
||||
await load();
|
||||
let adoptedProgressiveSnapshot = contextPoller.adopt(progressiveWorkHandoff?.liveSnapshot);
|
||||
if (!adoptedProgressiveSnapshot && progressiveWorkHandoff?.liveSnapshotPromise) {
|
||||
adoptedProgressiveSnapshot = await contextPoller.adoptPending(progressiveWorkHandoff.liveSnapshotPromise);
|
||||
}
|
||||
if (!adoptedProgressiveSnapshot) await load();
|
||||
await appShortcut.run();
|
||||
contextPoller.start();
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
contextPoller.setVisible(!document.hidden);
|
||||
if (!document.hidden) deviceSetup?.render();
|
||||
|
||||
@@ -7,6 +7,8 @@ function createProgressiveMyWork({ document, fetchSnapshot }) {
|
||||
let active = 'all';
|
||||
let stopped = false;
|
||||
let selectedByUser = false;
|
||||
let liveSnapshot = null;
|
||||
let liveSnapshotPromise = null;
|
||||
const deferredQueues = {
|
||||
today:'Today', agenda:'Agenda', later:'Later', draft:'Drafts',
|
||||
};
|
||||
@@ -59,13 +61,31 @@ function createProgressiveMyWork({ document, fetchSnapshot }) {
|
||||
|
||||
return {
|
||||
handoff() {
|
||||
return { selectedFilter:selectedByUser ? active : null };
|
||||
const state = { selectedFilter:selectedByUser ? active : null };
|
||||
if (liveSnapshot) {
|
||||
state.liveSnapshot = liveSnapshot;
|
||||
liveSnapshot = null;
|
||||
liveSnapshotPromise = null;
|
||||
} else if (liveSnapshotPromise) {
|
||||
state.liveSnapshotPromise = liveSnapshotPromise;
|
||||
liveSnapshotPromise = null;
|
||||
}
|
||||
return state;
|
||||
},
|
||||
async start() {
|
||||
if (status) status.textContent = 'Loading assigned work…';
|
||||
const request = Promise.resolve().then(() => fetchSnapshot());
|
||||
liveSnapshotPromise = request.then(snapshot => (
|
||||
snapshot && typeof snapshot === 'object' &&
|
||||
Object.prototype.hasOwnProperty.call(snapshot, 'context') ? snapshot : null
|
||||
), () => null);
|
||||
try {
|
||||
const snapshot = await fetchSnapshot();
|
||||
const snapshot = await request;
|
||||
if (stopped) return false;
|
||||
const transferable = snapshot && typeof snapshot === 'object' &&
|
||||
Object.prototype.hasOwnProperty.call(snapshot, 'context');
|
||||
if (transferable) liveSnapshot = snapshot;
|
||||
else liveSnapshotPromise = null;
|
||||
const context = snapshot?.context || snapshot || {};
|
||||
items = buildMyWork({ ...context, notifications:snapshot?.notifications || context.notifications || [] });
|
||||
updateCounts();
|
||||
|
||||
Reference in new issue
Block a user