diff --git a/.changeset/nice-deers-suffer.md b/.changeset/nice-deers-suffer.md new file mode 100644 index 00000000000..a845151cc84 --- /dev/null +++ b/.changeset/nice-deers-suffer.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/packages/firestore/exp/src/api/database.ts b/packages/firestore/exp/src/api/database.ts index c5b8bfe1710..7839f945014 100644 --- a/packages/firestore/exp/src/api/database.ts +++ b/packages/firestore/exp/src/api/database.ts @@ -45,7 +45,11 @@ import { Code, FirestoreError } from '../../../src/util/error'; import { Deferred } from '../../../src/util/promise'; import { LruParams } from '../../../src/local/lru_garbage_collector'; import { CACHE_SIZE_UNLIMITED } from '../../../src/api/database'; -import { DatabaseInfo } from '../../../src/core/database_info'; +import { DatabaseId, DatabaseInfo } from '../../../src/core/database_info'; +import { + indexedDbStoragePrefix, + indexedDbClearPersistence +} from '../../../src/local/indexeddb_persistence'; /** * The root reference to the Firestore database and the entry point for the @@ -143,7 +147,16 @@ export class Firestore extends LiteFirestore return terminate(this); } - _clearPersistence(): Promise { + /** + * Verifies that the client is not running and clears persistence by invoking + * `delegate` on the async queue. + * + * @param delegate A function that clears the clients + * backing storage. + */ + _clearPersistence( + delegate: (databaseId: DatabaseId, persistenceKey: string) => Promise + ): Promise { if (this._deferredInitialization !== undefined && !this._terminated) { throw new FirestoreError( Code.FAILED_PRECONDITION, @@ -152,16 +165,10 @@ export class Firestore extends LiteFirestore ); } - const settings = this._getSettings(); const deferred = new Deferred(); this._queue.enqueueAndForgetEvenAfterShutdown(async () => { try { - const databaseInfo = this._makeDatabaseInfo( - settings.host, - settings.ssl, - settings.experimentalForceLongPolling - ); - await this._componentProvider.clearPersistence(databaseInfo); + await delegate(this._databaseId, this._persistenceKey); deferred.resolve(); } catch (e) { deferred.reject(e); @@ -247,7 +254,11 @@ export function clearIndexedDbPersistence( firestore: firestore.FirebaseFirestore ): Promise { const firestoreImpl = cast(firestore, Firestore); - return firestoreImpl._clearPersistence(); + return firestoreImpl._clearPersistence((databaseId, persistenceKey) => { + return indexedDbClearPersistence( + indexedDbStoragePrefix(databaseId, persistenceKey) + ); + }); } export function waitForPendingWrites( diff --git a/packages/firestore/exp/test/deps/dependencies.json b/packages/firestore/exp/test/deps/dependencies.json index 4b1fc7958f9..2698488eef8 100644 --- a/packages/firestore/exp/test/deps/dependencies.json +++ b/packages/firestore/exp/test/deps/dependencies.json @@ -336,7 +336,7 @@ ], "variables": [] }, - "sizeInBytes": 231124 + "sizeInBytes": 231553 }, "CollectionReference": { "dependencies": { @@ -711,7 +711,7 @@ ], "variables": [] }, - "sizeInBytes": 257340 + "sizeInBytes": 257769 }, "DocumentReference": { "dependencies": { @@ -1041,7 +1041,7 @@ ], "variables": [] }, - "sizeInBytes": 228101 + "sizeInBytes": 228530 }, "DocumentSnapshot": { "dependencies": { @@ -1398,7 +1398,7 @@ ], "variables": [] }, - "sizeInBytes": 240443 + "sizeInBytes": 240880 }, "FieldPath": { "dependencies": { @@ -1736,7 +1736,7 @@ ], "variables": [] }, - "sizeInBytes": 230348 + "sizeInBytes": 230777 }, "FieldValue": { "dependencies": { @@ -2066,7 +2066,7 @@ ], "variables": [] }, - "sizeInBytes": 227693 + "sizeInBytes": 228122 }, "FirebaseFirestore": { "dependencies": { @@ -2394,7 +2394,7 @@ ], "variables": [] }, - "sizeInBytes": 227602 + "sizeInBytes": 228031 }, "GeoPoint": { "dependencies": { @@ -2731,7 +2731,7 @@ ], "variables": [] }, - "sizeInBytes": 230489 + "sizeInBytes": 230918 }, "Query": { "dependencies": { @@ -3105,7 +3105,7 @@ ], "variables": [] }, - "sizeInBytes": 256944 + "sizeInBytes": 257373 }, "QueryDocumentSnapshot": { "dependencies": { @@ -3462,7 +3462,7 @@ ], "variables": [] }, - "sizeInBytes": 240453 + "sizeInBytes": 240890 }, "QuerySnapshot": { "dependencies": { @@ -3823,7 +3823,7 @@ ], "variables": [] }, - "sizeInBytes": 243352 + "sizeInBytes": 243789 }, "Timestamp": { "dependencies": { @@ -4151,7 +4151,7 @@ ], "variables": [] }, - "sizeInBytes": 227579 + "sizeInBytes": 228008 }, "Transaction": { "dependencies": { @@ -4421,6 +4421,7 @@ "DocumentReference", "DocumentSet", "DocumentSnapshot", + "DocumentSnapshot$1", "DocumentWatchChange", "EventManager", "ExistenceFilter", @@ -4489,6 +4490,7 @@ "Precondition", "Query", "QueryDocumentSnapshot", + "QueryDocumentSnapshot$1", "QueryListener", "QueryListenersInfo", "QueryView", @@ -4501,6 +4503,7 @@ "SerializableFieldValue", "ServerTimestampTransform", "SetMutation", + "SnapshotMetadata", "SnapshotVersion", "SortedMap", "SortedMapIterator", @@ -4516,6 +4519,7 @@ "Timestamp", "Transaction", "Transaction$1", + "Transaction$2", "TransactionRunner", "TransformMutation", "TransformOperation", @@ -4531,7 +4535,7 @@ ], "variables": [] }, - "sizeInBytes": 253423 + "sizeInBytes": 255828 }, "WriteBatch": { "dependencies": { @@ -4906,7 +4910,7 @@ ], "variables": [] }, - "sizeInBytes": 249451 + "sizeInBytes": 249880 }, "addDoc": { "dependencies": { @@ -5288,7 +5292,7 @@ ], "variables": [] }, - "sizeInBytes": 260858 + "sizeInBytes": 261297 }, "arrayRemove": { "dependencies": { @@ -5649,7 +5653,7 @@ ], "variables": [] }, - "sizeInBytes": 240358 + "sizeInBytes": 240787 }, "arrayUnion": { "dependencies": { @@ -6010,7 +6014,7 @@ ], "variables": [] }, - "sizeInBytes": 240351 + "sizeInBytes": 240780 }, "clearIndexedDbPersistence": { "dependencies": { @@ -6052,6 +6056,7 @@ "canonifyTimestamp", "canonifyValue", "cast", + "checkForAndReportiOSError", "clearIndexedDbPersistence", "coercedFieldValuesArray", "compareArrays", @@ -6107,6 +6112,8 @@ "getWindow", "hardAssert", "ignoreIfPrimaryLeaseLoss", + "indexedDbClearPersistence", + "indexedDbStoragePrefix", "invokeBatchGetDocumentsRpc", "invokeCommitRpc", "isArray", @@ -6215,7 +6222,8 @@ "valueEquals", "verifyMutationKeyMatches", "versionFromListenResponse", - "wrapInUserErrorIfRecoverable" + "wrapInUserErrorIfRecoverable", + "wrapRequest" ], "classes": [ "AddedLimboDocument", @@ -6257,6 +6265,8 @@ "GrpcConnection", "InFilter", "IndexFreeQueryEngine", + "IndexedDbTransactionError", + "IterationController", "JsonProtoSerializer", "KeyFieldFilter", "KeyFieldInFilter", @@ -6312,6 +6322,9 @@ "ResourcePath", "ServerTimestampTransform", "SetMutation", + "SimpleDb", + "SimpleDbStore", + "SimpleDbTransaction", "SnapshotVersion", "SortedMap", "SortedMapIterator", @@ -6339,7 +6352,7 @@ ], "variables": [] }, - "sizeInBytes": 227730 + "sizeInBytes": 238401 }, "collection": { "dependencies": { @@ -6716,7 +6729,7 @@ ], "variables": [] }, - "sizeInBytes": 257968 + "sizeInBytes": 258404 }, "collectionGroup": { "dependencies": { @@ -7091,7 +7104,7 @@ ], "variables": [] }, - "sizeInBytes": 257400 + "sizeInBytes": 257829 }, "deleteDoc": { "dependencies": { @@ -7422,7 +7435,7 @@ ], "variables": [] }, - "sizeInBytes": 228351 + "sizeInBytes": 228780 }, "deleteField": { "dependencies": { @@ -7755,7 +7768,7 @@ ], "variables": [] }, - "sizeInBytes": 228767 + "sizeInBytes": 229201 }, "disableNetwork": { "dependencies": { @@ -8084,7 +8097,7 @@ ], "variables": [] }, - "sizeInBytes": 227766 + "sizeInBytes": 228195 }, "doc": { "dependencies": { @@ -8461,7 +8474,7 @@ ], "variables": [] }, - "sizeInBytes": 258044 + "sizeInBytes": 258473 }, "documentId": { "dependencies": { @@ -8800,7 +8813,7 @@ ], "variables": [] }, - "sizeInBytes": 230398 + "sizeInBytes": 230827 }, "enableIndexedDbPersistence": { "dependencies": { @@ -8945,6 +8958,8 @@ "hardAssert", "ignoreIfPrimaryLeaseLoss", "immediateSuccessor", + "indexedDbClearPersistence", + "indexedDbStoragePrefix", "invokeBatchGetDocumentsRpc", "invokeCommitRpc", "isArray", @@ -9232,7 +9247,7 @@ ], "variables": [] }, - "sizeInBytes": 311847 + "sizeInBytes": 312127 }, "enableMultiTabIndexedDbPersistence": { "dependencies": { @@ -9383,6 +9398,8 @@ "hardAssert", "ignoreIfPrimaryLeaseLoss", "immediateSuccessor", + "indexedDbClearPersistence", + "indexedDbStoragePrefix", "invokeBatchGetDocumentsRpc", "invokeCommitRpc", "isArray", @@ -9681,7 +9698,7 @@ ], "variables": [] }, - "sizeInBytes": 336213 + "sizeInBytes": 336519 }, "enableNetwork": { "dependencies": { @@ -10010,7 +10027,7 @@ ], "variables": [] }, - "sizeInBytes": 227763 + "sizeInBytes": 228192 }, "getDoc": { "dependencies": { @@ -10373,7 +10390,7 @@ ], "variables": [] }, - "sizeInBytes": 242885 + "sizeInBytes": 243322 }, "getDocFromCache": { "dependencies": { @@ -10732,7 +10749,7 @@ ], "variables": [] }, - "sizeInBytes": 241065 + "sizeInBytes": 241502 }, "getDocFromServer": { "dependencies": { @@ -11095,7 +11112,7 @@ ], "variables": [] }, - "sizeInBytes": 242923 + "sizeInBytes": 243360 }, "getFirestore": { "dependencies": { @@ -11424,7 +11441,7 @@ ], "variables": [] }, - "sizeInBytes": 227678 + "sizeInBytes": 228107 }, "getQuery": { "dependencies": { @@ -11809,7 +11826,7 @@ ], "variables": [] }, - "sizeInBytes": 263227 + "sizeInBytes": 263664 }, "getQueryFromCache": { "dependencies": { @@ -12190,7 +12207,7 @@ ], "variables": [] }, - "sizeInBytes": 261556 + "sizeInBytes": 261993 }, "getQueryFromServer": { "dependencies": { @@ -12574,7 +12591,7 @@ ], "variables": [] }, - "sizeInBytes": 262965 + "sizeInBytes": 263402 }, "increment": { "dependencies": { @@ -12910,7 +12927,7 @@ ], "variables": [] }, - "sizeInBytes": 228911 + "sizeInBytes": 229340 }, "initializeFirestore": { "dependencies": { @@ -13239,7 +13256,7 @@ ], "variables": [] }, - "sizeInBytes": 227764 + "sizeInBytes": 228518 }, "onSnapshot": { "dependencies": { @@ -13627,7 +13644,7 @@ ], "variables": [] }, - "sizeInBytes": 264883 + "sizeInBytes": 265330 }, "onSnapshotsInSync": { "dependencies": { @@ -13960,7 +13977,7 @@ ], "variables": [] }, - "sizeInBytes": 228959 + "sizeInBytes": 229388 }, "parent": { "dependencies": { @@ -14336,7 +14353,7 @@ ], "variables": [] }, - "sizeInBytes": 257683 + "sizeInBytes": 258112 }, "queryEqual": { "dependencies": { @@ -14711,7 +14728,7 @@ ], "variables": [] }, - "sizeInBytes": 257152 + "sizeInBytes": 257581 }, "refEqual": { "dependencies": { @@ -15087,7 +15104,7 @@ ], "variables": [] }, - "sizeInBytes": 257625 + "sizeInBytes": 258054 }, "runTransaction": { "dependencies": { @@ -15358,6 +15375,7 @@ "DocumentReference", "DocumentSet", "DocumentSnapshot", + "DocumentSnapshot$1", "DocumentWatchChange", "EventManager", "ExistenceFilter", @@ -15426,6 +15444,7 @@ "Precondition", "Query", "QueryDocumentSnapshot", + "QueryDocumentSnapshot$1", "QueryListener", "QueryListenersInfo", "QueryView", @@ -15438,6 +15457,7 @@ "SerializableFieldValue", "ServerTimestampTransform", "SetMutation", + "SnapshotMetadata", "SnapshotVersion", "SortedMap", "SortedMapIterator", @@ -15453,6 +15473,7 @@ "Timestamp", "Transaction", "Transaction$1", + "Transaction$2", "TransactionRunner", "TransformMutation", "TransformOperation", @@ -15468,7 +15489,7 @@ ], "variables": [] }, - "sizeInBytes": 253778 + "sizeInBytes": 256185 }, "serverTimestamp": { "dependencies": { @@ -15802,7 +15823,7 @@ ], "variables": [] }, - "sizeInBytes": 228554 + "sizeInBytes": 228983 }, "setDoc": { "dependencies": { @@ -16170,7 +16191,7 @@ ], "variables": [] }, - "sizeInBytes": 244389 + "sizeInBytes": 244818 }, "setLogLevel": { "dependencies": { @@ -16499,7 +16520,7 @@ ], "variables": [] }, - "sizeInBytes": 227644 + "sizeInBytes": 228073 }, "snapshotEqual": { "dependencies": { @@ -16881,7 +16902,7 @@ ], "variables": [] }, - "sizeInBytes": 261977 + "sizeInBytes": 262414 }, "terminate": { "dependencies": { @@ -17209,7 +17230,7 @@ ], "variables": [] }, - "sizeInBytes": 227594 + "sizeInBytes": 228023 }, "updateDoc": { "dependencies": { @@ -17483,6 +17504,7 @@ "FieldMask", "FieldPath", "FieldPath$1", + "FieldPath$2", "Filter", "FirebaseCredentialsProvider", "Firestore", @@ -17579,7 +17601,7 @@ ], "variables": [] }, - "sizeInBytes": 245753 + "sizeInBytes": 246372 }, "waitForPendingWrites": { "dependencies": { @@ -17908,7 +17930,7 @@ ], "variables": [] }, - "sizeInBytes": 227784 + "sizeInBytes": 228213 }, "writeBatch": { "dependencies": { @@ -18284,6 +18306,6 @@ ], "variables": [] }, - "sizeInBytes": 249664 + "sizeInBytes": 250093 } } \ No newline at end of file diff --git a/packages/firestore/src/api/database.ts b/packages/firestore/src/api/database.ts index a68998623c5..7608023f100 100644 --- a/packages/firestore/src/api/database.ts +++ b/packages/firestore/src/api/database.ts @@ -438,8 +438,10 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService { const deferred = new Deferred(); this._queue.enqueueAndForgetEvenAfterShutdown(async () => { try { - const databaseInfo = this.makeDatabaseInfo(); - await this._componentProvider.clearPersistence(databaseInfo); + await this._componentProvider.clearPersistence( + this._databaseId, + this._persistenceKey + ); deferred.resolve(); } catch (e) { deferred.reject(e); diff --git a/packages/firestore/src/core/component_provider.ts b/packages/firestore/src/core/component_provider.ts index 93436b1fb1e..73fdc87cb70 100644 --- a/packages/firestore/src/core/component_provider.ts +++ b/packages/firestore/src/core/component_provider.ts @@ -36,7 +36,7 @@ import { import { RemoteStore } from '../remote/remote_store'; import { EventManager } from './event_manager'; import { AsyncQueue } from '../util/async_queue'; -import { DatabaseInfo } from './database_info'; +import { DatabaseId, DatabaseInfo } from './database_info'; import { Datastore } from '../remote/datastore'; import { User } from '../auth/user'; import { PersistenceSettings } from './firestore_client'; @@ -46,7 +46,11 @@ import { Code, FirestoreError } from '../util/error'; import { OnlineStateSource } from './types'; import { LruParams, LruScheduler } from '../local/lru_garbage_collector'; import { IndexFreeQueryEngine } from '../local/index_free_query_engine'; -import { IndexedDbPersistence } from '../local/indexeddb_persistence'; +import { + indexedDbStoragePrefix, + IndexedDbPersistence, + indexedDbClearPersistence +} from '../local/indexeddb_persistence'; import { MemoryEagerDelegate, MemoryPersistence @@ -85,7 +89,10 @@ export interface ComponentProvider { initialize(cfg: ComponentConfiguration): Promise; - clearPersistence(databaseId: DatabaseInfo): Promise; + clearPersistence( + databaseId: DatabaseId, + persistenceKey: string + ): Promise; } /** @@ -182,7 +189,10 @@ export class MemoryComponentProvider implements ComponentProvider { ); } - clearPersistence(databaseInfo: DatabaseInfo): Promise { + clearPersistence( + databaseId: DatabaseId, + persistenceKey: string + ): Promise { throw new FirestoreError( Code.FAILED_PRECONDITION, MEMORY_ONLY_PERSISTENCE_ERROR_MESSAGE @@ -229,8 +239,9 @@ export class IndexedDbComponentProvider extends MemoryComponentProvider { 'Can only start durable persistence' ); - const persistenceKey = IndexedDbPersistence.buildStoragePrefix( - cfg.databaseInfo + const persistenceKey = indexedDbStoragePrefix( + cfg.databaseInfo.databaseId, + cfg.databaseInfo.persistenceKey ); const serializer = newSerializer(cfg.databaseInfo.databaseId); return new IndexedDbPersistence( @@ -251,11 +262,13 @@ export class IndexedDbComponentProvider extends MemoryComponentProvider { return new MemorySharedClientState(); } - clearPersistence(databaseInfo: DatabaseInfo): Promise { - const persistenceKey = IndexedDbPersistence.buildStoragePrefix( - databaseInfo + clearPersistence( + databaseId: DatabaseId, + persistenceKey: string + ): Promise { + return indexedDbClearPersistence( + indexedDbStoragePrefix(databaseId, persistenceKey) ); - return IndexedDbPersistence.clearPersistence(persistenceKey); } } @@ -325,8 +338,9 @@ export class MultiTabIndexedDbComponentProvider extends IndexedDbComponentProvid 'IndexedDB persistence is only available on platforms that support LocalStorage.' ); } - const persistenceKey = IndexedDbPersistence.buildStoragePrefix( - cfg.databaseInfo + const persistenceKey = indexedDbStoragePrefix( + cfg.databaseInfo.databaseId, + cfg.databaseInfo.persistenceKey ); return new WebStorageSharedClientState( window, diff --git a/packages/firestore/src/local/indexeddb_persistence.ts b/packages/firestore/src/local/indexeddb_persistence.ts index 170770f6fbe..65d8a80b921 100644 --- a/packages/firestore/src/local/indexeddb_persistence.ts +++ b/packages/firestore/src/local/indexeddb_persistence.ts @@ -16,7 +16,7 @@ */ import { User } from '../auth/user'; -import { DatabaseInfo } from '../core/database_info'; +import { DatabaseId } from '../core/database_info'; import { ListenSequence, SequenceNumberSyncer } from '../core/listen_sequence'; import { ListenSequenceNumber, TargetId } from '../core/types'; import { DocumentKey } from '../model/document_key'; @@ -116,6 +116,12 @@ const UNSUPPORTED_PLATFORM_ERROR_MSG = // firestore_zombie__ const ZOMBIED_CLIENTS_KEY_PREFIX = 'firestore_zombie'; +/** + * The name of the main (and currently only) IndexedDB database. This name is + * appended to the prefix provided to the IndexedDbPersistence constructor. + */ +export const MAIN_DATABASE = 'main'; + export class IndexedDbTransaction extends PersistenceTransaction { constructor( readonly simpleDbTransaction: SimpleDbTransaction, @@ -185,12 +191,6 @@ export class IndexedDbPersistence implements Persistence { } } - /** - * The name of the main (and currently only) IndexedDB database. this name is - * appended to the prefix provided to the IndexedDbPersistence constructor. - */ - static MAIN_DATABASE = 'main'; - // Technically `simpleDb` should be `| undefined` because it is // initialized asynchronously by start(), but that would be more misleading // than useful. @@ -257,7 +257,7 @@ export class IndexedDbPersistence implements Persistence { } this.referenceDelegate = new IndexedDbLruDelegate(this, lruParams); - this.dbName = persistenceKey + IndexedDbPersistence.MAIN_DATABASE; + this.dbName = persistenceKey + MAIN_DATABASE; this.serializer = new LocalSerializer(serializer); this.targetCache = new IndexedDbTargetCache( this.referenceDelegate, @@ -733,14 +733,6 @@ export class IndexedDbPersistence implements Persistence { }); } - static async clearPersistence(persistenceKey: string): Promise { - if (!IndexedDbPersistence.isAvailable()) { - return Promise.resolve(); - } - const dbName = persistenceKey + IndexedDbPersistence.MAIN_DATABASE; - await SimpleDb.delete(dbName); - } - get started(): boolean { return this._started; } @@ -905,26 +897,6 @@ export class IndexedDbPersistence implements Persistence { return SimpleDb.isAvailable(); } - /** - * Generates a string used as a prefix when storing data in IndexedDB and - * LocalStorage. - */ - static buildStoragePrefix(databaseInfo: DatabaseInfo): string { - // Use two different prefix formats: - // - // * firestore / persistenceKey / projectID . databaseID / ... - // * firestore / persistenceKey / projectID / ... - // - // projectIDs are DNS-compatible names and cannot contain dots - // so there's no danger of collisions. - let database = databaseInfo.databaseId.projectId; - if (!databaseInfo.databaseId.isDefaultDatabase) { - database += '.' + databaseInfo.databaseId.database; - } - - return 'firestore/' + databaseInfo.persistenceKey + '/' + database + '/'; - } - /** Checks the primary lease and removes it if we are the current primary. */ private releasePrimaryLeaseIfHeld( txn: PersistenceTransaction @@ -1335,3 +1307,36 @@ function writeSentinelKey( sentinelRow(key, txn.currentSequenceNumber) ); } + +/** + * Generates a string used as a prefix when storing data in IndexedDB and + * LocalStorage. + */ +export function indexedDbStoragePrefix( + databaseId: DatabaseId, + persistenceKey: string +): string { + // Use two different prefix formats: + // + // * firestore / persistenceKey / projectID . databaseID / ... + // * firestore / persistenceKey / projectID / ... + // + // projectIDs are DNS-compatible names and cannot contain dots + // so there's no danger of collisions. + let database = databaseId.projectId; + if (!databaseId.isDefaultDatabase) { + database += '.' + databaseId.database; + } + + return 'firestore/' + persistenceKey + '/' + database + '/'; +} + +export async function indexedDbClearPersistence( + persistenceKey: string +): Promise { + if (!SimpleDb.isAvailable()) { + return Promise.resolve(); + } + const dbName = persistenceKey + MAIN_DATABASE; + await SimpleDb.delete(dbName); +} diff --git a/packages/firestore/test/unit/local/persistence_test_helpers.ts b/packages/firestore/test/unit/local/persistence_test_helpers.ts index a3087bd0a00..be50d4e9e66 100644 --- a/packages/firestore/test/unit/local/persistence_test_helpers.ts +++ b/packages/firestore/test/unit/local/persistence_test_helpers.ts @@ -16,7 +16,7 @@ */ import { User } from '../../../src/auth/user'; -import { DatabaseId, DatabaseInfo } from '../../../src/core/database_info'; +import { DatabaseId } from '../../../src/core/database_info'; import { SequenceNumberSyncer } from '../../../src/core/listen_sequence'; import { BatchId, @@ -26,7 +26,12 @@ import { ListenSequenceNumber } from '../../../src/core/types'; -import { IndexedDbPersistence } from '../../../src/local/indexeddb_persistence'; +import { + indexedDbStoragePrefix, + indexedDbClearPersistence, + IndexedDbPersistence, + MAIN_DATABASE +} from '../../../src/local/indexeddb_persistence'; import { LocalSerializer } from '../../../src/local/local_serializer'; import { LruParams } from '../../../src/local/lru_garbage_collector'; import { @@ -61,18 +66,10 @@ export const MOCK_SEQUENCE_NUMBER_SYNCER: SequenceNumberSyncer = { export const TEST_DATABASE_ID = new DatabaseId('test-project'); export const TEST_PERSISTENCE_KEY = '[PersistenceTestHelpers]'; -/** The DatabaseInfo used by tests that need a serializer. */ -const TEST_DATABASE_INFO = new DatabaseInfo( - TEST_DATABASE_ID, - TEST_PERSISTENCE_KEY, - 'host', - /*ssl=*/ false, - /*forceLongPolling=*/ false -); - /** The persistence prefix used for testing in IndexedBD and LocalStorage. */ -export const TEST_PERSISTENCE_PREFIX = IndexedDbPersistence.buildStoragePrefix( - TEST_DATABASE_INFO +export const TEST_PERSISTENCE_PREFIX = indexedDbStoragePrefix( + TEST_DATABASE_ID, + TEST_PERSISTENCE_KEY ); /** @@ -81,8 +78,8 @@ export const TEST_PERSISTENCE_PREFIX = IndexedDbPersistence.buildStoragePrefix( * `TEST_DATABASE_ID`. */ export const INDEXEDDB_TEST_DATABASE_NAME = - IndexedDbPersistence.buildStoragePrefix(TEST_DATABASE_INFO) + - IndexedDbPersistence.MAIN_DATABASE; + indexedDbStoragePrefix(TEST_DATABASE_ID, TEST_PERSISTENCE_KEY) + + MAIN_DATABASE; const JSON_SERIALIZER = new JsonProtoSerializer( TEST_DATABASE_ID, @@ -111,7 +108,7 @@ export async function testIndexedDbPersistence( const clientId = AutoId.newId(); const prefix = `${TEST_PERSISTENCE_PREFIX}/`; if (!options.dontPurgeData) { - await SimpleDb.delete(prefix + IndexedDbPersistence.MAIN_DATABASE); + await SimpleDb.delete(prefix + MAIN_DATABASE); } const persistence = new IndexedDbPersistence( !!options.synchronizeTabs, @@ -142,7 +139,7 @@ export async function testMemoryLruPersistence( /** Clears the persistence in tests */ export function clearTestPersistence(): Promise { - return IndexedDbPersistence.clearPersistence(TEST_PERSISTENCE_PREFIX); + return indexedDbClearPersistence(TEST_PERSISTENCE_PREFIX); } class NoOpSharedClientStateSyncer implements SharedClientStateSyncer { diff --git a/packages/firestore/test/unit/specs/spec_test_components.ts b/packages/firestore/test/unit/specs/spec_test_components.ts index cc922094a56..e238fa4b663 100644 --- a/packages/firestore/test/unit/specs/spec_test_components.ts +++ b/packages/firestore/test/unit/specs/spec_test_components.ts @@ -26,7 +26,10 @@ import { PersistenceTransaction, PersistenceTransactionMode } from '../../../src/local/persistence'; -import { IndexedDbPersistence } from '../../../src/local/indexeddb_persistence'; +import { + indexedDbStoragePrefix, + IndexedDbPersistence +} from '../../../src/local/indexeddb_persistence'; import { PersistencePromise } from '../../../src/local/persistence_promise'; import { IndexedDbTransactionError } from '../../../src/local/simple_db'; import { debugAssert, fail } from '../../../src/util/assert'; @@ -131,8 +134,9 @@ export class MockIndexedDbComponentProvider extends MultiTabIndexedDbComponentPr } createSharedClientState(cfg: ComponentConfiguration): SharedClientState { - const persistenceKey = IndexedDbPersistence.buildStoragePrefix( - cfg.databaseInfo + const persistenceKey = indexedDbStoragePrefix( + cfg.databaseInfo.databaseId, + cfg.databaseInfo.persistenceKey ); return new WebStorageSharedClientState( this.window, @@ -149,8 +153,9 @@ export class MockIndexedDbComponentProvider extends MultiTabIndexedDbComponentPr 'Can only start durable persistence' ); - const persistenceKey = IndexedDbPersistence.buildStoragePrefix( - cfg.databaseInfo + const persistenceKey = indexedDbStoragePrefix( + cfg.databaseInfo.databaseId, + cfg.databaseInfo.persistenceKey ); const serializer = newSerializer(cfg.databaseInfo.databaseId);