Skip to content

Commit a9d7b77

Browse files
Using synthesized RemoteEvent
1 parent fc478ff commit a9d7b77

File tree

3 files changed

+46
-28
lines changed

3 files changed

+46
-28
lines changed

packages/firestore/src/core/sync_engine.ts

+25-24
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
349349
.then(result => {
350350
this.sharedClientState.addPendingMutation(result.batchId);
351351
this.addMutationCallback(result.batchId, userCallback);
352-
return this.emitNewSnapsAndNotifyLocalStore(
353-
result.changes,
354-
SnapshotVersion.MIN
355-
);
352+
return this.emitNewSnapsAndNotifyLocalStore(result.changes);
356353
})
357354
.then(() => {
358355
return this.remoteStore.fillWritePipeline();
@@ -462,11 +459,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
462459
}
463460
}
464461
);
465-
return this.emitNewSnapsAndNotifyLocalStore(
466-
changes,
467-
remoteEvent.snapshotVersion,
468-
remoteEvent
469-
);
462+
return this.emitNewSnapsAndNotifyLocalStore(changes, remoteEvent);
470463
})
471464
.catch(err => this.ignoreIfPrimaryLeaseLoss(err));
472465
}
@@ -583,22 +576,30 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
583576
return;
584577
}
585578

579+
let remoteEvent;
580+
586581
if (batchState === 'pending') {
587582
// If we are the primary client, we need to send this write to the
588583
// backend. Secondary clients will ignore these writes since their remote
589584
// connection is disabled.
590585
await this.remoteStore.fillWritePipeline();
591-
} else if (batchState === 'acknowledged' || batchState === 'rejected') {
586+
} else if (batchState === 'acknowledged') {
587+
remoteEvent = RemoteEvent.createSynthesizedRemoteEventForSuccessfulWrite(
588+
snapshotVersion
589+
);
592590
// NOTE: Both these methods are no-ops for batches that originated from
593591
// other clients.
594-
this.processUserCallback(batchId, error ? error : null);
595-
592+
this.processUserCallback(batchId, null);
593+
this.localStore.removeCachedMutationBatchMetadata(batchId);
594+
} else if (batchState === 'rejected') {
595+
assert(error !== null, 'Error not set for rejected mutation');
596+
this.processUserCallback(batchId, error!);
596597
this.localStore.removeCachedMutationBatchMetadata(batchId);
597598
} else {
598599
fail(`Unknown batchState: ${batchState}`);
599600
}
600601

601-
await this.emitNewSnapsAndNotifyLocalStore(documents, snapshotVersion);
602+
await this.emitNewSnapsAndNotifyLocalStore(documents, remoteEvent);
602603
}
603604

604605
applySuccessfulWrite(
@@ -617,9 +618,12 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
617618
return this.localStore
618619
.acknowledgeBatch(mutationBatchResult)
619620
.then(changes => {
621+
const synthesizedRemoteEvent = RemoteEvent.createSynthesizedRemoteEventForSuccessfulWrite(
622+
mutationBatchResult.commitVersion
623+
);
620624
return this.emitNewSnapsAndNotifyLocalStore(
621625
changes,
622-
mutationBatchResult.commitVersion
626+
synthesizedRemoteEvent
623627
);
624628
})
625629
.catch(err => this.ignoreIfPrimaryLeaseLoss(err));
@@ -643,10 +647,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
643647
'rejected',
644648
error
645649
);
646-
return this.emitNewSnapsAndNotifyLocalStore(
647-
changes,
648-
SnapshotVersion.MIN
649-
);
650+
return this.emitNewSnapsAndNotifyLocalStore(changes);
650651
})
651652
.catch(err => this.ignoreIfPrimaryLeaseLoss(err));
652653
}
@@ -779,13 +780,16 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
779780

780781
private async emitNewSnapsAndNotifyLocalStore(
781782
changes: MaybeDocumentMap,
782-
snapshotVersion: SnapshotVersion,
783783
remoteEvent?: RemoteEvent
784784
): Promise<void> {
785785
const newSnaps: ViewSnapshot[] = [];
786786
const docChangesInAllViews: LocalViewChanges[] = [];
787787
const queriesProcessed: Array<Promise<void>> = [];
788788

789+
const snapshotVersion = remoteEvent
790+
? remoteEvent.snapshotVersion
791+
: SnapshotVersion.MIN;
792+
789793
this.queryViewsByQuery.forEach((_, queryView) => {
790794
queriesProcessed.push(
791795
Promise.resolve()
@@ -882,10 +886,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
882886
result.removedBatchIds,
883887
result.addedBatchIds
884888
);
885-
await this.emitNewSnapsAndNotifyLocalStore(
886-
result.affectedDocuments,
887-
SnapshotVersion.MIN
888-
);
889+
await this.emitNewSnapsAndNotifyLocalStore(result.affectedDocuments);
889890
}
890891

891892
await this.remoteStore.handleCredentialChange();
@@ -1015,12 +1016,12 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
10151016
case 'not-current': {
10161017
const changes = await this.localStore.getNewDocumentChanges();
10171018
const synthesizedRemoteEvent = RemoteEvent.createSynthesizedRemoteEventForCurrentChange(
1019+
snapshotVersion,
10181020
targetId,
10191021
state === 'current'
10201022
);
10211023
return this.emitNewSnapsAndNotifyLocalStore(
10221024
changes,
1023-
snapshotVersion,
10241025
synthesizedRemoteEvent
10251026
);
10261027
}

packages/firestore/src/remote/remote_event.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export class RemoteEvent {
6565
*/
6666
// PORTING NOTE: Multi-tab only
6767
static createSynthesizedRemoteEventForCurrentChange(
68+
snapshotVersion: SnapshotVersion,
6869
targetId: TargetId,
6970
current: boolean
7071
): RemoteEvent {
@@ -75,13 +76,29 @@ export class RemoteEvent {
7576
)
7677
};
7778
return new RemoteEvent(
78-
SnapshotVersion.MIN,
79+
snapshotVersion,
7980
targetChanges,
8081
targetIdSet(),
8182
maybeDocumentMap(),
8283
documentKeySet()
8384
);
8485
}
86+
87+
/**
88+
* Create a synthesized remote event that is used to apply the commit version
89+
* of an successful write to a View.
90+
*/
91+
static createSynthesizedRemoteEventForSuccessfulWrite(
92+
snapshotVersion: SnapshotVersion
93+
): RemoteEvent {
94+
return new RemoteEvent(
95+
snapshotVersion,
96+
[],
97+
targetIdSet(),
98+
maybeDocumentMap(),
99+
documentKeySet()
100+
);
101+
}
85102
}
86103

87104
/**

packages/firestore/test/unit/local/persistence_test_helpers.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {
3838
import { FirestoreError } from '../../../src/util/error';
3939
import { AutoId } from '../../../src/util/misc';
4040
import { PlatformSupport } from '../../../src/platform/platform';
41-
import {SnapshotVersion} from '../../../src/core/snapshot_version';
41+
import { SnapshotVersion } from '../../../src/core/snapshot_version';
4242

4343
/** The persistence prefix used for testing in IndexedBD and LocalStorage. */
4444
export const TEST_PERSISTENCE_PREFIX =
@@ -91,7 +91,7 @@ class NoOpSharedClientStateSyncer implements SharedClientStateSyncer {
9191
constructor(private readonly activeClients: ClientId[]) {}
9292
async applyBatchState(
9393
batchId: BatchId,
94-
snapshotVersion:SnapshotVersion,
94+
snapshotVersion: SnapshotVersion,
9595
state: MutationBatchState,
9696
error?: FirestoreError
9797
): Promise<void> {}
@@ -105,7 +105,7 @@ class NoOpSharedClientStateSyncer implements SharedClientStateSyncer {
105105
}
106106
async applyTargetState(
107107
targetId: TargetId,
108-
snapshotVersion:SnapshotVersion,
108+
snapshotVersion: SnapshotVersion,
109109
state: QueryTargetState,
110110
error?: FirestoreError
111111
): Promise<void> {}

0 commit comments

Comments
 (0)