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,83 @@ 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
+ * Determines the type of client-side index that will be used when executing the
94
+ * given query against the local cache.
95
+ *
96
+ * @param query The query whose client-side index type to get; it is typed as
97
+ * `unknown` so that it is usable in the minified, bundled code, but it should
98
+ * be a `Query` object.
99
+ */
100
+ static async getQueryIndexType (
101
+ query : unknown
102
+ ) : Promise < 'full' | 'partial' | 'none' > {
103
+ const query_ = cast < Query > ( query as Query , Query ) ;
104
+ const firestore = cast ( query_ . firestore , Firestore ) ;
105
+ const client = ensureFirestoreConfigured ( firestore ) ;
106
+
107
+ const indexType = await FirestoreClientTestingHooks . getQueryIndexType (
108
+ client ,
109
+ query_ . _query
110
+ ) ;
111
+
112
+ switch ( indexType ) {
113
+ case IndexType . NONE :
114
+ return 'none' ;
115
+ case IndexType . PARTIAL :
116
+ return 'partial' ;
117
+ case IndexType . FULL :
118
+ return 'full' ;
119
+ default :
120
+ throw new Error ( `unrecognized IndexType: ${ indexType } ` ) ;
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Sets the persistent cache index auto-creation settings for the given
126
+ * Firestore instance.
127
+ *
128
+ * @return a Promise that is fulfilled when the settings are successfully
129
+ * applied, or rejected if applying the settings fails.
130
+ */
131
+ static setPersistentCacheIndexAutoCreationSettings (
132
+ indexManager : PersistentCacheIndexManager ,
133
+ settings : {
134
+ indexAutoCreationMinCollectionSize ?: number ;
135
+ relativeIndexReadCostPerDocument ?: number ;
136
+ }
137
+ ) : Promise < void > {
138
+ return PersistentCacheIndexManagerTestingHooks . setIndexAutoCreationSettings (
139
+ indexManager ,
140
+ settings
141
+ ) ;
142
+ }
57
143
}
58
144
59
145
/**
@@ -68,6 +154,23 @@ export type ExistenceFilterMismatchCallback = (
68
154
info : ExistenceFilterMismatchInfo
69
155
) => unknown ;
70
156
157
+ /**
158
+ * The signature of callbacks registered with
159
+ * `TestingHooks.onPersistentCacheIndexAutoCreationToggle()`.
160
+ *
161
+ * The `promise` argument will be fulfilled when the asynchronous work started
162
+ * by the call to `enablePersistentCacheIndexAutoCreation()` or
163
+ * `disablePersistentCacheIndexAutoCreation()` completes successfully, or will
164
+ * be rejected if it fails.
165
+ *
166
+ * The return value, if any, is ignored.
167
+ *
168
+ * @internal
169
+ */
170
+ export type PersistentCacheIndexAutoCreationToggleCallback = (
171
+ promise : Promise < void >
172
+ ) => unknown ;
173
+
71
174
/**
72
175
* The implementation of `TestingHooksSpi`.
73
176
*/
@@ -77,6 +180,9 @@ class TestingHooksSpiImpl implements TestingHooksSpi {
77
180
ExistenceFilterMismatchCallback
78
181
> ( ) ;
79
182
183
+ private readonly persistentCacheIndexAutoCreationToggleCallbacksById =
184
+ new Map < Symbol , PersistentCacheIndexAutoCreationToggleCallback > ( ) ;
185
+
80
186
private constructor ( ) { }
81
187
82
188
static get instance ( ) : TestingHooksSpiImpl {
@@ -96,11 +202,35 @@ class TestingHooksSpiImpl implements TestingHooksSpi {
96
202
onExistenceFilterMismatch (
97
203
callback : ExistenceFilterMismatchCallback
98
204
) : Unsubscribe {
99
- const id = Symbol ( ) ;
100
- const callbacks = this . existenceFilterMismatchCallbacksById ;
101
- callbacks . set ( id , callback ) ;
102
- return ( ) => callbacks . delete ( id ) ;
205
+ return registerCallback (
206
+ callback ,
207
+ this . existenceFilterMismatchCallbacksById
208
+ ) ;
209
+ }
210
+
211
+ notifyPersistentCacheIndexAutoCreationToggle ( promise : Promise < void > ) : void {
212
+ this . persistentCacheIndexAutoCreationToggleCallbacksById . forEach ( callback =>
213
+ callback ( promise )
214
+ ) ;
103
215
}
216
+
217
+ onPersistentCacheIndexAutoCreationToggle (
218
+ callback : PersistentCacheIndexAutoCreationToggleCallback
219
+ ) : Unsubscribe {
220
+ return registerCallback (
221
+ callback ,
222
+ this . persistentCacheIndexAutoCreationToggleCallbacksById
223
+ ) ;
224
+ }
225
+ }
226
+
227
+ function registerCallback < T > (
228
+ callback : T ,
229
+ callbacks : Map < Symbol , T >
230
+ ) : Unsubscribe {
231
+ const id = Symbol ( ) ;
232
+ callbacks . set ( id , callback ) ;
233
+ return ( ) => callbacks . delete ( id ) ;
104
234
}
105
235
106
236
let testingHooksSpiImplInstance : TestingHooksSpiImpl | null = null ;
0 commit comments