Skip to content

Commit c28a9f5

Browse files
Review
1 parent a857f59 commit c28a9f5

File tree

2 files changed

+53
-25
lines changed

2 files changed

+53
-25
lines changed

packages/firestore/src/local/local_store.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,9 +1054,10 @@ export function lookupMutationDocuments(
10541054
localStore: LocalStore,
10551055
batchId: BatchId
10561056
): Promise<MaybeDocumentMap | null> {
1057-
const localStoreImpl = debugCast(localStore, LocalStoreImpl);
1058-
const mutationQueueImpl = debugCast(
1059-
localStoreImpl.mutationQueue,
1057+
const [localStoreImpl, mutationQueueImpl] = debugCast(
1058+
localStore,
1059+
LocalStoreImpl,
1060+
(localStore as LocalStoreImpl).mutationQueue,
10601061
IndexedDbMutationQueue // We only support IndexedDb in multi-tab mode.
10611062
);
10621063
return localStoreImpl.persistence.runTransaction(
@@ -1082,9 +1083,8 @@ export function removeCachedMutationBatchMetadata(
10821083
localStore: LocalStore,
10831084
batchId: BatchId
10841085
): void {
1085-
const localStoreImpl = debugCast(localStore, LocalStoreImpl);
10861086
const mutationQueueImpl = debugCast(
1087-
localStoreImpl.mutationQueue,
1087+
debugCast(localStore, LocalStoreImpl).mutationQueue,
10881088
IndexedDbMutationQueue // We only support IndexedDb in multi-tab mode.
10891089
);
10901090
mutationQueueImpl.removeCachedMutationKeys(batchId);
@@ -1094,9 +1094,8 @@ export function removeCachedMutationBatchMetadata(
10941094
export function getCurrentlyActiveClients(
10951095
localStore: LocalStore
10961096
): Promise<ClientId[]> {
1097-
const localStoreImpl = debugCast(localStore, LocalStoreImpl);
10981097
const persistenceImpl = debugCast(
1099-
localStoreImpl.persistence,
1098+
debugCast(localStore, LocalStoreImpl).persistence,
11001099
IndexedDbPersistence // We only support IndexedDb in multi-tab mode.
11011100
);
11021101
return persistenceImpl.getActiveClients();
@@ -1107,9 +1106,10 @@ export function getCachedTarget(
11071106
localStore: LocalStore,
11081107
targetId: TargetId
11091108
): Promise<Target | null> {
1110-
const localStoreImpl = debugCast(localStore, LocalStoreImpl);
1111-
const targetCacheImpl = debugCast(
1112-
localStoreImpl.targetCache,
1109+
const [localStoreImpl, targetCacheImpl] = debugCast(
1110+
localStore,
1111+
LocalStoreImpl,
1112+
(localStore as LocalStoreImpl).targetCache,
11131113
IndexedDbTargetCache // We only support IndexedDb in multi-tab mode.
11141114
);
11151115
const cachedTargetData = localStoreImpl.targetDataByTarget.get(targetId);
@@ -1131,16 +1131,17 @@ export function getCachedTarget(
11311131
/**
11321132
* Returns the set of documents that have been updated since the last call.
11331133
* If this is the first call, returns the set of changes since client
1134-
* initialization. Further invocations will return document changes since
1135-
* the point of rejection.
1134+
* initialization. Further invocations will return document that have changed
1135+
* since the prior call.
11361136
*/
11371137
// PORTING NOTE: Multi-Tab only.
11381138
export function getNewDocumentChanges(
11391139
localStore: LocalStore
11401140
): Promise<MaybeDocumentMap> {
1141-
const localStoreImpl = debugCast(localStore, LocalStoreImpl);
1142-
const remoteDocumentCacheImpl = debugCast(
1143-
localStoreImpl.remoteDocuments,
1141+
const [localStoreImpl, remoteDocumentCacheImpl] = debugCast(
1142+
localStore,
1143+
LocalStoreImpl,
1144+
(localStore as LocalStoreImpl).remoteDocuments,
11441145
IndexedDbRemoteDocumentCache // We only support IndexedDb in multi-tab mode.
11451146
);
11461147
return localStoreImpl.persistence
@@ -1157,8 +1158,8 @@ export function getNewDocumentChanges(
11571158
}
11581159

11591160
/**
1160-
* Reads the newest document change from persistence and forwards the internal
1161-
* synchronization marker so that calls to `getNewDocumentChanges()`
1161+
* Reads the newest document change from persistence and moves the internal
1162+
* synchronization marker forward so that calls to `getNewDocumentChanges()`
11621163
* only return changes that happened after client initialization.
11631164
*/
11641165
// PORTING NOTE: Multi-Tab only.

packages/firestore/src/util/assert.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,44 @@ export function debugAssert(
7272
}
7373

7474
/**
75-
* Casts `obj` to `T`. In non-production builds, verifies that `obj` is an
76-
* instance of `T` before casting.
75+
* Casts `obj1` to `S` and `obj2` to `T`. In non-production builds,
76+
* verifies that `obj1` and `obj2` are instances of `S` and `T` before casting.
7777
*/
78-
export function debugCast<T>(
78+
export function debugCast<S, T>(
79+
obj1: object,
80+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
81+
constructor1: { new (...args: any[]): S },
82+
obj2: object,
83+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
84+
constructor2: { new (...args: any[]): T }
85+
): [S, T] | never;
86+
/**
87+
* Casts `obj` to `S`. In non-production builds, verifies that `obj` is an
88+
* instance of `S` before casting.
89+
*/
90+
export function debugCast<S>(
7991
obj: object,
8092
// eslint-disable-next-line @typescript-eslint/no-explicit-any
81-
constructor: { new (...args: any[]): T }
82-
): T | never {
93+
constructor: { new (...args: any[]): S }
94+
): S | never;
95+
export function debugCast<S, T>(
96+
obj1: object,
97+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
98+
constructor1: { new (...args: any[]): S },
99+
obj2?: object,
100+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
101+
constructor2?: { new (...args: any[]): T }
102+
): S | [S, T] | never {
103+
debugAssert(
104+
obj1 instanceof constructor1,
105+
`Expected type '${constructor1.name}', but was '${obj1.constructor.name}'`
106+
);
107+
if (!obj2 || !constructor2) {
108+
return obj1 as S;
109+
}
83110
debugAssert(
84-
obj instanceof constructor,
85-
`Expected type '${constructor.name}', but was '${obj.constructor.name}'`
111+
obj2 instanceof constructor2,
112+
`Expected type '${constructor2.name}', but was '${obj2.constructor.name}'`
86113
);
87-
return obj as T;
114+
return [obj1 as S, obj2 as T];
88115
}

0 commit comments

Comments
 (0)