fix: reconcile concurrent Draft saves (Closes #857)
This commit is contained in:
1 parent
631eb2e584
commit
8a3316a5c4
3 files changed
+192
-19
No files matched your search
@@ -252,6 +252,7 @@
|
||||
<div class="small" id="notification-page-status" aria-live="polite"></div>
|
||||
<button class="load-more-notifications" id="load-more-notifications" type="button" hidden>Load older updates</button>
|
||||
<div class="small" id="my-work-action-status" aria-live="assertive"></div>
|
||||
<button class="retry-work-route" id="retry-unfiled-draft-sync" type="button" hidden>Retry Draft sync</button>
|
||||
<div class="small" id="today-sync-status" aria-live="polite">Today is saved on this device.</div>
|
||||
<div class="small" id="later-sync-status" aria-live="polite">Later is saved on this device.</div>
|
||||
<button class="retry-work-route" id="retry-work-route" type="button" hidden>Retry shared work item</button>
|
||||
|
||||
@@ -6,12 +6,13 @@
|
||||
function createUnfiledDraftSync(options, mountedFetch) {
|
||||
if (mountedFetch) return createUnfiledDraftSync.mount(options, mountedFetch);
|
||||
const {
|
||||
captures, fetchJson, storage, getLogin = () => '', onState = () => {},
|
||||
captures, fetchJson, storage, getLogin = () => '', onState = () => {}, maxConflictRetries = 2,
|
||||
createConflictId = () => 'conflict-' + (globalThis.crypto?.randomUUID?.() || Date.now().toString(36)),
|
||||
} = options;
|
||||
const key = 'stackchain.unfiled-draft-sync.v1';
|
||||
let inFlight = null;
|
||||
let rerun = false;
|
||||
let queuedOwner = '';
|
||||
|
||||
function read(login) {
|
||||
try {
|
||||
@@ -33,8 +34,8 @@
|
||||
storage?.setItem(key, JSON.stringify(state.all));
|
||||
}
|
||||
|
||||
function publish(status, message) {
|
||||
onState({status, ...(message ? {message} : {})});
|
||||
function publish(status, message, details = {}) {
|
||||
onState({status, ...(message ? {message} : {}), ...details});
|
||||
const visibleStatus = globalThis.document?.querySelector?.('#my-work-action-status');
|
||||
if (message && visibleStatus) visibleStatus.textContent = message;
|
||||
}
|
||||
@@ -131,9 +132,7 @@
|
||||
: 'Drafts synced across devices.');
|
||||
return true;
|
||||
} catch (error) {
|
||||
if (error?.status === 409) {
|
||||
publish('conflict', 'Drafts changed on another device. Sync again to combine them.');
|
||||
} else {
|
||||
if (error?.status !== 409) {
|
||||
publish('pending', 'Saved on this device · sync pending');
|
||||
}
|
||||
throw error;
|
||||
@@ -145,13 +144,32 @@
|
||||
if (!owner) return Promise.resolve(false);
|
||||
if (inFlight) {
|
||||
rerun = true;
|
||||
queuedOwner = owner;
|
||||
return inFlight;
|
||||
}
|
||||
inFlight = (async () => {
|
||||
let result = false;
|
||||
let conflictRetries = 0;
|
||||
let activeOwner = owner;
|
||||
do {
|
||||
rerun = false;
|
||||
result = await runSync(owner);
|
||||
try {
|
||||
result = await runSync(activeOwner);
|
||||
} catch (error) {
|
||||
if (error?.status !== 409) throw error;
|
||||
if (conflictRetries >= maxConflictRetries) {
|
||||
publish('conflict', 'Draft sync needs attention. Retry when your devices finish saving.', {retryable:true});
|
||||
throw error;
|
||||
}
|
||||
conflictRetries += 1;
|
||||
rerun = true;
|
||||
}
|
||||
if (queuedOwner) {
|
||||
if (queuedOwner !== activeOwner) conflictRetries = 0;
|
||||
activeOwner = queuedOwner;
|
||||
queuedOwner = '';
|
||||
rerun = true;
|
||||
}
|
||||
} while (rerun);
|
||||
return result;
|
||||
})().finally(() => { inFlight = null; });
|
||||
@@ -168,16 +186,29 @@
|
||||
return sync(owner);
|
||||
}
|
||||
|
||||
function retry() {
|
||||
return sync(getLogin());
|
||||
}
|
||||
|
||||
captures.subscribe?.((id, removed) => {
|
||||
const login = getLogin();
|
||||
if (login) void (removed ? remove(id, login) : sync(login)).catch(() => {});
|
||||
});
|
||||
|
||||
return {sync, remove};
|
||||
return {sync, remove, retry};
|
||||
}
|
||||
createUnfiledDraftSync.mount = (captures, fetchJson) => {
|
||||
const getLogin = captures.currentLogin;
|
||||
const controller = createUnfiledDraftSync({captures, fetchJson, storage:globalThis.localStorage, getLogin});
|
||||
const retryButton = globalThis.document?.querySelector?.('#retry-unfiled-draft-sync');
|
||||
const controller = createUnfiledDraftSync({
|
||||
captures, fetchJson, storage:globalThis.localStorage, getLogin,
|
||||
onState:state => { if (retryButton) retryButton.hidden = !state.retryable; },
|
||||
});
|
||||
retryButton?.addEventListener?.('click', async () => {
|
||||
retryButton.disabled = true;
|
||||
try { await controller.retry(); } catch (_error) { /* state remains actionable */ }
|
||||
finally { retryButton.disabled = false; }
|
||||
});
|
||||
const connect = () => {
|
||||
const login = getLogin();
|
||||
if (login) void controller.sync(login).catch(() => {});
|
||||
|
||||
Reference in new issue
Block a user