Skip to content

Commit a789d74

Browse files
Differentiate between hardAsserts and debugAsserts (#2859)
1 parent 6a4c00f commit a789d74

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+453
-363
lines changed

packages/firestore/src/api/credentials.ts

+20-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2017 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import { User } from '../auth/user';
19-
import { assert } from '../util/assert';
19+
import { hardAssert, debugAssert } from '../util/assert';
2020
import { Code, FirestoreError } from '../util/error';
2121
import {
2222
FirebaseAuthInternal,
@@ -116,14 +116,17 @@ export class EmptyCredentialsProvider implements CredentialsProvider {
116116
invalidateToken(): void {}
117117

118118
setChangeListener(changeListener: CredentialChangeListener): void {
119-
assert(!this.changeListener, 'Can only call setChangeListener() once.');
119+
debugAssert(
120+
!this.changeListener,
121+
'Can only call setChangeListener() once.'
122+
);
120123
this.changeListener = changeListener;
121124
// Fire with initial user.
122125
changeListener(User.UNAUTHENTICATED);
123126
}
124127

125128
removeChangeListener(): void {
126-
assert(
129+
debugAssert(
127130
this.changeListener !== null,
128131
'removeChangeListener() when no listener registered'
129132
);
@@ -190,7 +193,7 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {
190193
}
191194

192195
getToken(): Promise<Token | null> {
193-
assert(
196+
debugAssert(
194197
this.tokenListener != null,
195198
'getToken cannot be called after listener removed.'
196199
);
@@ -217,7 +220,7 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {
217220
);
218221
} else {
219222
if (tokenData) {
220-
assert(
223+
hardAssert(
221224
typeof tokenData.accessToken === 'string',
222225
'Invalid tokenData returned from getToken():' + tokenData
223226
);
@@ -234,7 +237,10 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {
234237
}
235238

236239
setChangeListener(changeListener: CredentialChangeListener): void {
237-
assert(!this.changeListener, 'Can only call setChangeListener() once.');
240+
debugAssert(
241+
!this.changeListener,
242+
'Can only call setChangeListener() once.'
243+
);
238244
this.changeListener = changeListener;
239245

240246
// Fire the initial event
@@ -244,8 +250,11 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {
244250
}
245251

246252
removeChangeListener(): void {
247-
assert(this.tokenListener != null, 'removeChangeListener() called twice');
248-
assert(
253+
debugAssert(
254+
this.tokenListener != null,
255+
'removeChangeListener() called twice'
256+
);
257+
debugAssert(
249258
this.changeListener !== null,
250259
'removeChangeListener() called when no listener registered'
251260
);
@@ -263,7 +272,7 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {
263272
// to guarantee to get the actual user.
264273
private getUser(): User {
265274
const currentUid = this.auth && this.auth.getUid();
266-
assert(
275+
hardAssert(
267276
currentUid === null || typeof currentUid === 'string',
268277
'Received invalid UID: ' + currentUid
269278
);
@@ -342,7 +351,7 @@ export function makeCredentialsProvider(
342351
case 'gapi':
343352
const client = credentials.client as Gapi;
344353
// Make sure this really is a Gapi client.
345-
assert(
354+
hardAssert(
346355
!!(
347356
typeof client === 'object' &&
348357
client !== null &&

packages/firestore/src/api/database.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import { isServerTimestamp } from '../model/server_timestamps';
4949
import { refValue } from '../model/values';
5050
import { PlatformSupport } from '../platform/platform';
5151
import { makeConstructorPrivate } from '../util/api';
52-
import { assert, fail } from '../util/assert';
52+
import { debugAssert, fail } from '../util/assert';
5353
import { AsyncObserver } from '../util/async_observer';
5454
import { AsyncQueue } from '../util/async_queue';
5555
import { Code, FirestoreError } from '../util/error';
@@ -481,9 +481,12 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
481481
componentProvider: ComponentProvider,
482482
persistenceSettings: PersistenceSettings
483483
): Promise<void> {
484-
assert(!!this._settings.host, 'FirestoreSettings.host is not set');
484+
debugAssert(!!this._settings.host, 'FirestoreSettings.host is not set');
485485

486-
assert(!this._firestoreClient, 'configureClient() called multiple times');
486+
debugAssert(
487+
!this._firestoreClient,
488+
'configureClient() called multiple times'
489+
);
487490

488491
const databaseInfo = this.makeDatabaseInfo();
489492

@@ -1177,7 +1180,7 @@ export class DocumentReference<T = firestore.DocumentData>
11771180
const asyncObserver = new AsyncObserver<ViewSnapshot>({
11781181
next: snapshot => {
11791182
if (observer.next) {
1180-
assert(
1183+
debugAssert(
11811184
snapshot.docs.size <= 1,
11821185
'Too many documents returned on a document query'
11831186
);
@@ -1422,7 +1425,7 @@ export class QueryDocumentSnapshot<T = firestore.DocumentData>
14221425
implements firestore.QueryDocumentSnapshot<T> {
14231426
data(options?: SnapshotOptions): T {
14241427
const data = super.data(options);
1425-
assert(
1428+
debugAssert(
14261429
data !== undefined,
14271430
'Document in a QueryDocumentSnapshot should exist'
14281431
);
@@ -2468,11 +2471,11 @@ export function changesFromSnapshot<T>(
24682471
snapshot.mutatedKeys.has(change.doc.key),
24692472
converter
24702473
);
2471-
assert(
2474+
debugAssert(
24722475
change.type === ChangeType.Added,
24732476
'Invalid event type for first snapshot'
24742477
);
2475-
assert(
2478+
debugAssert(
24762479
!lastDoc || snapshot.query.docComparator(lastDoc, change.doc) < 0,
24772480
'Got added events in wrong order'
24782481
);
@@ -2505,7 +2508,7 @@ export function changesFromSnapshot<T>(
25052508
let newIndex = -1;
25062509
if (change.type !== ChangeType.Added) {
25072510
oldIndex = indexTracker.indexOf(change.doc.key);
2508-
assert(oldIndex >= 0, 'Index for document not found');
2511+
debugAssert(oldIndex >= 0, 'Index for document not found');
25092512
indexTracker = indexTracker.delete(change.doc.key);
25102513
}
25112514
if (change.type !== ChangeType.Removed) {

packages/firestore/src/api/user_data_reader.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
TransformMutation
3333
} from '../model/mutation';
3434
import { FieldPath } from '../model/path';
35-
import { assert, fail } from '../util/assert';
35+
import { debugAssert, fail } from '../util/assert';
3636
import { Code, FirestoreError } from '../util/error';
3737
import { isPlainObject, valueDescription } from '../util/input_validation';
3838
import { Dict, forEach, isEmpty } from '../util/obj';
@@ -494,8 +494,8 @@ export class UserDataReader {
494494
FieldPath.EMPTY_PATH
495495
);
496496
const parsed = this.parseData(input, context);
497-
assert(parsed != null, 'Parsed data should not be null.');
498-
assert(
497+
debugAssert(parsed != null, 'Parsed data should not be null.');
498+
debugAssert(
499499
context.fieldTransforms.length === 0,
500500
'Field transforms should have been disallowed.'
501501
);
@@ -633,7 +633,7 @@ export class UserDataReader {
633633
// fieldMask so it gets deleted.
634634
context.fieldMask.push(context.path);
635635
} else if (context.dataSource === UserDataSource.Update) {
636-
assert(
636+
debugAssert(
637637
context.path.length > 0,
638638
'FieldValue.delete() at the top level should have already' +
639639
' been handled.'

packages/firestore/src/api/user_data_writer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
getLocalWriteTime,
3636
getPreviousValue
3737
} from '../model/server_timestamps';
38-
import { assert, fail } from '../util/assert';
38+
import { fail, hardAssert } from '../util/assert';
3939
import { forEach } from '../util/obj';
4040
import { TypeOrder } from '../model/field_value';
4141
import { ResourcePath } from '../model/path';
@@ -130,7 +130,7 @@ export class UserDataWriter<T = firestore.DocumentData> {
130130

131131
private convertReference(name: string): DocumentReference<T> {
132132
const resourcePath = ResourcePath.fromString(name);
133-
assert(
133+
hardAssert(
134134
isValidResourceName(resourcePath),
135135
'ReferenceValue is not valid ' + name
136136
);

packages/firestore/src/core/component_provider.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { Platform } from '../platform/platform';
3131
import { Datastore } from '../remote/datastore';
3232
import { User } from '../auth/user';
3333
import { PersistenceSettings } from './firestore_client';
34-
import { assert } from '../util/assert';
34+
import { debugAssert } from '../util/assert';
3535
import { GarbageCollectionScheduler, Persistence } from '../local/persistence';
3636
import { Code, FirestoreError } from '../util/error';
3737
import { OnlineStateSource } from './types';
@@ -93,11 +93,11 @@ export class IndexedDbComponentProvider implements ComponentProvider {
9393
maxConcurrentLimboResolutions: number,
9494
persistenceSettings: PersistenceSettings
9595
): Promise<void> {
96-
assert(
96+
debugAssert(
9797
persistenceSettings.durable,
9898
'IndexedDbComponentProvider can only provide durable persistence'
9999
);
100-
assert(!this.sharedClientState, 'initialize() already called');
100+
debugAssert(!this.sharedClientState, 'initialize() already called');
101101

102102
const persistenceKey = IndexedDbPersistence.buildStoragePrefix(
103103
databaseInfo

packages/firestore/src/core/event_manager.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2017 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { assert } from '../util/assert';
18+
import { debugAssert } from '../util/assert';
1919
import { EventHandler } from '../util/misc';
2020
import { ObjectMap } from '../util/obj_map';
2121
import { Query } from './query';
@@ -73,7 +73,7 @@ export class EventManager implements SyncEngineListener {
7373

7474
// Run global snapshot listeners if a consistent snapshot has been emitted.
7575
const raisedEvent = listener.applyOnlineStateChange(this.onlineState);
76-
assert(
76+
debugAssert(
7777
!raisedEvent,
7878
"applyOnlineStateChange() shouldn't raise an event for brand-new listeners."
7979
);
@@ -226,7 +226,7 @@ export class QueryListener {
226226
* indeed raised.
227227
*/
228228
onViewSnapshot(snap: ViewSnapshot): boolean {
229-
assert(
229+
debugAssert(
230230
snap.docChanges.length > 0 || snap.syncStateChanged,
231231
'We got a new snapshot with no changes?'
232232
);
@@ -288,7 +288,7 @@ export class QueryListener {
288288
snap: ViewSnapshot,
289289
onlineState: OnlineState
290290
): boolean {
291-
assert(
291+
debugAssert(
292292
!this.raisedInitialEvent,
293293
'Determining whether to raise first event but already had first event'
294294
);
@@ -304,7 +304,7 @@ export class QueryListener {
304304
// Don't raise the event if we're online, aren't synced yet (checked
305305
// above) and are waiting for a sync.
306306
if (this.options.waitForSyncWhenOnline && maybeOnline) {
307-
assert(
307+
debugAssert(
308308
snap.fromCache,
309309
'Waiting for sync, but snapshot is not from cache'
310310
);
@@ -337,7 +337,7 @@ export class QueryListener {
337337
}
338338

339339
private raiseInitialEvent(snap: ViewSnapshot): void {
340-
assert(
340+
debugAssert(
341341
!this.raisedInitialEvent,
342342
'Trying to raise initial events for second time'
343343
);

0 commit comments

Comments
 (0)