Skip to content

Differentiate between hardAsserts and debugAsserts #2859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions packages/firestore/src/api/credentials.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2017 Google Inc.
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,7 @@
*/

import { User } from '../auth/user';
import { assert } from '../util/assert';
import { hardAssert, debugAssert } from '../util/assert';
import { Code, FirestoreError } from '../util/error';
import {
FirebaseAuthInternal,
Expand Down Expand Up @@ -116,14 +116,17 @@ export class EmptyCredentialsProvider implements CredentialsProvider {
invalidateToken(): void {}

setChangeListener(changeListener: CredentialChangeListener): void {
assert(!this.changeListener, 'Can only call setChangeListener() once.');
debugAssert(
!this.changeListener,
'Can only call setChangeListener() once.'
);
this.changeListener = changeListener;
// Fire with initial user.
changeListener(User.UNAUTHENTICATED);
}

removeChangeListener(): void {
assert(
debugAssert(
this.changeListener !== null,
'removeChangeListener() when no listener registered'
);
Expand Down Expand Up @@ -190,7 +193,7 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {
}

getToken(): Promise<Token | null> {
assert(
debugAssert(
this.tokenListener != null,
'getToken cannot be called after listener removed.'
);
Expand All @@ -217,7 +220,7 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {
);
} else {
if (tokenData) {
assert(
hardAssert(
typeof tokenData.accessToken === 'string',
'Invalid tokenData returned from getToken():' + tokenData
);
Expand All @@ -234,7 +237,10 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {
}

setChangeListener(changeListener: CredentialChangeListener): void {
assert(!this.changeListener, 'Can only call setChangeListener() once.');
debugAssert(
!this.changeListener,
'Can only call setChangeListener() once.'
);
this.changeListener = changeListener;

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

removeChangeListener(): void {
assert(this.tokenListener != null, 'removeChangeListener() called twice');
assert(
debugAssert(
this.tokenListener != null,
'removeChangeListener() called twice'
);
debugAssert(
this.changeListener !== null,
'removeChangeListener() called when no listener registered'
);
Expand All @@ -263,7 +272,7 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {
// to guarantee to get the actual user.
private getUser(): User {
const currentUid = this.auth && this.auth.getUid();
assert(
hardAssert(
currentUid === null || typeof currentUid === 'string',
'Received invalid UID: ' + currentUid
);
Expand Down Expand Up @@ -342,7 +351,7 @@ export function makeCredentialsProvider(
case 'gapi':
const client = credentials.client as Gapi;
// Make sure this really is a Gapi client.
assert(
hardAssert(
!!(
typeof client === 'object' &&
client !== null &&
Expand Down
19 changes: 11 additions & 8 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { isServerTimestamp } from '../model/server_timestamps';
import { refValue } from '../model/values';
import { PlatformSupport } from '../platform/platform';
import { makeConstructorPrivate } from '../util/api';
import { assert, fail } from '../util/assert';
import { debugAssert, fail } from '../util/assert';
import { AsyncObserver } from '../util/async_observer';
import { AsyncQueue } from '../util/async_queue';
import { Code, FirestoreError } from '../util/error';
Expand Down Expand Up @@ -481,9 +481,12 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
componentProvider: ComponentProvider,
persistenceSettings: PersistenceSettings
): Promise<void> {
assert(!!this._settings.host, 'FirestoreSettings.host is not set');
debugAssert(!!this._settings.host, 'FirestoreSettings.host is not set');

assert(!this._firestoreClient, 'configureClient() called multiple times');
debugAssert(
!this._firestoreClient,
'configureClient() called multiple times'
);

const databaseInfo = this.makeDatabaseInfo();

Expand Down Expand Up @@ -1177,7 +1180,7 @@ export class DocumentReference<T = firestore.DocumentData>
const asyncObserver = new AsyncObserver<ViewSnapshot>({
next: snapshot => {
if (observer.next) {
assert(
debugAssert(
snapshot.docs.size <= 1,
'Too many documents returned on a document query'
);
Expand Down Expand Up @@ -1422,7 +1425,7 @@ export class QueryDocumentSnapshot<T = firestore.DocumentData>
implements firestore.QueryDocumentSnapshot<T> {
data(options?: SnapshotOptions): T {
const data = super.data(options);
assert(
debugAssert(
data !== undefined,
'Document in a QueryDocumentSnapshot should exist'
);
Expand Down Expand Up @@ -2468,11 +2471,11 @@ export function changesFromSnapshot<T>(
snapshot.mutatedKeys.has(change.doc.key),
converter
);
assert(
debugAssert(
change.type === ChangeType.Added,
'Invalid event type for first snapshot'
);
assert(
debugAssert(
!lastDoc || snapshot.query.docComparator(lastDoc, change.doc) < 0,
'Got added events in wrong order'
);
Expand Down Expand Up @@ -2505,7 +2508,7 @@ export function changesFromSnapshot<T>(
let newIndex = -1;
if (change.type !== ChangeType.Added) {
oldIndex = indexTracker.indexOf(change.doc.key);
assert(oldIndex >= 0, 'Index for document not found');
debugAssert(oldIndex >= 0, 'Index for document not found');
indexTracker = indexTracker.delete(change.doc.key);
}
if (change.type !== ChangeType.Removed) {
Expand Down
8 changes: 4 additions & 4 deletions packages/firestore/src/api/user_data_reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
TransformMutation
} from '../model/mutation';
import { FieldPath } from '../model/path';
import { assert, fail } from '../util/assert';
import { debugAssert, fail } from '../util/assert';
import { Code, FirestoreError } from '../util/error';
import { isPlainObject, valueDescription } from '../util/input_validation';
import { Dict, forEach, isEmpty } from '../util/obj';
Expand Down Expand Up @@ -494,8 +494,8 @@ export class UserDataReader {
FieldPath.EMPTY_PATH
);
const parsed = this.parseData(input, context);
assert(parsed != null, 'Parsed data should not be null.');
assert(
debugAssert(parsed != null, 'Parsed data should not be null.');
debugAssert(
context.fieldTransforms.length === 0,
'Field transforms should have been disallowed.'
);
Expand Down Expand Up @@ -633,7 +633,7 @@ export class UserDataReader {
// fieldMask so it gets deleted.
context.fieldMask.push(context.path);
} else if (context.dataSource === UserDataSource.Update) {
assert(
debugAssert(
context.path.length > 0,
'FieldValue.delete() at the top level should have already' +
' been handled.'
Expand Down
4 changes: 2 additions & 2 deletions packages/firestore/src/api/user_data_writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
getLocalWriteTime,
getPreviousValue
} from '../model/server_timestamps';
import { assert, fail } from '../util/assert';
import { fail, hardAssert } from '../util/assert';
import { forEach } from '../util/obj';
import { TypeOrder } from '../model/field_value';
import { ResourcePath } from '../model/path';
Expand Down Expand Up @@ -130,7 +130,7 @@ export class UserDataWriter<T = firestore.DocumentData> {

private convertReference(name: string): DocumentReference<T> {
const resourcePath = ResourcePath.fromString(name);
assert(
hardAssert(
isValidResourceName(resourcePath),
'ReferenceValue is not valid ' + name
);
Expand Down
6 changes: 3 additions & 3 deletions packages/firestore/src/core/component_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { Platform } from '../platform/platform';
import { Datastore } from '../remote/datastore';
import { User } from '../auth/user';
import { PersistenceSettings } from './firestore_client';
import { assert } from '../util/assert';
import { debugAssert } from '../util/assert';
import { GarbageCollectionScheduler, Persistence } from '../local/persistence';
import { Code, FirestoreError } from '../util/error';
import { OnlineStateSource } from './types';
Expand Down Expand Up @@ -93,11 +93,11 @@ export class IndexedDbComponentProvider implements ComponentProvider {
maxConcurrentLimboResolutions: number,
persistenceSettings: PersistenceSettings
): Promise<void> {
assert(
debugAssert(
persistenceSettings.durable,
'IndexedDbComponentProvider can only provide durable persistence'
);
assert(!this.sharedClientState, 'initialize() already called');
debugAssert(!this.sharedClientState, 'initialize() already called');

const persistenceKey = IndexedDbPersistence.buildStoragePrefix(
databaseInfo
Expand Down
14 changes: 7 additions & 7 deletions packages/firestore/src/core/event_manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2017 Google Inc.
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { assert } from '../util/assert';
import { debugAssert } from '../util/assert';
import { EventHandler } from '../util/misc';
import { ObjectMap } from '../util/obj_map';
import { Query } from './query';
Expand Down Expand Up @@ -73,7 +73,7 @@ export class EventManager implements SyncEngineListener {

// Run global snapshot listeners if a consistent snapshot has been emitted.
const raisedEvent = listener.applyOnlineStateChange(this.onlineState);
assert(
debugAssert(
!raisedEvent,
"applyOnlineStateChange() shouldn't raise an event for brand-new listeners."
);
Expand Down Expand Up @@ -226,7 +226,7 @@ export class QueryListener {
* indeed raised.
*/
onViewSnapshot(snap: ViewSnapshot): boolean {
assert(
debugAssert(
snap.docChanges.length > 0 || snap.syncStateChanged,
'We got a new snapshot with no changes?'
);
Expand Down Expand Up @@ -288,7 +288,7 @@ export class QueryListener {
snap: ViewSnapshot,
onlineState: OnlineState
): boolean {
assert(
debugAssert(
!this.raisedInitialEvent,
'Determining whether to raise first event but already had first event'
);
Expand All @@ -304,7 +304,7 @@ export class QueryListener {
// Don't raise the event if we're online, aren't synced yet (checked
// above) and are waiting for a sync.
if (this.options.waitForSyncWhenOnline && maybeOnline) {
assert(
debugAssert(
snap.fromCache,
'Waiting for sync, but snapshot is not from cache'
);
Expand Down Expand Up @@ -337,7 +337,7 @@ export class QueryListener {
}

private raiseInitialEvent(snap: ViewSnapshot): void {
assert(
debugAssert(
!this.raisedInitialEvent,
'Trying to raise initial events for second time'
);
Expand Down
Loading