@@ -22,6 +22,7 @@ import { decode, encode } from './encoded_resource_path';
22
22
import { IndexManager } from './index_manager' ;
23
23
import { IndexedDbPersistence } from './indexeddb_persistence' ;
24
24
import { DbCollectionParent , DbCollectionParentKey } from './indexeddb_schema' ;
25
+ import { MemoryCollectionParentIndex } from './memory_index_manager' ;
25
26
import { PersistenceTransaction } from './persistence' ;
26
27
import { PersistencePromise } from './persistence_promise' ;
27
28
import { SimpleDbStore } from './simple_db' ;
@@ -30,17 +31,43 @@ import { SimpleDbStore } from './simple_db';
30
31
* A persisted implementation of IndexManager.
31
32
*/
32
33
export class IndexedDbIndexManager implements IndexManager {
34
+ /**
35
+ * An in-memory copy of the index entries we've already written since the SDK
36
+ * launched. Used to avoid re-writing the same entry repeatedly.
37
+ *
38
+ * This is *NOT* a complete cache of what's in persistence and so can never be used to
39
+ * satisfy reads.
40
+ */
41
+ private collectionParentsCache = new MemoryCollectionParentIndex ( ) ;
42
+
43
+ /**
44
+ * Adds a new entry to the collection parent index.
45
+ *
46
+ * Repeated calls for the same collectionPath should be avoided within a
47
+ * transaction as IndexedDbIndexManager only caches writes once a transaction
48
+ * has been committed.
49
+ */
33
50
addToCollectionParentIndex (
34
51
transaction : PersistenceTransaction ,
35
52
collectionPath : ResourcePath
36
53
) : PersistencePromise < void > {
37
54
assert ( collectionPath . length % 2 === 1 , 'Expected a collection path.' ) ;
38
- const collectionId = collectionPath . lastSegment ( ) ;
39
- const parentPath = collectionPath . popLast ( ) ;
40
- return collectionParentsStore ( transaction ) . put ( {
41
- collectionId,
42
- parent : encode ( parentPath )
43
- } ) ;
55
+ if ( ! this . collectionParentsCache . has ( collectionPath ) ) {
56
+ const collectionId = collectionPath . lastSegment ( ) ;
57
+ const parentPath = collectionPath . popLast ( ) ;
58
+
59
+ transaction . addOnCommittedListener ( ( ) => {
60
+ // Add the collection to the in memory cache only if the transaction was
61
+ // successfully committed.
62
+ this . collectionParentsCache . add ( collectionPath ) ;
63
+ } ) ;
64
+
65
+ return collectionParentsStore ( transaction ) . put ( {
66
+ collectionId,
67
+ parent : encode ( parentPath )
68
+ } ) ;
69
+ }
70
+ return PersistencePromise . resolve ( ) ;
44
71
}
45
72
46
73
getCollectionParents (
0 commit comments