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