@@ -44,20 +44,19 @@ import {
44
44
MemoryReferenceDelegate
45
45
} from '../local/memory_persistence' ;
46
46
47
- export interface Components {
47
+ /**
48
+ * Initializes and wires up all core components for Firestore. Implementations
49
+ * override `initialize()` to provide all components.
50
+ */
51
+ export interface ComponentProvider {
48
52
persistence : Persistence ;
49
53
sharedClientState : SharedClientState ;
50
54
localStore : LocalStore ;
51
55
syncEngine : SyncEngine ;
52
56
gcScheduler : GarbageCollectionScheduler | null ;
53
57
remoteStore : RemoteStore ;
54
58
eventManager : EventManager ;
55
- }
56
59
57
- /**
58
- * Initializes and wires up all core components for Firestore.
59
- */
60
- export interface ComponentProvider {
61
60
initialize (
62
61
asyncQueue : AsyncQueue ,
63
62
databaseInfo : DatabaseInfo ,
@@ -67,7 +66,7 @@ export interface ComponentProvider {
67
66
initialUser : User ,
68
67
maxConcurrentLimboResolutions : number ,
69
68
persistenceSettings : PersistenceSettings
70
- ) : Promise < Components > ;
69
+ ) : Promise < void > ;
71
70
72
71
clearPersistence ( databaseId : DatabaseInfo ) : Promise < void > ;
73
72
}
@@ -76,6 +75,14 @@ export interface ComponentProvider {
76
75
* Provides all components needed for Firestore with IndexedDB persistence.
77
76
*/
78
77
export class IndexedDbComponentProvider implements ComponentProvider {
78
+ persistence ! : IndexedDbPersistence ;
79
+ sharedClientState ! : SharedClientState ;
80
+ localStore ! : LocalStore ;
81
+ syncEngine ! : SyncEngine ;
82
+ gcScheduler ! : GarbageCollectionScheduler | null ;
83
+ remoteStore ! : RemoteStore ;
84
+ eventManager ! : EventManager ;
85
+
79
86
async initialize (
80
87
asyncQueue : AsyncQueue ,
81
88
databaseInfo : DatabaseInfo ,
@@ -85,13 +92,12 @@ export class IndexedDbComponentProvider implements ComponentProvider {
85
92
initialUser : User ,
86
93
maxConcurrentLimboResolutions : number ,
87
94
persistenceSettings : PersistenceSettings
88
- ) : Promise < Components > {
95
+ ) : Promise < void > {
89
96
assert (
90
97
persistenceSettings . durable ,
91
98
'IndexedDbComponentProvider can only provide durable persistence'
92
99
) ;
93
-
94
- const components : Partial < Components > = { } ;
100
+ assert ( ! this . sharedClientState , 'initialize() already called' ) ;
95
101
96
102
const persistenceKey = IndexedDbPersistence . buildStoragePrefix (
97
103
databaseInfo
@@ -105,7 +111,7 @@ export class IndexedDbComponentProvider implements ComponentProvider {
105
111
) ;
106
112
}
107
113
108
- components . sharedClientState = persistenceSettings . synchronizeTabs
114
+ this . sharedClientState = persistenceSettings . synchronizeTabs
109
115
? new WebStorageSharedClientState (
110
116
asyncQueue ,
111
117
platform ,
@@ -114,73 +120,69 @@ export class IndexedDbComponentProvider implements ComponentProvider {
114
120
initialUser
115
121
)
116
122
: new MemorySharedClientState ( ) ;
117
- components . sharedClientState . onlineStateHandler = onlineState =>
118
- components . syncEngine ! . applyOnlineStateChange (
123
+ this . sharedClientState . onlineStateHandler = onlineState =>
124
+ this . syncEngine ! . applyOnlineStateChange (
119
125
onlineState ,
120
126
OnlineStateSource . SharedClientState
121
127
) ;
122
128
123
- components . persistence = await IndexedDbPersistence . createIndexedDbPersistence (
124
- {
125
- allowTabSynchronization : persistenceSettings . synchronizeTabs ,
126
- persistenceKey,
127
- clientId,
128
- platform,
129
- queue : asyncQueue ,
130
- serializer,
131
- lruParams : LruParams . withCacheSize ( persistenceSettings . cacheSizeBytes ) ,
132
- sequenceNumberSyncer : components . sharedClientState
133
- }
134
- ) ;
129
+ this . persistence = await IndexedDbPersistence . createIndexedDbPersistence ( {
130
+ allowTabSynchronization : persistenceSettings . synchronizeTabs ,
131
+ persistenceKey,
132
+ clientId,
133
+ platform,
134
+ queue : asyncQueue ,
135
+ serializer,
136
+ lruParams : LruParams . withCacheSize ( persistenceSettings . cacheSizeBytes ) ,
137
+ sequenceNumberSyncer : this . sharedClientState
138
+ } ) ;
135
139
136
- const garbageCollector = ( components . persistence as IndexedDbPersistence )
137
- . referenceDelegate . garbageCollector ;
140
+ const garbageCollector = this . persistence . referenceDelegate
141
+ . garbageCollector ;
138
142
139
- components . gcScheduler = new LruScheduler ( garbageCollector , asyncQueue ) ;
140
- components . localStore = new LocalStore (
141
- components . persistence ,
143
+ this . gcScheduler = new LruScheduler ( garbageCollector , asyncQueue ) ;
144
+ this . localStore = new LocalStore (
145
+ this . persistence ,
142
146
new IndexFreeQueryEngine ( ) ,
143
147
initialUser
144
148
) ;
145
- components . remoteStore = new RemoteStore (
146
- components . localStore ,
149
+ this . remoteStore = new RemoteStore (
150
+ this . localStore ,
147
151
datastore ,
148
152
asyncQueue ,
149
153
onlineState =>
150
- components . syncEngine ! . applyOnlineStateChange (
154
+ this . syncEngine ! . applyOnlineStateChange (
151
155
onlineState ,
152
156
OnlineStateSource . RemoteStore
153
157
) ,
154
158
platform . newConnectivityMonitor ( )
155
159
) ;
156
- components . syncEngine = new SyncEngine (
157
- components . localStore ,
158
- components . remoteStore ,
159
- components . sharedClientState ,
160
+ this . syncEngine = new SyncEngine (
161
+ this . localStore ,
162
+ this . remoteStore ,
163
+ this . sharedClientState ,
160
164
initialUser ,
161
165
maxConcurrentLimboResolutions
162
166
) ;
163
- components . eventManager = new EventManager ( components . syncEngine ) ;
167
+ this . eventManager = new EventManager ( this . syncEngine ) ;
164
168
165
- components . remoteStore . syncEngine = components . syncEngine ;
166
- components . sharedClientState . syncEngine = components . syncEngine ;
169
+ this . remoteStore . syncEngine = this . syncEngine ;
170
+ this . sharedClientState . syncEngine = this . syncEngine ;
167
171
168
- await components . sharedClientState . start ( ) ;
169
- await components . remoteStore . start ( ) ;
170
- await components . localStore . start ( ) ;
172
+ await this . sharedClientState . start ( ) ;
173
+ await this . remoteStore . start ( ) ;
174
+ await this . localStore . start ( ) ;
171
175
172
176
// NOTE: This will immediately call the listener, so we make sure to
173
177
// set it after localStore / remoteStore are started.
174
- await components . persistence . setPrimaryStateListener ( async isPrimary => {
175
- await components . syncEngine ! . applyPrimaryState ( isPrimary ) ;
176
- if ( isPrimary && ! components . gcScheduler ! . started ) {
177
- components . gcScheduler ! . start ( components . localStore ! ) ;
178
+ await this . persistence . setPrimaryStateListener ( async isPrimary => {
179
+ await this . syncEngine . applyPrimaryState ( isPrimary ) ;
180
+ if ( isPrimary && ! this . gcScheduler ! . started ) {
181
+ this . gcScheduler ! . start ( this . localStore ) ;
178
182
} else if ( ! isPrimary ) {
179
- components . gcScheduler ! . stop ( ) ;
183
+ this . gcScheduler ! . stop ( ) ;
180
184
}
181
185
} ) ;
182
-
183
- return components as Components ;
184
186
}
185
187
186
188
clearPersistence ( databaseInfo : DatabaseInfo ) : Promise < void > {
@@ -200,6 +202,14 @@ const MEMORY_ONLY_PERSISTENCE_ERROR_MESSAGE =
200
202
* Provides all components needed for Firestore with in-memory persistence.
201
203
*/
202
204
export class MemoryComponentProvider implements ComponentProvider {
205
+ persistence ! : Persistence ;
206
+ sharedClientState ! : SharedClientState ;
207
+ localStore ! : LocalStore ;
208
+ syncEngine ! : SyncEngine ;
209
+ gcScheduler ! : GarbageCollectionScheduler | null ;
210
+ remoteStore ! : RemoteStore ;
211
+ eventManager ! : EventManager ;
212
+
203
213
constructor (
204
214
readonly referenceDelegateFactory : (
205
215
p : MemoryPersistence
@@ -215,54 +225,50 @@ export class MemoryComponentProvider implements ComponentProvider {
215
225
initialUser : User ,
216
226
maxConcurrentLimboResolutions : number ,
217
227
persistenceSettings : PersistenceSettings
218
- ) : Promise < Components > {
228
+ ) : Promise < void > {
219
229
if ( persistenceSettings . durable ) {
220
230
throw new FirestoreError (
221
231
Code . FAILED_PRECONDITION ,
222
232
MEMORY_ONLY_PERSISTENCE_ERROR_MESSAGE
223
233
) ;
224
234
}
225
235
226
- const components : Partial < Components > = { } ;
227
-
228
- components . persistence = new MemoryPersistence (
236
+ this . persistence = new MemoryPersistence (
229
237
clientId ,
230
238
this . referenceDelegateFactory
231
239
) ;
232
- components . gcScheduler = null ;
233
- components . sharedClientState = new MemorySharedClientState ( ) ;
234
- components . localStore = new LocalStore (
235
- components . persistence ,
240
+ this . gcScheduler = null ;
241
+ this . sharedClientState = new MemorySharedClientState ( ) ;
242
+ this . localStore = new LocalStore (
243
+ this . persistence ,
236
244
new IndexFreeQueryEngine ( ) ,
237
245
initialUser
238
246
) ;
239
- components . remoteStore = new RemoteStore (
240
- components . localStore ,
247
+ this . remoteStore = new RemoteStore (
248
+ this . localStore ,
241
249
datastore ,
242
250
asyncQueue ,
243
251
onlineState =>
244
- components . syncEngine ! . applyOnlineStateChange (
252
+ this . syncEngine ! . applyOnlineStateChange (
245
253
onlineState ,
246
254
OnlineStateSource . RemoteStore
247
255
) ,
248
256
platform . newConnectivityMonitor ( )
249
257
) ;
250
- components . syncEngine = new SyncEngine (
251
- components . localStore ,
252
- components . remoteStore ,
253
- components . sharedClientState ,
258
+ this . syncEngine = new SyncEngine (
259
+ this . localStore ,
260
+ this . remoteStore ,
261
+ this . sharedClientState ,
254
262
initialUser ,
255
263
maxConcurrentLimboResolutions
256
264
) ;
257
- components . eventManager = new EventManager ( components . syncEngine ) ;
258
-
259
- components . remoteStore . syncEngine = components . syncEngine ;
265
+ this . eventManager = new EventManager ( this . syncEngine ) ;
260
266
261
- await components . remoteStore . start ( ) ;
262
- await components . remoteStore . applyPrimaryState ( true ) ;
263
- await components . syncEngine . applyPrimaryState ( true ) ;
267
+ this . remoteStore . syncEngine = this . syncEngine ;
264
268
265
- return components as Components ;
269
+ await this . remoteStore . start ( ) ;
270
+ await this . remoteStore . applyPrimaryState ( true ) ;
271
+ await this . syncEngine . applyPrimaryState ( true ) ;
266
272
}
267
273
268
274
clearPersistence ( ) : never {
0 commit comments