Skip to content

Commit 2c8c17c

Browse files
Tree-Shakeable LocalSerializer (#3241)
1 parent 58e1b6f commit 2c8c17c

6 files changed

+260
-214
lines changed

packages/firestore/src/local/indexeddb_mutation_queue.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ import {
4242
DbMutationQueue,
4343
DbMutationQueueKey
4444
} from './indexeddb_schema';
45-
import { LocalSerializer } from './local_serializer';
45+
import {
46+
fromDbMutationBatch,
47+
LocalSerializer,
48+
toDbMutationBatch
49+
} from './local_serializer';
4650
import { MutationQueue } from './mutation_queue';
4751
import { PersistenceTransaction, ReferenceDelegate } from './persistence';
4852
import { PersistencePromise } from './persistence_promise';
@@ -148,7 +152,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
148152
baseMutations,
149153
mutations
150154
);
151-
const dbBatch = this.serializer.toDbMutationBatch(this.userId, batch);
155+
const dbBatch = toDbMutationBatch(this.serializer, this.userId, batch);
152156

153157
const promises: Array<PersistencePromise<void>> = [];
154158
let collectionParents = new SortedSet<ResourcePath>((l, r) =>
@@ -193,7 +197,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
193197
dbBatch.userId === this.userId,
194198
`Unexpected user '${dbBatch.userId}' for mutation batch ${batchId}`
195199
);
196-
return this.serializer.fromDbMutationBatch(dbBatch);
200+
return fromDbMutationBatch(this.serializer, dbBatch);
197201
}
198202
return null;
199203
});
@@ -244,7 +248,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
244248
dbBatch.batchId >= nextBatchId,
245249
'Should have found mutation after ' + nextBatchId
246250
);
247-
foundBatch = this.serializer.fromDbMutationBatch(dbBatch);
251+
foundBatch = fromDbMutationBatch(this.serializer, dbBatch);
248252
}
249253
control.done();
250254
}
@@ -282,7 +286,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
282286
return mutationsStore(transaction)
283287
.loadAll(DbMutationBatch.userMutationsIndex, range)
284288
.next(dbBatches =>
285-
dbBatches.map(dbBatch => this.serializer.fromDbMutationBatch(dbBatch))
289+
dbBatches.map(dbBatch => fromDbMutationBatch(this.serializer, dbBatch))
286290
);
287291
}
288292

@@ -331,7 +335,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
331335
mutation.userId === this.userId,
332336
`Unexpected user '${mutation.userId}' for mutation batch ${batchId}`
333337
);
334-
results.push(this.serializer.fromDbMutationBatch(mutation));
338+
results.push(fromDbMutationBatch(this.serializer, mutation));
335339
});
336340
})
337341
.next(() => results);
@@ -462,7 +466,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
462466
mutation.userId === this.userId,
463467
`Unexpected user '${mutation.userId}' for mutation batch ${batchId}`
464468
);
465-
results.push(this.serializer.fromDbMutationBatch(mutation));
469+
results.push(fromDbMutationBatch(this.serializer, mutation));
466470
})
467471
);
468472
});

packages/firestore/src/local/indexeddb_remote_document_cache.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ import {
4444
DbRemoteDocumentGlobalKey,
4545
DbRemoteDocumentKey
4646
} from './indexeddb_schema';
47-
import { LocalSerializer } from './local_serializer';
47+
import {
48+
fromDbRemoteDocument,
49+
fromDbTimestampKey,
50+
LocalSerializer,
51+
toDbRemoteDocument,
52+
toDbTimestampKey
53+
} from './local_serializer';
4854
import { PersistenceTransaction } from './persistence';
4955
import { PersistencePromise } from './persistence_promise';
5056
import { RemoteDocumentCache } from './remote_document_cache';
@@ -262,7 +268,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
262268
// since all document changes to queries that have a
263269
// lastLimboFreeSnapshotVersion (`sinceReadTime`) have a read time set.
264270
const collectionKey = query.path.toArray();
265-
const readTimeKey = this.serializer.toDbTimestampKey(sinceReadTime);
271+
const readTimeKey = toDbTimestampKey(sinceReadTime);
266272
iterationOptions.range = IDBKeyRange.lowerBound(
267273
[collectionKey, readTimeKey],
268274
/* open= */ true
@@ -281,7 +287,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
281287
return;
282288
}
283289

284-
const maybeDoc = this.serializer.fromDbRemoteDocument(dbRemoteDoc);
290+
const maybeDoc = fromDbRemoteDocument(this.serializer, dbRemoteDoc);
285291
if (!query.path.isPrefixOf(maybeDoc.key.path)) {
286292
control.done();
287293
} else if (maybeDoc instanceof Document && query.matches(maybeDoc)) {
@@ -305,7 +311,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
305311
}> {
306312
let changedDocs = maybeDocumentMap();
307313

308-
let lastReadTime = this.serializer.toDbTimestampKey(sinceReadTime);
314+
let lastReadTime = toDbTimestampKey(sinceReadTime);
309315

310316
const documentsStore = remoteDocumentsStore(transaction);
311317
const range = IDBKeyRange.lowerBound(lastReadTime, true);
@@ -315,15 +321,15 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
315321
(_, dbRemoteDoc) => {
316322
// Unlike `getEntry()` and others, `getNewDocumentChanges()` parses
317323
// the documents directly since we want to keep sentinel deletes.
318-
const doc = this.serializer.fromDbRemoteDocument(dbRemoteDoc);
324+
const doc = fromDbRemoteDocument(this.serializer, dbRemoteDoc);
319325
changedDocs = changedDocs.insert(doc.key, doc);
320326
lastReadTime = dbRemoteDoc.readTime!;
321327
}
322328
)
323329
.next(() => {
324330
return {
325331
changedDocs,
326-
readTime: this.serializer.fromDbTimestampKey(lastReadTime)
332+
readTime: fromDbTimestampKey(lastReadTime)
327333
};
328334
});
329335
}
@@ -346,7 +352,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
346352
{ index: DbRemoteDocument.readTimeIndex, reverse: true },
347353
(key, dbRemoteDoc, control) => {
348354
if (dbRemoteDoc.readTime) {
349-
readTime = this.serializer.fromDbTimestampKey(dbRemoteDoc.readTime);
355+
readTime = fromDbTimestampKey(dbRemoteDoc.readTime);
350356
}
351357
control.done();
352358
}
@@ -393,7 +399,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
393399
dbRemoteDoc: DbRemoteDocument | null
394400
): MaybeDocument | null {
395401
if (dbRemoteDoc) {
396-
const doc = this.serializer.fromDbRemoteDocument(dbRemoteDoc);
402+
const doc = fromDbRemoteDocument(this.serializer, dbRemoteDoc);
397403
if (
398404
doc instanceof NoDocument &&
399405
doc.version.isEqual(SnapshotVersion.min())
@@ -456,7 +462,8 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
456462
!this.readTime.isEqual(SnapshotVersion.min()),
457463
'Cannot add a document with a read time of zero'
458464
);
459-
const doc = this.documentCache.serializer.toDbRemoteDocument(
465+
const doc = toDbRemoteDocument(
466+
this.documentCache.serializer,
460467
maybeDocument,
461468
this.readTime
462469
);
@@ -472,7 +479,8 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
472479
// RemoteDocumentCache. This entry is represented by a NoDocument
473480
// with a version of 0 and ignored by `maybeDecodeDocument()` but
474481
// preserved in `getNewDocumentChanges()`.
475-
const deletedDoc = this.documentCache.serializer.toDbRemoteDocument(
482+
const deletedDoc = toDbRemoteDocument(
483+
this.documentCache.serializer,
476484
new NoDocument(key, SnapshotVersion.min()),
477485
this.readTime
478486
);

packages/firestore/src/local/indexeddb_schema.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,23 @@
1818
import { BatchId, ListenSequenceNumber, TargetId } from '../core/types';
1919
import { ResourcePath } from '../model/path';
2020
import * as api from '../protos/firestore_proto_api';
21-
import { hardAssert, debugAssert } from '../util/assert';
21+
import { debugAssert, hardAssert } from '../util/assert';
2222

2323
import { SnapshotVersion } from '../core/snapshot_version';
2424
import { BATCHID_UNKNOWN } from '../model/mutation_batch';
2525
import {
2626
decodeResourcePath,
27-
encodeResourcePath,
28-
EncodedResourcePath
27+
EncodedResourcePath,
28+
encodeResourcePath
2929
} from './encoded_resource_path';
3030
import { removeMutationBatch } from './indexeddb_mutation_queue';
3131
import { dbDocumentSize } from './indexeddb_remote_document_cache';
32-
import { LocalSerializer } from './local_serializer';
32+
import {
33+
fromDbMutationBatch,
34+
fromDbTarget,
35+
LocalSerializer,
36+
toDbTarget
37+
} from './local_serializer';
3338
import { MemoryCollectionParentIndex } from './memory_index_manager';
3439
import { PersistencePromise } from './persistence_promise';
3540
import { SimpleDbSchemaConverter, SimpleDbTransaction } from './simple_db';
@@ -202,7 +207,7 @@ export class SchemaConverter implements SimpleDbSchemaConverter {
202207
dbBatch.userId === queue.userId,
203208
`Cannot process batch ${dbBatch.batchId} from unexpected user`
204209
);
205-
const batch = this.serializer.fromDbMutationBatch(dbBatch);
210+
const batch = fromDbMutationBatch(this.serializer, dbBatch);
206211

207212
return removeMutationBatch(
208213
txn,
@@ -324,8 +329,8 @@ export class SchemaConverter implements SimpleDbSchemaConverter {
324329
): PersistencePromise<void> {
325330
const targetStore = txn.store<DbTargetKey, DbTarget>(DbTarget.store);
326331
return targetStore.iterate((key, originalDbTarget) => {
327-
const originalTargetData = this.serializer.fromDbTarget(originalDbTarget);
328-
const updatedDbTarget = this.serializer.toDbTarget(originalTargetData);
332+
const originalTargetData = fromDbTarget(originalDbTarget);
333+
const updatedDbTarget = toDbTarget(this.serializer, originalTargetData);
329334
return targetStore.put(updatedDbTarget);
330335
});
331336
}

packages/firestore/src/local/indexeddb_target_cache.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import {
3939
DbTargetGlobalKey,
4040
DbTargetKey
4141
} from './indexeddb_schema';
42-
import { LocalSerializer } from './local_serializer';
42+
import { fromDbTarget, LocalSerializer, toDbTarget } from './local_serializer';
4343
import { ActiveTargets } from './lru_garbage_collector';
4444
import { PersistenceTransaction } from './persistence';
4545
import { PersistencePromise } from './persistence_promise';
@@ -162,7 +162,7 @@ export class IndexedDbTargetCache implements TargetCache {
162162
const promises: Array<PersistencePromise<void>> = [];
163163
return targetsStore(txn)
164164
.iterate((key, value) => {
165-
const targetData = this.serializer.fromDbTarget(value);
165+
const targetData = fromDbTarget(value);
166166
if (
167167
targetData.sequenceNumber <= upperBound &&
168168
activeTargetIds.get(targetData.targetId) === null
@@ -183,7 +183,7 @@ export class IndexedDbTargetCache implements TargetCache {
183183
f: (q: TargetData) => void
184184
): PersistencePromise<void> {
185185
return targetsStore(txn).iterate((key, value) => {
186-
const targetData = this.serializer.fromDbTarget(value);
186+
const targetData = fromDbTarget(value);
187187
f(targetData);
188188
});
189189
}
@@ -211,7 +211,7 @@ export class IndexedDbTargetCache implements TargetCache {
211211
targetData: TargetData
212212
): PersistencePromise<void> {
213213
return targetsStore(transaction).put(
214-
this.serializer.toDbTarget(targetData)
214+
toDbTarget(this.serializer, targetData)
215215
);
216216
}
217217

@@ -262,7 +262,7 @@ export class IndexedDbTargetCache implements TargetCache {
262262
.iterate(
263263
{ range, index: DbTarget.queryTargetsIndexName },
264264
(key, value, control) => {
265-
const found = this.serializer.fromDbTarget(value);
265+
const found = fromDbTarget(value);
266266
// After finding a potential match, check that the target is
267267
// actually equal to the requested target.
268268
if (targetEquals(target, found.target)) {
@@ -392,7 +392,7 @@ export class IndexedDbTargetCache implements TargetCache {
392392
.get(targetId)
393393
.next(found => {
394394
if (found) {
395-
return this.serializer.fromDbTarget(found);
395+
return fromDbTarget(found);
396396
} else {
397397
return null;
398398
}

0 commit comments

Comments
 (0)