Skip to content

Remove Cyclic Dependencies #4160

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 51 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
5599705
Move FieldMask
schmidt-sebastian Dec 3, 2020
b8efc13
Move normalizeTimestamp
schmidt-sebastian Dec 3, 2020
6af13ee
Move TypeOrder
schmidt-sebastian Dec 3, 2020
f315c89
Move query
schmidt-sebastian Dec 3, 2020
bf8f746
Move toDouble, toInteger
schmidt-sebastian Dec 3, 2020
fb16170
Move Serializer
schmidt-sebastian Dec 3, 2020
b682857
Move PersistenceTransaction
schmidt-sebastian Dec 3, 2020
6e32f92
Move ExponentialBackoff
schmidt-sebastian Dec 3, 2020
e2846a9
Move LocalStoreImpl
schmidt-sebastian Dec 3, 2020
8fcc47b
LruGarbageCollector
schmidt-sebastian Dec 3, 2020
3456d83
Move PRIMARY_LEASE_LOST_ERROR_MSG
schmidt-sebastian Dec 3, 2020
0fba935
BundleReader
schmidt-sebastian Dec 3, 2020
3e69755
Move BundleLoader
schmidt-sebastian Dec 3, 2020
a4758a0
IndexedDbBundleCache
schmidt-sebastian Dec 3, 2020
1b1a67d
IndexedDbIndexManager
schmidt-sebastian Dec 3, 2020
11df697
Clean up IndexedDbPersistence
schmidt-sebastian Dec 3, 2020
1159302
Remove unused imports
schmidt-sebastian Dec 3, 2020
e5b9a81
removeMutationBatch/DocumentKey
schmidt-sebastian Dec 3, 2020
b639a29
IndexedDbSchemaConverter
schmidt-sebastian Dec 3, 2020
a503100
IndexedDbBundleCache
schmidt-sebastian Dec 3, 2020
cccabf2
MemoryTargetCache
schmidt-sebastian Dec 3, 2020
0b518f0
Move FieldValue
schmidt-sebastian Dec 3, 2020
cb07140
Move FieldValue implementations
schmidt-sebastian Dec 3, 2020
d9c66ec
Fix Components
schmidt-sebastian Dec 3, 2020
c06dd59
Make Reference unique
schmidt-sebastian Dec 3, 2020
d03efd3
Move ensureClientConfigured/configureClient
schmidt-sebastian Dec 3, 2020
c2f951e
Move SnapshotMetadata
schmidt-sebastian Dec 3, 2020
2c0faa2
Fix lite/.../query.ts
schmidt-sebastian Dec 3, 2020
97b2189
Move UserDataWriter
schmidt-sebastian Dec 3, 2020
ec5ac56
Add onwarn
schmidt-sebastian Dec 3, 2020
fef7824
Cleanup
schmidt-sebastian Dec 3, 2020
7fb2de0
Fix unit tests
schmidt-sebastian Dec 3, 2020
2e666fe
Cleanup imports/exports
schmidt-sebastian Dec 3, 2020
fde4f7f
FieldValue exports
schmidt-sebastian Dec 3, 2020
f324093
Manual cleanup
schmidt-sebastian Dec 4, 2020
a1f8f77
Merge
schmidt-sebastian Dec 4, 2020
e66d39d
Add separate FieldMask
schmidt-sebastian Dec 4, 2020
57ff562
Add TypeOrder comment
schmidt-sebastian Dec 4, 2020
a4001fc
s/numberSerializer/valueSerializer
schmidt-sebastian Dec 4, 2020
618017d
AsyncQueue/AsyncQueueImpl
schmidt-sebastian Dec 5, 2020
a35b1d6
s/_methods/_impl
schmidt-sebastian Dec 5, 2020
a8e3409
getStore
schmidt-sebastian Dec 5, 2020
0294aa3
BATCHID_UNKNOWN
schmidt-sebastian Dec 5, 2020
6482dae
removeMutationBatch
schmidt-sebastian Dec 5, 2020
b15d996
DocumentKey
schmidt-sebastian Dec 5, 2020
045f33f
FirestoreService
schmidt-sebastian Dec 5, 2020
55fb014
Fix bad merge
schmidt-sebastian Dec 5, 2020
d41071c
Feedback
schmidt-sebastian Dec 8, 2020
9f9cfaf
Merge
schmidt-sebastian Dec 8, 2020
25d414c
Fix bundles
schmidt-sebastian Dec 8, 2020
d180871
Undo API Extractor
schmidt-sebastian Dec 8, 2020
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
6 changes: 2 additions & 4 deletions packages/firestore/exp/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ import {
OfflineComponentProvider,
OnlineComponentProvider
} from '../../../src/core/component_provider';
import {
FirebaseFirestore as LiteFirestore,
Settings as LiteSettings
} from '../../../lite/src/api/database';
import { FirebaseFirestore as LiteFirestore } from '../../../lite/src/api/database';
import { DatabaseId } from '../../../src/core/database_info';
import { Code, FirestoreError } from '../../../src/util/error';
import { Deferred } from '../../../src/util/promise';
Expand All @@ -53,6 +50,7 @@ import {
indexedDbStoragePrefix
} from '../../../src/local/indexeddb_persistence';
import { cast } from '../../../src/util/input_validation';
import { Settings as LiteSettings } from '../../../lite/src/api/components';

/** DOMException error code constants. */
const DOM_EXCEPTION_INVALID_STATE = 11;
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/lite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { registerFirestore } from './register';
registerFirestore();

export {
Settings,
FirebaseFirestore,
initializeFirestore,
getFirestore,
Expand Down Expand Up @@ -94,3 +93,4 @@ export { Timestamp } from '../src/api/timestamp';

export { FirestoreErrorCode, FirestoreError } from '../src/util/error';
export { FieldValue } from '../src/api/field_value';
export { Settings } from './src/api/components';
141 changes: 137 additions & 4 deletions packages/firestore/lite/src/api/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,141 @@
import { Datastore, newDatastore } from '../../../src/remote/datastore';
import { newConnection } from '../../../src/platform/connection';
import { newSerializer } from '../../../src/platform/serializer';
import { FirebaseFirestore, makeDatabaseInfo } from './database';
import { logDebug } from '../../../src/util/log';
import { Code, FirestoreError } from '../../../src/util/error';
import { _FirebaseService } from '@firebase/app-types-exp';
import { DatabaseId, DatabaseInfo } from '../../../src/core/database_info';
import {
LRU_COLLECTION_DISABLED,
LRU_DEFAULT_CACHE_SIZE_BYTES
} from '../../../src/local/lru_garbage_collector';
import { LRU_MINIMUM_CACHE_SIZE_BYTES } from '../../../src/local/lru_garbage_collector_impl';
import { validateIsNotUsedTogether } from '../../../src/util/input_validation';
import {
CredentialsProvider,
CredentialsSettings
} from '../../../src/api/credentials';

export const LOG_TAG = 'ComponentProvider';

// settings() defaults:
export const DEFAULT_HOST = 'firestore.googleapis.com';
export const DEFAULT_SSL = true;

/** Undocumented, private additional settings not exposed in our public API. */
export interface PrivateSettings extends Settings {
// Can be a google-auth-library or gapi client.
credentials?: CredentialsSettings;
}

/**
* A concrete type describing all the values that can be applied via a
* user-supplied firestore.Settings object. This is a separate type so that
* defaults can be supplied and the value can be checked for equality.
*/
export class FirestoreSettings {
/** The hostname to connect to. */
readonly host: string;

/** Whether to use SSL when connecting. */
readonly ssl: boolean;

readonly cacheSizeBytes: number;

readonly experimentalForceLongPolling: boolean;

readonly experimentalAutoDetectLongPolling: boolean;

readonly ignoreUndefinedProperties: boolean;

// Can be a google-auth-library or gapi client.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
credentials?: any;

constructor(settings: PrivateSettings) {
if (settings.host === undefined) {
if (settings.ssl !== undefined) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
"Can't provide ssl option if host option is not set"
);
}
this.host = DEFAULT_HOST;
this.ssl = DEFAULT_SSL;
} else {
this.host = settings.host;
this.ssl = settings.ssl ?? DEFAULT_SSL;
}

this.credentials = settings.credentials;
this.ignoreUndefinedProperties = !!settings.ignoreUndefinedProperties;

if (settings.cacheSizeBytes === undefined) {
this.cacheSizeBytes = LRU_DEFAULT_CACHE_SIZE_BYTES;
} else {
if (
settings.cacheSizeBytes !== LRU_COLLECTION_DISABLED &&
settings.cacheSizeBytes < LRU_MINIMUM_CACHE_SIZE_BYTES
) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
`cacheSizeBytes must be at least ${LRU_MINIMUM_CACHE_SIZE_BYTES}`
);
} else {
this.cacheSizeBytes = settings.cacheSizeBytes;
}
}

this.experimentalForceLongPolling = !!settings.experimentalForceLongPolling;
this.experimentalAutoDetectLongPolling = !!settings.experimentalAutoDetectLongPolling;

validateIsNotUsedTogether(
'experimentalForceLongPolling',
settings.experimentalForceLongPolling,
'experimentalAutoDetectLongPolling',
settings.experimentalAutoDetectLongPolling
);
}

isEqual(other: FirestoreSettings): boolean {
return (
this.host === other.host &&
this.ssl === other.ssl &&
this.credentials === other.credentials &&
this.cacheSizeBytes === other.cacheSizeBytes &&
this.experimentalForceLongPolling ===
other.experimentalForceLongPolling &&
this.experimentalAutoDetectLongPolling ===
other.experimentalAutoDetectLongPolling &&
this.ignoreUndefinedProperties === other.ignoreUndefinedProperties
);
}
}

// The components module manages the lifetime of dependencies of the Firestore
// client. Dependencies can be lazily constructed and only one exists per
// Firestore instance.

export interface FirestoreService extends _FirebaseService {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments, please. In particular what's a FirestoreService and how does it differ from a FirebaseFirestore instance?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the following:

/**
 * An interface implemented by FirebaseFirestore that provides
 * compatibility with the usage in this file. 
 * 
 * This interface mainly exists to remove a cyclic dependency.
 */

I will explore doing the same FirebaseFirestore/FirebaseFirestoreImpl pattern in a follow-up PR.

_credentials: CredentialsProvider;
_persistenceKey: string;
_databaseId: DatabaseId;
_terminated: boolean;

_freezeSettings(): FirestoreSettings;
}
/**
* An instance map that ensures only one Datastore exists per Firestore
* instance.
*/
const datastoreInstances = new Map<FirebaseFirestore, Datastore>();
const datastoreInstances = new Map<FirestoreService, Datastore>();

/**
* Returns an initialized and started Datastore for the given Firestore
* instance. Callers must invoke removeComponents() when the Firestore
* instance is terminated.
*/
export function getDatastore(firestore: FirebaseFirestore): Datastore {
export function getDatastore(firestore: FirestoreService): Datastore {
if (firestore._terminated) {
throw new FirestoreError(
Code.FAILED_PRECONDITION,
Expand Down Expand Up @@ -74,11 +183,35 @@ export function getDatastore(firestore: FirebaseFirestore): Datastore {
* Removes all components associated with the provided instance. Must be called
* when the `Firestore` instance is terminated.
*/
export function removeComponents(firestore: FirebaseFirestore): void {
export function removeComponents(firestore: FirestoreService): void {
const datastore = datastoreInstances.get(firestore);
if (datastore) {
logDebug(LOG_TAG, 'Removing Datastore');
datastoreInstances.delete(firestore);
datastore.terminate();
}
}

export interface Settings {
host?: string;
ssl?: boolean;
ignoreUndefinedProperties?: boolean;
cacheSizeBytes?: number;
experimentalForceLongPolling?: boolean;
experimentalAutoDetectLongPolling?: boolean;
}

export function makeDatabaseInfo(
databaseId: DatabaseId,
persistenceKey: string,
settings: FirestoreSettings
): DatabaseInfo {
return new DatabaseInfo(
databaseId,
persistenceKey,
settings.host,
settings.ssl,
settings.experimentalForceLongPolling,
settings.experimentalAutoDetectLongPolling
);
}
143 changes: 11 additions & 132 deletions packages/firestore/lite/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,145 +16,39 @@
*/

import { _getProvider, _removeServiceInstance } from '@firebase/app-exp';
import { _FirebaseService, FirebaseApp } from '@firebase/app-types-exp';
import { FirebaseApp } from '@firebase/app-types-exp';
import { Provider } from '@firebase/component';

import { Code, FirestoreError } from '../../../src/util/error';
import { DatabaseId, DatabaseInfo } from '../../../src/core/database_info';
import { DatabaseId } from '../../../src/core/database_info';
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
import {
CredentialsProvider,
FirebaseCredentialsProvider,
CredentialsSettings,
EmptyCredentialsProvider,
FirebaseCredentialsProvider,
makeCredentialsProvider
} from '../../../src/api/credentials';
import { removeComponents } from './components';
import { LRU_MINIMUM_CACHE_SIZE_BYTES } from '../../../src/local/lru_garbage_collector_impl';
import {
cast,
validateIsNotUsedTogether
} from '../../../src/util/input_validation';
import {
LRU_COLLECTION_DISABLED,
LRU_DEFAULT_CACHE_SIZE_BYTES
} from '../../../src/local/lru_garbage_collector';
FirestoreService,
FirestoreSettings,
PrivateSettings,
removeComponents,
Settings
} from './components';
import { cast } from '../../../src/util/input_validation';

declare module '@firebase/component' {
interface NameServiceMapping {
'firestore/lite': FirebaseFirestore;
}
}

// settings() defaults:
const DEFAULT_HOST = 'firestore.googleapis.com';
const DEFAULT_SSL = true;

export interface Settings {
host?: string;
ssl?: boolean;
ignoreUndefinedProperties?: boolean;
cacheSizeBytes?: number;
experimentalForceLongPolling?: boolean;
experimentalAutoDetectLongPolling?: boolean;
}

/** Undocumented, private additional settings not exposed in our public API. */
interface PrivateSettings extends Settings {
// Can be a google-auth-library or gapi client.
credentials?: CredentialsSettings;
}

/**
* A concrete type describing all the values that can be applied via a
* user-supplied firestore.Settings object. This is a separate type so that
* defaults can be supplied and the value can be checked for equality.
*/
export class FirestoreSettings {
/** The hostname to connect to. */
readonly host: string;

/** Whether to use SSL when connecting. */
readonly ssl: boolean;

readonly cacheSizeBytes: number;

readonly experimentalForceLongPolling: boolean;

readonly experimentalAutoDetectLongPolling: boolean;

readonly ignoreUndefinedProperties: boolean;

// Can be a google-auth-library or gapi client.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
credentials?: any;

constructor(settings: PrivateSettings) {
if (settings.host === undefined) {
if (settings.ssl !== undefined) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
"Can't provide ssl option if host option is not set"
);
}
this.host = DEFAULT_HOST;
this.ssl = DEFAULT_SSL;
} else {
this.host = settings.host;
this.ssl = settings.ssl ?? DEFAULT_SSL;
}

this.credentials = settings.credentials;
this.ignoreUndefinedProperties = !!settings.ignoreUndefinedProperties;

if (settings.cacheSizeBytes === undefined) {
this.cacheSizeBytes = LRU_DEFAULT_CACHE_SIZE_BYTES;
} else {
if (
settings.cacheSizeBytes !== LRU_COLLECTION_DISABLED &&
settings.cacheSizeBytes < LRU_MINIMUM_CACHE_SIZE_BYTES
) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
`cacheSizeBytes must be at least ${LRU_MINIMUM_CACHE_SIZE_BYTES}`
);
} else {
this.cacheSizeBytes = settings.cacheSizeBytes;
}
}

this.experimentalForceLongPolling = !!settings.experimentalForceLongPolling;
this.experimentalAutoDetectLongPolling = !!settings.experimentalAutoDetectLongPolling;

validateIsNotUsedTogether(
'experimentalForceLongPolling',
settings.experimentalForceLongPolling,
'experimentalAutoDetectLongPolling',
settings.experimentalAutoDetectLongPolling
);
}

isEqual(other: FirestoreSettings): boolean {
return (
this.host === other.host &&
this.ssl === other.ssl &&
this.credentials === other.credentials &&
this.cacheSizeBytes === other.cacheSizeBytes &&
this.experimentalForceLongPolling ===
other.experimentalForceLongPolling &&
this.experimentalAutoDetectLongPolling ===
other.experimentalAutoDetectLongPolling &&
this.ignoreUndefinedProperties === other.ignoreUndefinedProperties
);
}
}

/**
* The Cloud Firestore service interface.
*
* Do not call this constructor directly. Instead, use {@link getFirestore}.
*/
export class FirebaseFirestore implements _FirebaseService {
export class FirebaseFirestore implements FirestoreService {
readonly _databaseId: DatabaseId;
readonly _persistenceKey: string = '(lite)';
_credentials: CredentialsProvider;
Expand Down Expand Up @@ -324,18 +218,3 @@ export function terminate(firestore: FirebaseFirestore): Promise<void> {
_removeServiceInstance(firestore.app, 'firestore/lite');
return firestore._delete();
}

export function makeDatabaseInfo(
databaseId: DatabaseId,
persistenceKey: string,
settings: FirestoreSettings
): DatabaseInfo {
return new DatabaseInfo(
databaseId,
persistenceKey,
settings.host,
settings.ssl,
settings.experimentalForceLongPolling,
settings.experimentalAutoDetectLongPolling
);
}
Loading