@@ -234,12 +234,10 @@ async function applyRemoteUpdate() {
|
||||
if (stateFingerprint(current) === stateFingerprint(snapshot)) return;
|
||||
} catch (_) {}
|
||||
}
|
||||
const identity = loadIdentity();
|
||||
if (identity && identity.isMaster) {
|
||||
const currentSize = current ? snapshotContentSize(current) : snapshotContentSize(holesailManager.getStateSnapshot());
|
||||
const snapshotSize = snapshotContentSize(snapshot);
|
||||
if (snapshotSize < currentSize) return;
|
||||
}
|
||||
// Never overwrite our state with a snapshot that has less content (avoids empty overwriting master or peer).
|
||||
const currentSize = current ? snapshotContentSize(current) : snapshotContentSize(holesailManager.getStateSnapshot());
|
||||
const snapshotSize = snapshotContentSize(snapshot);
|
||||
if (snapshotSize < currentSize) return;
|
||||
await applySyncedState(snapshot);
|
||||
lastSyncedAt = Date.now();
|
||||
if (emitEvent) emitEvent('syncApplied', {});
|
||||
@@ -638,14 +636,30 @@ async function pairWithInvite(invite) {
|
||||
saveIdentity(pass.key, pass.encryptionKey, false);
|
||||
currentInvite = null;
|
||||
log('Sync: paired with invite (joined existing sync group)');
|
||||
const entry = await pass.get(STATE_KEY);
|
||||
// Wait for master's state to replicate; retry a few times so we don't apply empty on a cold view.
|
||||
let entry = null;
|
||||
const maxTries = 8;
|
||||
const retryMs = 350;
|
||||
for (let i = 0; i < maxTries; i++) {
|
||||
entry = await pass.get(STATE_KEY);
|
||||
if (entry && entry.value != null) {
|
||||
const str = typeof entry.value === 'string' ? entry.value : (entry.value && entry.value.toString ? entry.value.toString() : '');
|
||||
if (str) {
|
||||
const snapshot = JSON.parse(str);
|
||||
if (snapshotContentSize(snapshot) > 0) break;
|
||||
}
|
||||
}
|
||||
if (i < maxTries - 1) await new Promise(r => setTimeout(r, retryMs));
|
||||
}
|
||||
if (entry && entry.value != null) {
|
||||
const str = typeof entry.value === 'string' ? entry.value : (entry.value && entry.value.toString ? entry.value.toString() : '');
|
||||
if (str) {
|
||||
const snapshot = JSON.parse(str);
|
||||
await applySyncedState(snapshot);
|
||||
lastSyncedAt = Date.now();
|
||||
if (emitEvent) emitEvent('syncApplied', {});
|
||||
if (snapshotContentSize(snapshot) > 0) {
|
||||
await applySyncedState(snapshot);
|
||||
lastSyncedAt = Date.now();
|
||||
if (emitEvent) emitEvent('syncApplied', {});
|
||||
}
|
||||
}
|
||||
}
|
||||
return { ok: true };
|
||||
|
||||
Reference in New Issue
Block a user