Skip to content

Commit 0462045

Browse files
Clean up IndexedDb initialization (#2163)
1 parent 1ca8b66 commit 0462045

File tree

5 files changed

+80
-131
lines changed

5 files changed

+80
-131
lines changed

packages/firestore/src/core/firestore_client.ts

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ export class FirestoreClient {
328328
): Promise<LruGarbageCollector> {
329329
// TODO(http://b/33384523): For now we just disable garbage collection
330330
// when persistence is enabled.
331-
const storagePrefix = IndexedDbPersistence.buildStoragePrefix(
331+
const persistenceKey = IndexedDbPersistence.buildStoragePrefix(
332332
this.databaseInfo
333333
);
334334
// Opt to use proto3 JSON in case the platform doesn't support Uint8Array.
@@ -347,36 +347,31 @@ export class FirestoreClient {
347347
);
348348
}
349349

350-
let persistence: IndexedDbPersistence;
351350
const lruParams = settings.lruParams();
352-
if (settings.synchronizeTabs) {
353-
this.sharedClientState = new WebStorageSharedClientState(
354-
this.asyncQueue,
355-
this.platform,
356-
storagePrefix,
357-
this.clientId,
358-
user
359-
);
360-
persistence = await IndexedDbPersistence.createMultiClientIndexedDbPersistence(
361-
storagePrefix,
362-
this.clientId,
363-
this.platform,
364-
this.asyncQueue,
351+
352+
this.sharedClientState = settings.synchronizeTabs
353+
? new WebStorageSharedClientState(
354+
this.asyncQueue,
355+
this.platform,
356+
persistenceKey,
357+
this.clientId,
358+
user
359+
)
360+
: new MemorySharedClientState();
361+
362+
const persistence = await IndexedDbPersistence.createIndexedDbPersistence(
363+
{
364+
allowTabSynchronization: settings.synchronizeTabs,
365+
persistenceKey,
366+
clientId: this.clientId,
367+
platform: this.platform,
368+
queue: this.asyncQueue,
365369
serializer,
366370
lruParams,
367-
{ sequenceNumberSyncer: this.sharedClientState }
368-
);
369-
} else {
370-
this.sharedClientState = new MemorySharedClientState();
371-
persistence = await IndexedDbPersistence.createIndexedDbPersistence(
372-
storagePrefix,
373-
this.clientId,
374-
this.platform,
375-
this.asyncQueue,
376-
serializer,
377-
lruParams
378-
);
379-
}
371+
sequenceNumberSyncer: this.sharedClientState
372+
}
373+
);
374+
380375
this.persistence = persistence;
381376
return persistence.referenceDelegate.garbageCollector;
382377
});

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 28 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,6 @@ export class IndexedDbTransaction extends PersistenceTransaction {
168168
* TODO(b/114226234): Remove `synchronizeTabs` section when multi-tab is no
169169
* longer optional.
170170
*/
171-
export interface MultiClientParams {
172-
sequenceNumberSyncer: SequenceNumberSyncer;
173-
}
174171
export class IndexedDbPersistence implements Persistence {
175172
static getStore<Key extends IDBValidKey, Value>(
176173
txn: PersistenceTransaction,
@@ -191,43 +188,32 @@ export class IndexedDbPersistence implements Persistence {
191188
*/
192189
static MAIN_DATABASE = 'main';
193190

194-
static async createIndexedDbPersistence(
195-
persistenceKey: string,
196-
clientId: ClientId,
197-
platform: Platform,
198-
queue: AsyncQueue,
199-
serializer: JsonProtoSerializer,
200-
lruParams: LruParams
201-
): Promise<IndexedDbPersistence> {
202-
const persistence = new IndexedDbPersistence(
203-
persistenceKey,
204-
clientId,
205-
platform,
206-
queue,
207-
serializer,
208-
lruParams
209-
);
210-
await persistence.start();
211-
return persistence;
212-
}
191+
static async createIndexedDbPersistence(options: {
192+
allowTabSynchronization: boolean;
193+
persistenceKey: string;
194+
clientId: ClientId;
195+
platform: Platform;
196+
lruParams: LruParams;
197+
queue: AsyncQueue;
198+
serializer: JsonProtoSerializer;
199+
sequenceNumberSyncer: SequenceNumberSyncer;
200+
}): Promise<IndexedDbPersistence> {
201+
if (!IndexedDbPersistence.isAvailable()) {
202+
throw new FirestoreError(
203+
Code.UNIMPLEMENTED,
204+
UNSUPPORTED_PLATFORM_ERROR_MSG
205+
);
206+
}
213207

214-
static async createMultiClientIndexedDbPersistence(
215-
persistenceKey: string,
216-
clientId: ClientId,
217-
platform: Platform,
218-
queue: AsyncQueue,
219-
serializer: JsonProtoSerializer,
220-
lruParams: LruParams,
221-
multiClientParams: MultiClientParams
222-
): Promise<IndexedDbPersistence> {
223208
const persistence = new IndexedDbPersistence(
224-
persistenceKey,
225-
clientId,
226-
platform,
227-
queue,
228-
serializer,
229-
lruParams,
230-
multiClientParams
209+
options.allowTabSynchronization,
210+
options.persistenceKey,
211+
options.clientId,
212+
options.platform,
213+
options.lruParams,
214+
options.queue,
215+
options.serializer,
216+
options.sequenceNumberSyncer
231217
);
232218
await persistence.start();
233219
return persistence;
@@ -262,9 +248,6 @@ export class IndexedDbPersistence implements Persistence {
262248
/** The last time we garbage collected the Remote Document Changelog. */
263249
private lastGarbageCollectionTime = Number.NEGATIVE_INFINITY;
264250

265-
/** Whether to allow shared multi-tab access to the persistence layer. */
266-
private allowTabSynchronization: boolean;
267-
268251
/** A listener to notify on primary state changes. */
269252
private primaryStateListener: PrimaryStateListener = _ => Promise.resolve();
270253

@@ -274,29 +257,20 @@ export class IndexedDbPersistence implements Persistence {
274257
private readonly webStorage: Storage;
275258
readonly referenceDelegate: IndexedDbLruDelegate;
276259

277-
// Note that `multiClientParams` must be present to enable multi-client support while multi-tab
278-
// is still experimental. When multi-client is switched to always on, `multiClientParams` will
279-
// no longer be optional.
280260
private constructor(
261+
private readonly allowTabSynchronization: boolean,
281262
private readonly persistenceKey: string,
282263
private readonly clientId: ClientId,
283264
platform: Platform,
265+
lruParams: LruParams,
284266
private readonly queue: AsyncQueue,
285267
serializer: JsonProtoSerializer,
286-
lruParams: LruParams,
287-
private readonly multiClientParams?: MultiClientParams
268+
private readonly sequenceNumberSyncer: SequenceNumberSyncer
288269
) {
289-
if (!IndexedDbPersistence.isAvailable()) {
290-
throw new FirestoreError(
291-
Code.UNIMPLEMENTED,
292-
UNSUPPORTED_PLATFORM_ERROR_MSG
293-
);
294-
}
295270
this.referenceDelegate = new IndexedDbLruDelegate(this, lruParams);
296271
this.dbName = persistenceKey + IndexedDbPersistence.MAIN_DATABASE;
297272
this.serializer = new LocalSerializer(serializer);
298273
this.document = platform.document;
299-
this.allowTabSynchronization = multiClientParams !== undefined;
300274
this.queryCache = new IndexedDbQueryCache(
301275
this.referenceDelegate,
302276
this.serializer
@@ -353,12 +327,9 @@ export class IndexedDbPersistence implements Persistence {
353327
txn => {
354328
return getHighestListenSequenceNumber(txn).next(
355329
highestListenSequenceNumber => {
356-
const sequenceNumberSyncer = this.multiClientParams
357-
? this.multiClientParams.sequenceNumberSyncer
358-
: undefined;
359330
this.listenSequence = new ListenSequence(
360331
highestListenSequenceNumber,
361-
sequenceNumberSyncer
332+
this.sequenceNumberSyncer
362333
);
363334
}
364335
);

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

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -119,26 +119,16 @@ async function withCustomPersistence(
119119
PlatformSupport.getPlatform(),
120120
new SharedFakeWebStorage()
121121
);
122-
const persistence = await (multiClient
123-
? IndexedDbPersistence.createMultiClientIndexedDbPersistence(
124-
TEST_PERSISTENCE_PREFIX,
125-
clientId,
126-
platform,
127-
queue,
128-
serializer,
129-
LruParams.DEFAULT,
130-
{
131-
sequenceNumberSyncer: MOCK_SEQUENCE_NUMBER_SYNCER
132-
}
133-
)
134-
: IndexedDbPersistence.createIndexedDbPersistence(
135-
TEST_PERSISTENCE_PREFIX,
136-
clientId,
137-
platform,
138-
queue,
139-
serializer,
140-
LruParams.DEFAULT
141-
));
122+
const persistence = await IndexedDbPersistence.createIndexedDbPersistence({
123+
allowTabSynchronization: multiClient,
124+
persistenceKey: TEST_PERSISTENCE_PREFIX,
125+
clientId,
126+
platform,
127+
queue,
128+
serializer,
129+
lruParams: LruParams.DEFAULT,
130+
sequenceNumberSyncer: MOCK_SEQUENCE_NUMBER_SYNCER
131+
});
142132

143133
await fn(persistence, platform, queue);
144134
await persistence.shutdown();

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -108,24 +108,16 @@ export async function testIndexedDbPersistence(
108108
await SimpleDb.delete(prefix + IndexedDbPersistence.MAIN_DATABASE);
109109
}
110110
const platform = PlatformSupport.getPlatform();
111-
return options.synchronizeTabs
112-
? IndexedDbPersistence.createMultiClientIndexedDbPersistence(
113-
TEST_PERSISTENCE_PREFIX,
114-
clientId,
115-
platform,
116-
queue,
117-
JSON_SERIALIZER,
118-
lruParams,
119-
{ sequenceNumberSyncer: MOCK_SEQUENCE_NUMBER_SYNCER }
120-
)
121-
: IndexedDbPersistence.createIndexedDbPersistence(
122-
TEST_PERSISTENCE_PREFIX,
123-
clientId,
124-
platform,
125-
queue,
126-
JSON_SERIALIZER,
127-
lruParams
128-
);
111+
return IndexedDbPersistence.createIndexedDbPersistence({
112+
allowTabSynchronization: !!options.synchronizeTabs,
113+
persistenceKey: TEST_PERSISTENCE_PREFIX,
114+
clientId,
115+
platform,
116+
queue,
117+
serializer: JSON_SERIALIZER,
118+
lruParams,
119+
sequenceNumberSyncer: MOCK_SEQUENCE_NUMBER_SYNCER
120+
});
129121
}
130122

131123
/** Creates and starts a MemoryPersistence instance for testing. */

packages/firestore/test/unit/specs/spec_test_runner.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,15 +1216,16 @@ class IndexedDbTestRunner extends TestRunner {
12161216
gcEnabled: boolean
12171217
): Promise<Persistence> {
12181218
// TODO(gsoltis): can we or should we disable this test if gc is enabled?
1219-
return IndexedDbPersistence.createMultiClientIndexedDbPersistence(
1220-
TEST_PERSISTENCE_PREFIX,
1221-
this.clientId,
1222-
this.platform,
1223-
this.queue,
1219+
return IndexedDbPersistence.createIndexedDbPersistence({
1220+
allowTabSynchronization: true,
1221+
persistenceKey: TEST_PERSISTENCE_PREFIX,
1222+
clientId: this.clientId,
1223+
platform: this.platform,
1224+
queue: this.queue,
12241225
serializer,
1225-
LruParams.DEFAULT,
1226-
{ sequenceNumberSyncer: this.sharedClientState }
1227-
);
1226+
lruParams: LruParams.DEFAULT,
1227+
sequenceNumberSyncer: this.sharedClientState
1228+
});
12281229
}
12291230

12301231
static destroyPersistence(): Promise<void> {

0 commit comments

Comments
 (0)