@@ -20,7 +20,12 @@ import { FirebaseApp } from '@firebase/app-types';
20
20
import { FirebaseService } from '@firebase/app-types/private' ;
21
21
import { DatabaseId , DatabaseInfo } from '../core/database_info' ;
22
22
import { ListenOptions } from '../core/event_manager' ;
23
- import { FirestoreClient } from '../core/firestore_client' ;
23
+ import {
24
+ FirestoreClient ,
25
+ IndexedDbPersistenceSettings ,
26
+ InternalPersistenceSettings ,
27
+ MemoryPersistenceSettings
28
+ } from '../core/firestore_client' ;
24
29
import {
25
30
Bound ,
26
31
Direction ,
@@ -32,6 +37,7 @@ import {
32
37
} from '../core/query' ;
33
38
import { Transaction as InternalTransaction } from '../core/transaction' ;
34
39
import { ChangeType , ViewSnapshot } from '../core/view_snapshot' ;
40
+ import { LruParams } from '../local/lru_garbage_collector' ;
35
41
import { Document , MaybeDocument , NoDocument } from '../model/document' ;
36
42
import { DocumentKey } from '../model/document_key' ;
37
43
import {
@@ -102,6 +108,13 @@ const DEFAULT_HOST = 'firestore.googleapis.com';
102
108
const DEFAULT_SSL = true ;
103
109
const DEFAULT_TIMESTAMPS_IN_SNAPSHOTS = false ;
104
110
111
+ /**
112
+ * Constant used to indicate the LRU garbage collection should be disabled.
113
+ * Set this value as the `cacheSizeBytes` on the settings passed to the
114
+ * `Firestore` instance.
115
+ */
116
+ export const CACHE_SIZE_UNLIMITED = LruParams . COLLECTION_DISABLED ;
117
+
105
118
// enablePersistence() defaults:
106
119
const DEFAULT_SYNCHRONIZE_TABS = false ;
107
120
@@ -127,12 +140,14 @@ export interface FirestoreDatabase {
127
140
*/
128
141
class FirestoreSettings {
129
142
/** The hostname to connect to. */
130
- host : string ;
143
+ readonly host : string ;
131
144
132
145
/** Whether to use SSL when connecting. */
133
- ssl : boolean ;
146
+ readonly ssl : boolean ;
147
+
148
+ readonly timestampsInSnapshots : boolean ;
134
149
135
- timestampsInSnapshots : boolean ;
150
+ readonly cacheSizeBytes : number ;
136
151
137
152
// Can be a google-auth-library or gapi client.
138
153
// tslint:disable-next-line:no-any
@@ -159,7 +174,8 @@ class FirestoreSettings {
159
174
'host' ,
160
175
'ssl' ,
161
176
'credentials' ,
162
- 'timestampsInSnapshots'
177
+ 'timestampsInSnapshots' ,
178
+ 'cacheSizeBytes'
163
179
] ) ;
164
180
165
181
validateNamedOptionalType (
@@ -180,14 +196,39 @@ class FirestoreSettings {
180
196
settings . timestampsInSnapshots ,
181
197
DEFAULT_TIMESTAMPS_IN_SNAPSHOTS
182
198
) ;
199
+
200
+ validateNamedOptionalType (
201
+ 'settings' ,
202
+ 'number' ,
203
+ 'cacheSizeBytes' ,
204
+ settings . cacheSizeBytes
205
+ ) ;
206
+ if ( settings . cacheSizeBytes === undefined ) {
207
+ this . cacheSizeBytes = LruParams . DEFAULT_CACHE_SIZE_BYTES ;
208
+ } else {
209
+ if (
210
+ settings . cacheSizeBytes !== CACHE_SIZE_UNLIMITED &&
211
+ settings . cacheSizeBytes < LruParams . MINIMUM_CACHE_SIZE_BYTES
212
+ ) {
213
+ throw new FirestoreError (
214
+ Code . INVALID_ARGUMENT ,
215
+ `cacheSizeBytes must be at least ${
216
+ LruParams . MINIMUM_CACHE_SIZE_BYTES
217
+ } `
218
+ ) ;
219
+ } else {
220
+ this . cacheSizeBytes = settings . cacheSizeBytes ;
221
+ }
222
+ }
183
223
}
184
224
185
225
isEqual ( other : FirestoreSettings ) : boolean {
186
226
return (
187
227
this . host === other . host &&
188
228
this . ssl === other . ssl &&
189
229
this . timestampsInSnapshots === other . timestampsInSnapshots &&
190
- this . credentials === other . credentials
230
+ this . credentials === other . credentials &&
231
+ this . cacheSizeBytes === other . cacheSizeBytes
191
232
) ;
192
233
}
193
234
}
@@ -201,38 +242,6 @@ class FirestoreConfig {
201
242
persistence : boolean ;
202
243
}
203
244
204
- /**
205
- * Encapsulates the settings that can be used to configure Firestore
206
- * persistence.
207
- */
208
- export class PersistenceSettings {
209
- /** Whether to enable multi-tab synchronization. */
210
- experimentalTabSynchronization : boolean ;
211
-
212
- constructor (
213
- readonly enabled : boolean ,
214
- settings ?: firestore . PersistenceSettings
215
- ) {
216
- assert (
217
- enabled || ! settings ,
218
- 'Can only provide PersistenceSettings with persistence enabled'
219
- ) ;
220
- settings = settings || { } ;
221
- this . experimentalTabSynchronization = objUtils . defaulted (
222
- settings . experimentalTabSynchronization ,
223
- DEFAULT_SYNCHRONIZE_TABS
224
- ) ;
225
- }
226
-
227
- isEqual ( other : PersistenceSettings ) : boolean {
228
- return (
229
- this . enabled === other . enabled &&
230
- this . experimentalTabSynchronization ===
231
- other . experimentalTabSynchronization
232
- ) ;
233
- }
234
- }
235
-
236
245
/**
237
246
* The root reference to the database.
238
247
*/
@@ -335,23 +344,29 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
335
344
'any other methods on a Firestore object.'
336
345
) ;
337
346
}
338
-
339
347
return this . configureClient (
340
- new PersistenceSettings ( /* enabled= */ true , settings )
348
+ new IndexedDbPersistenceSettings (
349
+ this . _config . settings . cacheSizeBytes ,
350
+ settings !== undefined &&
351
+ objUtils . defaulted (
352
+ settings . experimentalTabSynchronization ,
353
+ DEFAULT_SYNCHRONIZE_TABS
354
+ )
355
+ )
341
356
) ;
342
357
}
343
358
344
359
ensureClientConfigured ( ) : FirestoreClient {
345
360
if ( ! this . _firestoreClient ) {
346
361
// Kick off starting the client but don't actually wait for it.
347
362
// tslint:disable-next-line:no-floating-promises
348
- this . configureClient ( new PersistenceSettings ( /* enabled= */ false ) ) ;
363
+ this . configureClient ( new MemoryPersistenceSettings ( ) ) ;
349
364
}
350
365
return this . _firestoreClient as FirestoreClient ;
351
366
}
352
367
353
368
private configureClient (
354
- persistenceSettings : PersistenceSettings
369
+ persistenceSettings : InternalPersistenceSettings
355
370
) : Promise < void > {
356
371
assert (
357
372
! ! this . _config . settings . host ,
0 commit comments