15
15
* limitations under the License.
16
16
*/
17
17
18
+ import { ensureFirestoreConfigured , Firestore } from '../api/database' ;
19
+ import {
20
+ PersistentCacheIndexManager ,
21
+ TestingHooks as PersistentCacheIndexManagerTestingHooks
22
+ } from '../api/persistent_cache_index_manager' ;
18
23
import { Unsubscribe } from '../api/reference_impl' ;
24
+ import { TestingHooks as FirestoreClientTestingHooks } from '../core/firestore_client' ;
25
+ import { Query } from '../lite-api/reference' ;
26
+ import { IndexType } from '../local/index_manager' ;
19
27
28
+ import { cast } from './input_validation' ;
20
29
import {
21
30
setTestingHooksSpi ,
22
31
ExistenceFilterMismatchInfo ,
@@ -54,6 +63,106 @@ export class TestingHooks {
54
63
) : Unsubscribe {
55
64
return TestingHooksSpiImpl . instance . onExistenceFilterMismatch ( callback ) ;
56
65
}
66
+
67
+ /**
68
+ * Registers a callback to be notified when
69
+ * `enablePersistentCacheIndexAutoCreation()` or
70
+ * `disablePersistentCacheIndexAutoCreation()` is invoked.
71
+ *
72
+ * The relative order in which callbacks are notified is unspecified; do not
73
+ * rely on any particular ordering. If a given callback is registered multiple
74
+ * times then it will be notified multiple times, once per registration.
75
+ *
76
+ * @param callback the callback to invoke when
77
+ * `enablePersistentCacheIndexAutoCreation()` or
78
+ * `disablePersistentCacheIndexAutoCreation()` is invoked.
79
+ *
80
+ * @return a function that, when called, unregisters the given callback; only
81
+ * the first invocation of the returned function does anything; all subsequent
82
+ * invocations do nothing.
83
+ */
84
+ static onPersistentCacheIndexAutoCreationToggle (
85
+ callback : PersistentCacheIndexAutoCreationToggleCallback
86
+ ) : Unsubscribe {
87
+ return TestingHooksSpiImpl . instance . onPersistentCacheIndexAutoCreationToggle (
88
+ callback
89
+ ) ;
90
+ }
91
+
92
+ /**
93
+ * Registers a callback to be notified when
94
+ * `deleteAllPersistentCacheIndexes()` is invoked.
95
+ *
96
+ * The relative order in which callbacks are notified is unspecified; do not
97
+ * rely on any particular ordering. If a given callback is registered multiple
98
+ * times then it will be notified multiple times, once per registration.
99
+ *
100
+ * @param callback the callback to invoke when
101
+ * `deleteAllPersistentCacheIndexes()` is invoked.
102
+ *
103
+ * @return a function that, when called, unregisters the given callback; only
104
+ * the first invocation of the returned function does anything; all subsequent
105
+ * invocations do nothing.
106
+ */
107
+ static onPersistentCacheDeleteAllIndexes (
108
+ callback : PersistentCacheDeleteAllIndexesCallback
109
+ ) : Unsubscribe {
110
+ return TestingHooksSpiImpl . instance . onPersistentCacheDeleteAllIndexes (
111
+ callback
112
+ ) ;
113
+ }
114
+
115
+ /**
116
+ * Determines the type of client-side index that will be used when executing the
117
+ * given query against the local cache.
118
+ *
119
+ * @param query The query whose client-side index type to get; it is typed as
120
+ * `unknown` so that it is usable in the minified, bundled code, but it should
121
+ * be a `Query` object.
122
+ */
123
+ static async getQueryIndexType (
124
+ query : unknown
125
+ ) : Promise < 'full' | 'partial' | 'none' > {
126
+ const query_ = cast < Query > ( query as Query , Query ) ;
127
+ const firestore = cast ( query_ . firestore , Firestore ) ;
128
+ const client = ensureFirestoreConfigured ( firestore ) ;
129
+
130
+ const indexType = await FirestoreClientTestingHooks . getQueryIndexType (
131
+ client ,
132
+ query_ . _query
133
+ ) ;
134
+
135
+ switch ( indexType ) {
136
+ case IndexType . NONE :
137
+ return 'none' ;
138
+ case IndexType . PARTIAL :
139
+ return 'partial' ;
140
+ case IndexType . FULL :
141
+ return 'full' ;
142
+ default :
143
+ throw new Error ( `unrecognized IndexType: ${ indexType } ` ) ;
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Sets the persistent cache index auto-creation settings for the given
149
+ * Firestore instance.
150
+ *
151
+ * @return a Promise that is fulfilled when the settings are successfully
152
+ * applied, or rejected if applying the settings fails.
153
+ */
154
+ static setPersistentCacheIndexAutoCreationSettings (
155
+ indexManager : PersistentCacheIndexManager ,
156
+ settings : {
157
+ indexAutoCreationMinCollectionSize ?: number ;
158
+ relativeIndexReadCostPerDocument ?: number ;
159
+ }
160
+ ) : Promise < void > {
161
+ return PersistentCacheIndexManagerTestingHooks . setIndexAutoCreationSettings (
162
+ indexManager ,
163
+ settings
164
+ ) ;
165
+ }
57
166
}
58
167
59
168
/**
@@ -68,6 +177,39 @@ export type ExistenceFilterMismatchCallback = (
68
177
info : ExistenceFilterMismatchInfo
69
178
) => unknown ;
70
179
180
+ /**
181
+ * The signature of callbacks registered with
182
+ * `TestingHooks.onPersistentCacheIndexAutoCreationToggle()`.
183
+ *
184
+ * The `promise` argument will be fulfilled when the asynchronous work started
185
+ * by the call to `enablePersistentCacheIndexAutoCreation()` or
186
+ * `disablePersistentCacheIndexAutoCreation()` completes successfully, or will
187
+ * be rejected if it fails.
188
+ *
189
+ * The return value, if any, is ignored.
190
+ *
191
+ * @internal
192
+ */
193
+ export type PersistentCacheIndexAutoCreationToggleCallback = (
194
+ promise : Promise < void >
195
+ ) => unknown ;
196
+
197
+ /**
198
+ * The signature of callbacks registered with
199
+ * `TestingHooks.onPersistentCacheDeleteAllIndexes()`.
200
+ *
201
+ * The `promise` argument will be fulfilled when the asynchronous work started
202
+ * by the call to `deleteAllPersistentCacheIndexes()` completes successfully, or
203
+ * will be rejected if it fails.
204
+ *
205
+ * The return value of the callback, if any, is ignored.
206
+ *
207
+ * @internal
208
+ */
209
+ export type PersistentCacheDeleteAllIndexesCallback = (
210
+ promise : Promise < void >
211
+ ) => unknown ;
212
+
71
213
/**
72
214
* The implementation of `TestingHooksSpi`.
73
215
*/
@@ -77,6 +219,14 @@ class TestingHooksSpiImpl implements TestingHooksSpi {
77
219
ExistenceFilterMismatchCallback
78
220
> ( ) ;
79
221
222
+ private readonly persistentCacheIndexAutoCreationToggleCallbacksById =
223
+ new Map < Symbol , PersistentCacheIndexAutoCreationToggleCallback > ( ) ;
224
+
225
+ private readonly persistentCacheDeleteAllIndexesCallbacksById = new Map <
226
+ Symbol ,
227
+ PersistentCacheDeleteAllIndexesCallback
228
+ > ( ) ;
229
+
80
230
private constructor ( ) { }
81
231
82
232
static get instance ( ) : TestingHooksSpiImpl {
@@ -96,11 +246,50 @@ class TestingHooksSpiImpl implements TestingHooksSpi {
96
246
onExistenceFilterMismatch (
97
247
callback : ExistenceFilterMismatchCallback
98
248
) : Unsubscribe {
99
- const id = Symbol ( ) ;
100
- const callbacks = this . existenceFilterMismatchCallbacksById ;
101
- callbacks . set ( id , callback ) ;
102
- return ( ) => callbacks . delete ( id ) ;
249
+ return registerCallback (
250
+ callback ,
251
+ this . existenceFilterMismatchCallbacksById
252
+ ) ;
103
253
}
254
+
255
+ notifyPersistentCacheIndexAutoCreationToggle ( promise : Promise < void > ) : void {
256
+ this . persistentCacheIndexAutoCreationToggleCallbacksById . forEach ( callback =>
257
+ callback ( promise )
258
+ ) ;
259
+ }
260
+
261
+ onPersistentCacheIndexAutoCreationToggle (
262
+ callback : PersistentCacheIndexAutoCreationToggleCallback
263
+ ) : Unsubscribe {
264
+ return registerCallback (
265
+ callback ,
266
+ this . persistentCacheIndexAutoCreationToggleCallbacksById
267
+ ) ;
268
+ }
269
+
270
+ notifyPersistentCacheDeleteAllIndexes ( promise : Promise < void > ) : void {
271
+ this . persistentCacheDeleteAllIndexesCallbacksById . forEach ( callback =>
272
+ callback ( promise )
273
+ ) ;
274
+ }
275
+
276
+ onPersistentCacheDeleteAllIndexes (
277
+ callback : PersistentCacheDeleteAllIndexesCallback
278
+ ) : Unsubscribe {
279
+ return registerCallback (
280
+ callback ,
281
+ this . persistentCacheDeleteAllIndexesCallbacksById
282
+ ) ;
283
+ }
284
+ }
285
+
286
+ function registerCallback < T > (
287
+ callback : T ,
288
+ callbacks : Map < Symbol , T >
289
+ ) : Unsubscribe {
290
+ const id = Symbol ( ) ;
291
+ callbacks . set ( id , callback ) ;
292
+ return ( ) => callbacks . delete ( id ) ;
104
293
}
105
294
106
295
let testingHooksSpiImplInstance : TestingHooksSpiImpl | null = null ;
0 commit comments