Skip to content

Commit ec1f784

Browse files
committed
change ListenSource from Enum to union type
1 parent d0a0f9c commit ec1f784

File tree

8 files changed

+68
-80
lines changed

8 files changed

+68
-80
lines changed

common/api-review/firestore.api.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,7 @@ export function limit(limit: number): QueryLimitConstraint;
351351
export function limitToLast(limit: number): QueryLimitConstraint;
352352

353353
// @public
354-
export const enum ListenSource {
355-
Cache = 1,
356-
Default = 0
357-
}
354+
export type ListenSource = 'default' | 'cache';
358355

359356
// @public
360357
export function loadBundle(firestore: Firestore, bundleData: ReadableStream<Uint8Array> | ArrayBuffer | string): LoadBundleTask;

docs-devsite/firestore_.md

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,6 @@ https://github.com/firebase/firebase-js-sdk
157157
| [Transaction](./firestore_.transaction.md#transaction_class) | A reference to a transaction.<!-- -->The <code>Transaction</code> object passed to a transaction's <code>updateFunction</code> provides the methods to read and write data within the transaction context. See [runTransaction()](./firestore_.md#runtransaction_6f03ec4)<!-- -->. |
158158
| [WriteBatch](./firestore_.writebatch.md#writebatch_class) | A write batch, used to perform multiple writes as a single atomic unit.<!-- -->A <code>WriteBatch</code> object can be acquired by calling [writeBatch()](./firestore_.md#writebatch_231a8e0)<!-- -->. It provides methods for adding writes to the write batch. None of the writes will be committed (or visible locally) until [WriteBatch.commit()](./firestore_.writebatch.md#writebatchcommit) is called. |
159159

160-
## Enumerations
161-
162-
| Enumeration | Description |
163-
| --- | --- |
164-
| [ListenSource](./firestore_.md#listensource) | Describe the source a query listens to. |
165-
166160
## Interfaces
167161

168162
| Interface | Description |
@@ -210,6 +204,7 @@ https://github.com/firebase/firebase-js-sdk
210204
| [DocumentChangeType](./firestore_.md#documentchangetype) | The type of a <code>DocumentChange</code> may be 'added', 'removed', or 'modified'. |
211205
| [FirestoreErrorCode](./firestore_.md#firestoreerrorcode) | The set of Firestore status codes. The codes are the same at the ones exposed by gRPC here: https://github.com/grpc/grpc/blob/master/doc/statuscodes.md<!-- -->Possible values: - 'cancelled': The operation was cancelled (typically by the caller). - 'unknown': Unknown error or an error from a different error domain. - 'invalid-argument': Client specified an invalid argument. Note that this differs from 'failed-precondition'. 'invalid-argument' indicates arguments that are problematic regardless of the state of the system (e.g. an invalid field name). - 'deadline-exceeded': Deadline expired before operation could complete. For operations that change the state of the system, this error may be returned even if the operation has completed successfully. For example, a successful response from a server could have been delayed long enough for the deadline to expire. - 'not-found': Some requested document was not found. - 'already-exists': Some document that we attempted to create already exists. - 'permission-denied': The caller does not have permission to execute the specified operation. - 'resource-exhausted': Some resource has been exhausted, perhaps a per-user quota, or perhaps the entire file system is out of space. - 'failed-precondition': Operation was rejected because the system is not in a state required for the operation's execution. - 'aborted': The operation was aborted, typically due to a concurrency issue like transaction aborts, etc. - 'out-of-range': Operation was attempted past the valid range. - 'unimplemented': Operation is not implemented or not supported/enabled. - 'internal': Internal errors. Means some invariants expected by underlying system has been broken. If you see one of these errors, something is very broken. - 'unavailable': The service is currently unavailable. This is most likely a transient condition and may be corrected by retrying with a backoff. - 'data-loss': Unrecoverable data loss or corruption. - 'unauthenticated': The request does not have valid authentication credentials for the operation. |
212206
| [FirestoreLocalCache](./firestore_.md#firestorelocalcache) | Union type from all supported SDK cache layer. |
207+
| [ListenSource](./firestore_.md#listensource) | Describe the source a query listens to.<code>default</code>: listens to both cache and server changes <code>cache</code>: listens to changes in cache only |
213208
| [MemoryGarbageCollector](./firestore_.md#memorygarbagecollector) | Union type from all support gabage collectors for memory local cache. |
214209
| [NestedUpdateFields](./firestore_.md#nestedupdatefields) | For each field (e.g. 'bar'), find all nested keys (e.g. {<!-- -->'bar.baz': T1, 'bar.qux': T2<!-- -->}<!-- -->). Intersect them together to make a single map containing all possible keys that are all marked as optional |
215210
| [OrderByDirection](./firestore_.md#orderbydirection) | The direction of a [orderBy()](./firestore_.md#orderby_006d61f) clause is specified as 'desc' or 'asc' (descending or ascending). |
@@ -2557,6 +2552,18 @@ Union type from all supported SDK cache layer.
25572552
export declare type FirestoreLocalCache = MemoryLocalCache | PersistentLocalCache;
25582553
```
25592554

2555+
## ListenSource
2556+
2557+
Describe the source a query listens to.
2558+
2559+
`default`<!-- -->: listens to both cache and server changes `cache`<!-- -->: listens to changes in cache only
2560+
2561+
<b>Signature:</b>
2562+
2563+
```typescript
2564+
export declare type ListenSource = 'default' | 'cache';
2565+
```
2566+
25602567
## MemoryGarbageCollector
25612568

25622569
Union type from all support gabage collectors for memory local cache.
@@ -2722,21 +2729,3 @@ export declare type WithFieldValue<T> = T | (T extends Primitive ? T : T extends
27222729
[K in keyof T]: WithFieldValue<T[K]> | FieldValue;
27232730
} : never);
27242731
```
2725-
2726-
## ListenSource
2727-
2728-
Describe the source a query listens to.
2729-
2730-
<b>Signature:</b>
2731-
2732-
```typescript
2733-
export declare const enum ListenSource
2734-
```
2735-
2736-
## Enumeration Members
2737-
2738-
| Member | Value | Description |
2739-
| --- | --- | --- |
2740-
| Cache | <code>1</code> | Listen to changes in cache only |
2741-
| Default | <code>0</code> | Listen to both cache and server changes |
2742-

docs-devsite/firestore_.snapshotlistenoptions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export declare interface SnapshotListenOptions
2323
| Property | Type | Description |
2424
| --- | --- | --- |
2525
| [includeMetadataChanges](./firestore_.snapshotlistenoptions.md#snapshotlistenoptionsincludemetadatachanges) | boolean | Include a change even if only the metadata of the query or of a document changed. Default is false. |
26-
| [source](./firestore_.snapshotlistenoptions.md#snapshotlistenoptionssource) | [ListenSource](./firestore_.md#listensource) | Set the source the query listens to. Default to ListenSource.Default, which listens to both cache and server. |
26+
| [source](./firestore_.snapshotlistenoptions.md#snapshotlistenoptionssource) | [ListenSource](./firestore_.md#listensource) | Set the source the query listens to. Default to "default", which listens to both cache and server. |
2727

2828
## SnapshotListenOptions.includeMetadataChanges
2929

@@ -37,7 +37,7 @@ readonly includeMetadataChanges?: boolean;
3737

3838
## SnapshotListenOptions.source
3939

40-
Set the source the query listens to. Default to ListenSource.Default, which listens to both cache and server.
40+
Set the source the query listens to. Default to "default", which listens to both cache and server.
4141

4242
<b>Signature:</b>
4343

packages/firestore/src/api/reference_impl.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
NextFn,
2525
PartialObserver
2626
} from '../api/observer';
27+
import { ListenerDataSource } from '../core/event_manager';
2728
import {
2829
firestoreClientAddSnapshotsInSyncListener,
2930
firestoreClientGetDocumentFromLocalCache,
@@ -80,20 +81,18 @@ export interface SnapshotListenOptions {
8081
readonly includeMetadataChanges?: boolean;
8182

8283
/**
83-
* Set the source the query listens to. Default to ListenSource.Default, which
84+
* Set the source the query listens to. Default to "default", which
8485
* listens to both cache and server.
8586
*/
8687
readonly source?: ListenSource;
8788
}
8889

89-
/** Describe the source a query listens to. */
90-
export const enum ListenSource {
91-
/** Listen to both cache and server changes */
92-
Default,
93-
94-
/** Listen to changes in cache only */
95-
Cache
96-
}
90+
/** Describe the source a query listens to.
91+
*
92+
* `default`: listens to both cache and server changes
93+
* `cache`: listens to changes in cache only
94+
*/
95+
export type ListenSource = 'default' | 'cache';
9796

9897
/**
9998
* Reads the document referred to by this `DocumentReference`.
@@ -684,7 +683,7 @@ export function onSnapshot<AppModelType, DbModelType extends DocumentData>(
684683

685684
let options: SnapshotListenOptions = {
686685
includeMetadataChanges: false,
687-
source: ListenSource.Default
686+
source: 'default'
688687
};
689688
let currArg = 0;
690689
if (typeof args[currArg] === 'object' && !isPartialObserver(args[currArg])) {
@@ -694,7 +693,7 @@ export function onSnapshot<AppModelType, DbModelType extends DocumentData>(
694693

695694
const internalOptions = {
696695
includeMetadataChanges: options.includeMetadataChanges,
697-
source: options.source
696+
source: options.source as ListenerDataSource
698697
};
699698

700699
if (isPartialObserver(args[currArg])) {

packages/firestore/src/core/event_manager.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { ListenSource } from '../api/reference_impl';
1918
import { debugAssert, debugCast } from '../util/assert';
2019
import { wrapInUserErrorIfRecoverable } from '../util/async_queue';
2120
import { FirestoreError } from '../util/error';
@@ -342,6 +341,14 @@ function raiseSnapshotsInSyncEvent(eventManagerImpl: EventManagerImpl): void {
342341
});
343342
}
344343

344+
export enum ListenerDataSource {
345+
/** Listen to both cache and server changes */
346+
Default = 'default',
347+
348+
/** Listen to changes in cache only */
349+
Cache = 'cache'
350+
}
351+
345352
export interface ListenOptions {
346353
/** Raise events even when only the metadata changes */
347354
readonly includeMetadataChanges?: boolean;
@@ -353,7 +360,7 @@ export interface ListenOptions {
353360
readonly waitForSyncWhenOnline?: boolean;
354361

355362
/** Set the source events raised from. */
356-
readonly source?: ListenSource;
363+
readonly source?: ListenerDataSource;
357364
}
358365

359366
/**
@@ -528,6 +535,6 @@ export class QueryListener {
528535
}
529536

530537
listensToRemoteStore(): boolean {
531-
return this.options.source !== ListenSource.Cache;
538+
return this.options.source !== ListenerDataSource.Cache;
532539
}
533540
}

packages/firestore/test/integration/api/snasphot_listener_source.test.ts

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import { expect } from 'chai';
1919

20+
import { ListenerDataSource as Source } from '../../../src/core/event_manager';
2021
import { EventsAccumulator } from '../util/events_accumulator';
2122
import {
2223
addDoc,
@@ -28,7 +29,6 @@ import {
2829
getDocs,
2930
limit,
3031
limitToLast,
31-
ListenSource,
3232
onSnapshot,
3333
orderBy,
3434
query,
@@ -59,7 +59,7 @@ apiDescribe('Snapshot Listener source options ', persistence => {
5959
const storeEvent = new EventsAccumulator<QuerySnapshot>();
6060
const unsubscribe = onSnapshot(
6161
coll,
62-
{ source: ListenSource.Cache },
62+
{ source: Source.Cache },
6363
storeEvent.storeEvent
6464
);
6565

@@ -83,7 +83,7 @@ apiDescribe('Snapshot Listener source options ', persistence => {
8383
const storeEvent = new EventsAccumulator<DocumentSnapshot>();
8484
const unsubscribe = onSnapshot(
8585
docRef,
86-
{ source: ListenSource.Cache },
86+
{ source: Source.Cache },
8787
storeEvent.storeEvent
8888
);
8989

@@ -107,7 +107,7 @@ apiDescribe('Snapshot Listener source options ', persistence => {
107107
const storeEvent = new EventsAccumulator<QuerySnapshot>();
108108
const unsubscribe = onSnapshot(
109109
coll,
110-
{ includeMetadataChanges: true, source: ListenSource.Cache },
110+
{ includeMetadataChanges: true, source: Source.Cache },
111111
storeEvent.storeEvent
112112
);
113113

@@ -139,13 +139,13 @@ apiDescribe('Snapshot Listener source options ', persistence => {
139139
const storeEvent = new EventsAccumulator<QuerySnapshot>();
140140
const unsubscribe1 = onSnapshot(
141141
testQuery,
142-
{ source: ListenSource.Cache },
142+
{ source: Source.Cache },
143143
storeEvent.storeEvent
144144
);
145145

146146
const unsubscribe2 = onSnapshot(
147147
testQuery,
148-
{ source: ListenSource.Cache },
148+
{ source: Source.Cache },
149149
storeEvent.storeEvent
150150
);
151151

@@ -207,15 +207,15 @@ apiDescribe('Snapshot Listener source options ', persistence => {
207207
const storeLimitEvent = new EventsAccumulator<QuerySnapshot>();
208208
let limitUnlisten = onSnapshot(
209209
query(coll, orderBy('sort', 'asc'), limit(2)),
210-
{ source: ListenSource.Cache },
210+
{ source: Source.Cache },
211211
storeLimitEvent.storeEvent
212212
);
213213

214214
// Setup mirroring `limitToLast` query
215215
const storeLimitToLastEvent = new EventsAccumulator<QuerySnapshot>();
216216
let limitToLastUnlisten = onSnapshot(
217217
query(coll, orderBy('sort', 'desc'), limitToLast(2)),
218-
{ source: ListenSource.Cache },
218+
{ source: Source.Cache },
219219
storeLimitToLastEvent.storeEvent
220220
);
221221

@@ -235,7 +235,7 @@ apiDescribe('Snapshot Listener source options ', persistence => {
235235
limitUnlisten();
236236
limitUnlisten = onSnapshot(
237237
query(coll, orderBy('sort', 'asc'), limit(2)),
238-
{ source: ListenSource.Cache },
238+
{ source: Source.Cache },
239239
storeLimitEvent.storeEvent
240240
);
241241
snapshot = await storeLimitEvent.awaitEvent();
@@ -269,7 +269,7 @@ apiDescribe('Snapshot Listener source options ', persistence => {
269269
await updateDoc(doc(coll, 'a'), { k: 'a', sort: -2 });
270270
limitToLastUnlisten = onSnapshot(
271271
query(coll, orderBy('sort', 'desc'), limitToLast(2)),
272-
{ source: ListenSource.Cache },
272+
{ source: Source.Cache },
273273
storeLimitToLastEvent.storeEvent
274274
);
275275

@@ -320,7 +320,7 @@ apiDescribe('Snapshot Listener source options ', persistence => {
320320
const storeCacheEvent = new EventsAccumulator<QuerySnapshot>();
321321
const cacheUnlisten = onSnapshot(
322322
testQuery,
323-
{ source: ListenSource.Cache },
323+
{ source: Source.Cache },
324324
storeCacheEvent.storeEvent
325325
);
326326
snapshot = await storeCacheEvent.awaitEvent();
@@ -352,7 +352,7 @@ apiDescribe('Snapshot Listener source options ', persistence => {
352352

353353
const cacheUnlisten = onSnapshot(
354354
testQuery,
355-
{ source: ListenSource.Cache },
355+
{ source: Source.Cache },
356356
storeCacheEvent.storeEvent
357357
);
358358
let snapshot = await storeCacheEvent.awaitEvent();
@@ -401,7 +401,7 @@ apiDescribe('Snapshot Listener source options ', persistence => {
401401
const storeEvent = new EventsAccumulator<QuerySnapshot>();
402402
const unsubscribe = onSnapshot(
403403
testQuery,
404-
{ includeMetadataChanges: true, source: ListenSource.Cache },
404+
{ includeMetadataChanges: true, source: Source.Cache },
405405
storeEvent.storeEvent
406406
);
407407

@@ -443,7 +443,7 @@ apiDescribe('Snapshot Listener source options ', persistence => {
443443
const storeCacheEvent = new EventsAccumulator<QuerySnapshot>();
444444
const cacheUnlisten = onSnapshot(
445445
testQuery,
446-
{ includeMetadataChanges: true, source: ListenSource.Cache },
446+
{ includeMetadataChanges: true, source: Source.Cache },
447447
storeCacheEvent.storeEvent
448448
);
449449
let snapshot = await storeCacheEvent.awaitEvent();
@@ -523,7 +523,7 @@ apiDescribe('Snapshot Listener source options ', persistence => {
523523
const storeCacheEvent = new EventsAccumulator<QuerySnapshot>();
524524
const cacheUnlisten = onSnapshot(
525525
testQuery,
526-
{ source: ListenSource.Cache },
526+
{ source: Source.Cache },
527527
storeCacheEvent.storeEvent
528528
);
529529
await storeCacheEvent.awaitEvent();
@@ -568,7 +568,7 @@ apiDescribe('Snapshot Listener source options ', persistence => {
568568
const storeCacheEvent = new EventsAccumulator<QuerySnapshot>();
569569
const cacheUnlisten = onSnapshot(
570570
testQuery,
571-
{ source: ListenSource.Cache },
571+
{ source: Source.Cache },
572572
storeCacheEvent.storeEvent
573573
);
574574
await storeCacheEvent.awaitEvent();
@@ -617,7 +617,7 @@ apiDescribe('Snapshot Listener source options ', persistence => {
617617
const storeCacheEvent = new EventsAccumulator<QuerySnapshot>();
618618
let cacheUnlisten = onSnapshot(
619619
testQuery,
620-
{ source: ListenSource.Cache },
620+
{ source: Source.Cache },
621621
storeCacheEvent.storeEvent
622622
);
623623
snapshot = await storeCacheEvent.awaitEvent();
@@ -651,7 +651,7 @@ apiDescribe('Snapshot Listener source options ', persistence => {
651651

652652
cacheUnlisten = onSnapshot(
653653
testQuery,
654-
{ source: ListenSource.Cache },
654+
{ source: Source.Cache },
655655
storeCacheEvent.storeEvent
656656
);
657657

@@ -679,7 +679,7 @@ apiDescribe('Snapshot Listener source options ', persistence => {
679679
const storeEvent = new EventsAccumulator<QuerySnapshot>();
680680
const unsubscribe = onSnapshot(
681681
testQuery,
682-
{ source: ListenSource.Cache },
682+
{ source: Source.Cache },
683683
storeEvent.storeEvent
684684
);
685685

@@ -695,11 +695,7 @@ apiDescribe('Snapshot Listener source options ', persistence => {
695695
expect(toDataArray(snapshot)).to.deep.equal([]); // Precondition check.
696696

697697
const storeEvent = new EventsAccumulator<QuerySnapshot>();
698-
onSnapshot(
699-
coll,
700-
{ source: ListenSource.Cache },
701-
storeEvent.storeEvent
702-
);
698+
onSnapshot(coll, { source: Source.Cache }, storeEvent.storeEvent);
703699
snapshot = await storeEvent.awaitEvent();
704700
expect(snapshot.metadata.fromCache).to.be.true;
705701
expect(toDataArray(snapshot)).to.deep.equal([]);
@@ -711,7 +707,7 @@ apiDescribe('Snapshot Listener source options ', persistence => {
711707
const accumulator = new EventsAccumulator<QuerySnapshot>();
712708
const unsubscribe = onSnapshot(
713709
coll,
714-
{ source: ListenSource.Cache },
710+
{ source: Source.Cache },
715711
accumulator.storeEvent
716712
);
717713

@@ -756,7 +752,7 @@ apiDescribe('Snapshot Listener source options ', persistence => {
756752
const storeCacheEvent = new EventsAccumulator<QuerySnapshot>();
757753
const cacheUnlisten = onSnapshot(
758754
testQuery,
759-
{ source: ListenSource.Cache },
755+
{ source: Source.Cache },
760756
storeCacheEvent.storeEvent
761757
);
762758
snapshot = await storeCacheEvent.awaitEvent();

0 commit comments

Comments
 (0)