Skip to content

Commit dfa7afb

Browse files
Fix circular dependencies
1 parent 32de25c commit dfa7afb

File tree

3 files changed

+127
-119
lines changed

3 files changed

+127
-119
lines changed

packages/firestore/lite/src/api/components.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
import { Datastore, newDatastore } from '../../../src/remote/datastore';
1919
import { newConnection } from '../../../src/platform/connection';
2020
import { newSerializer } from '../../../src/platform/serializer';
21-
import { FirebaseFirestore } from './database';
21+
import { FirebaseFirestore, makeDatabaseInfo } from './database';
2222
import { logDebug } from '../../../src/util/log';
2323
import { Code, FirestoreError } from '../../../src/util/error';
24-
import { makeDatabaseInfo } from '../../../src/api/database';
2524

2625
export const LOG_TAG = 'ComponentProvider';
2726

packages/firestore/lite/src/api/database.ts

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,128 @@ import { _FirebaseService, FirebaseApp } from '@firebase/app-types-exp';
2020
import { Provider } from '@firebase/component';
2121

2222
import { Code, FirestoreError } from '../../../src/util/error';
23-
import { DatabaseId } from '../../../src/core/database_info';
23+
import { DatabaseId, DatabaseInfo } from '../../../src/core/database_info';
2424
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
2525
import {
2626
CredentialsProvider,
27+
CredentialsSettings,
2728
FirebaseCredentialsProvider
2829
} from '../../../src/api/credentials';
2930
import { removeComponents } from './components';
30-
import { FirestoreSettings } from '../../../src/api/database';
31+
import {
32+
LRU_COLLECTION_DISABLED,
33+
LRU_DEFAULT_CACHE_SIZE_BYTES,
34+
LRU_MINIMUM_CACHE_SIZE_BYTES
35+
} from '../../../src/local/lru_garbage_collector';
36+
import { validateIsNotUsedTogether } from '../../../src/util/input_validation';
3137

3238
declare module '@firebase/component' {
3339
interface NameServiceMapping {
3440
'firestore/lite': FirebaseFirestore;
3541
}
3642
}
3743

44+
// settings() defaults:
45+
const DEFAULT_HOST = 'firestore.googleapis.com';
46+
const DEFAULT_SSL = true;
47+
3848
export interface Settings {
3949
host?: string;
4050
ssl?: boolean;
4151
ignoreUndefinedProperties?: boolean;
52+
cacheSizeBytes?: number;
53+
experimentalForceLongPolling?: boolean;
54+
experimentalAutoDetectLongPolling?: boolean;
55+
}
56+
57+
/** Undocumented, private additional settings not exposed in our public API. */
58+
interface PrivateSettings extends Settings {
59+
// Can be a google-auth-library or gapi client.
60+
credentials?: CredentialsSettings;
61+
}
62+
63+
/**
64+
* A concrete type describing all the values that can be applied via a
65+
* user-supplied firestore.Settings object. This is a separate type so that
66+
* defaults can be supplied and the value can be checked for equality.
67+
*/
68+
export class FirestoreSettings {
69+
/** The hostname to connect to. */
70+
readonly host: string;
71+
72+
/** Whether to use SSL when connecting. */
73+
readonly ssl: boolean;
74+
75+
readonly cacheSizeBytes: number;
76+
77+
readonly experimentalForceLongPolling: boolean;
78+
79+
readonly experimentalAutoDetectLongPolling: boolean;
80+
81+
readonly ignoreUndefinedProperties: boolean;
82+
83+
// Can be a google-auth-library or gapi client.
84+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
85+
credentials?: any;
86+
87+
constructor(settings: PrivateSettings) {
88+
if (settings.host === undefined) {
89+
if (settings.ssl !== undefined) {
90+
throw new FirestoreError(
91+
Code.INVALID_ARGUMENT,
92+
"Can't provide ssl option if host option is not set"
93+
);
94+
}
95+
this.host = DEFAULT_HOST;
96+
this.ssl = DEFAULT_SSL;
97+
} else {
98+
this.host = settings.host;
99+
this.ssl = settings.ssl ?? DEFAULT_SSL;
100+
}
101+
102+
this.credentials = settings.credentials;
103+
this.ignoreUndefinedProperties = !!settings.ignoreUndefinedProperties;
104+
105+
if (settings.cacheSizeBytes === undefined) {
106+
this.cacheSizeBytes = LRU_DEFAULT_CACHE_SIZE_BYTES;
107+
} else {
108+
if (
109+
settings.cacheSizeBytes !== LRU_COLLECTION_DISABLED &&
110+
settings.cacheSizeBytes < LRU_MINIMUM_CACHE_SIZE_BYTES
111+
) {
112+
throw new FirestoreError(
113+
Code.INVALID_ARGUMENT,
114+
`cacheSizeBytes must be at least ${LRU_MINIMUM_CACHE_SIZE_BYTES}`
115+
);
116+
} else {
117+
this.cacheSizeBytes = settings.cacheSizeBytes;
118+
}
119+
}
120+
121+
this.experimentalForceLongPolling = !!settings.experimentalForceLongPolling;
122+
this.experimentalAutoDetectLongPolling = !!settings.experimentalAutoDetectLongPolling;
123+
124+
validateIsNotUsedTogether(
125+
'experimentalForceLongPolling',
126+
settings.experimentalForceLongPolling,
127+
'experimentalAutoDetectLongPolling',
128+
settings.experimentalAutoDetectLongPolling
129+
);
130+
}
131+
132+
isEqual(other: FirestoreSettings): boolean {
133+
return (
134+
this.host === other.host &&
135+
this.ssl === other.ssl &&
136+
this.credentials === other.credentials &&
137+
this.cacheSizeBytes === other.cacheSizeBytes &&
138+
this.experimentalForceLongPolling ===
139+
other.experimentalForceLongPolling &&
140+
this.experimentalAutoDetectLongPolling ===
141+
other.experimentalAutoDetectLongPolling &&
142+
this.ignoreUndefinedProperties === other.ignoreUndefinedProperties
143+
);
144+
}
42145
}
43146

44147
/**
@@ -194,3 +297,18 @@ export function terminate(firestore: FirebaseFirestore): Promise<void> {
194297
_removeServiceInstance(firestore.app, 'firestore/lite');
195298
return firestore._delete();
196299
}
300+
301+
export function makeDatabaseInfo(
302+
databaseId: DatabaseId,
303+
persistenceKey: string,
304+
settings: FirestoreSettings
305+
): DatabaseInfo {
306+
return new DatabaseInfo(
307+
databaseId,
308+
persistenceKey,
309+
settings.host,
310+
settings.ssl,
311+
settings.experimentalForceLongPolling,
312+
settings.experimentalAutoDetectLongPolling
313+
);
314+
}

packages/firestore/src/api/database.ts

Lines changed: 6 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import {
4646
import { FirebaseApp } from '@firebase/app-types';
4747
import { _FirebaseApp, FirebaseService } from '@firebase/app-types/private';
4848
import { Blob } from './blob';
49-
import { DatabaseId, DatabaseInfo } from '../core/database_info';
49+
import { DatabaseId } from '../core/database_info';
5050
import { ListenOptions } from '../core/event_manager';
5151
import {
5252
FirestoreClient,
@@ -111,7 +111,6 @@ import { AutoId } from '../util/misc';
111111
import { FieldPath as ExternalFieldPath } from './field_path';
112112
import {
113113
CredentialsProvider,
114-
CredentialsSettings,
115114
EmptyCredentialsProvider,
116115
FirebaseCredentialsProvider,
117116
makeCredentialsProvider
@@ -142,15 +141,12 @@ import {
142141
enableIndexedDbPersistence,
143142
enableMultiTabIndexedDbPersistence
144143
} from '../../exp/src/api/database';
144+
import { LRU_COLLECTION_DISABLED } from '../local/lru_garbage_collector';
145145
import {
146-
LRU_COLLECTION_DISABLED,
147-
LRU_DEFAULT_CACHE_SIZE_BYTES,
148-
LRU_MINIMUM_CACHE_SIZE_BYTES
149-
} from '../local/lru_garbage_collector';
150-
151-
// settings() defaults:
152-
const DEFAULT_HOST = 'firestore.googleapis.com';
153-
const DEFAULT_SSL = true;
146+
FirestoreSettings,
147+
makeDatabaseInfo
148+
} from '../../lite/src/api/database';
149+
import { DEFAULT_HOST } from '../../lite/src/api/components';
154150

155151
/**
156152
* Constant used to indicate the LRU garbage collection should be disabled.
@@ -159,12 +155,6 @@ const DEFAULT_SSL = true;
159155
*/
160156
export const CACHE_SIZE_UNLIMITED = LRU_COLLECTION_DISABLED;
161157

162-
/** Undocumented, private additional settings not exposed in our public API. */
163-
interface PrivateSettings extends PublicSettings {
164-
// Can be a google-auth-library or gapi client.
165-
credentials?: CredentialsSettings;
166-
}
167-
168158
/**
169159
* Options that can be provided in the Firestore constructor when not using
170160
* Firebase (aka standalone mode).
@@ -174,90 +164,6 @@ export interface FirestoreDatabase {
174164
database?: string;
175165
}
176166

177-
/**
178-
* A concrete type describing all the values that can be applied via a
179-
* user-supplied firestore.Settings object. This is a separate type so that
180-
* defaults can be supplied and the value can be checked for equality.
181-
*/
182-
export class FirestoreSettings {
183-
/** The hostname to connect to. */
184-
readonly host: string;
185-
186-
/** Whether to use SSL when connecting. */
187-
readonly ssl: boolean;
188-
189-
readonly cacheSizeBytes: number;
190-
191-
readonly experimentalForceLongPolling: boolean;
192-
193-
readonly experimentalAutoDetectLongPolling: boolean;
194-
195-
readonly ignoreUndefinedProperties: boolean;
196-
197-
// Can be a google-auth-library or gapi client.
198-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
199-
credentials?: any;
200-
201-
constructor(settings: PrivateSettings) {
202-
if (settings.host === undefined) {
203-
if (settings.ssl !== undefined) {
204-
throw new FirestoreError(
205-
Code.INVALID_ARGUMENT,
206-
"Can't provide ssl option if host option is not set"
207-
);
208-
}
209-
this.host = DEFAULT_HOST;
210-
this.ssl = DEFAULT_SSL;
211-
} else {
212-
this.host = settings.host;
213-
this.ssl = settings.ssl ?? DEFAULT_SSL;
214-
}
215-
216-
this.credentials = settings.credentials;
217-
this.ignoreUndefinedProperties = !!settings.ignoreUndefinedProperties;
218-
219-
if (settings.cacheSizeBytes === undefined) {
220-
this.cacheSizeBytes = LRU_DEFAULT_CACHE_SIZE_BYTES;
221-
} else {
222-
if (
223-
settings.cacheSizeBytes !== LRU_COLLECTION_DISABLED &&
224-
settings.cacheSizeBytes < LRU_MINIMUM_CACHE_SIZE_BYTES
225-
) {
226-
throw new FirestoreError(
227-
Code.INVALID_ARGUMENT,
228-
`cacheSizeBytes must be at least ${LRU_MINIMUM_CACHE_SIZE_BYTES}`
229-
);
230-
} else {
231-
this.cacheSizeBytes = settings.cacheSizeBytes;
232-
}
233-
}
234-
235-
this.experimentalForceLongPolling = !!settings.experimentalForceLongPolling;
236-
this.experimentalAutoDetectLongPolling = !!settings.experimentalAutoDetectLongPolling;
237-
238-
validateIsNotUsedTogether(
239-
'experimentalForceLongPolling',
240-
settings.experimentalForceLongPolling,
241-
'experimentalAutoDetectLongPolling',
242-
settings.experimentalAutoDetectLongPolling
243-
);
244-
}
245-
246-
isEqual(other: FirestoreSettings): boolean {
247-
return (
248-
this.host === other.host &&
249-
this.ssl === other.ssl &&
250-
this.credentials === other.credentials &&
251-
this.cacheSizeBytes === other.cacheSizeBytes &&
252-
this.experimentalForceLongPolling ===
253-
other.experimentalForceLongPolling &&
254-
this.experimentalAutoDetectLongPolling ===
255-
other.experimentalAutoDetectLongPolling &&
256-
this.ignoreUndefinedProperties === other.ignoreUndefinedProperties
257-
);
258-
}
259-
}
260-
261167
// TODO(firestore-compat): This interface exposes internal APIs that the Compat
262168
// layer implements to interact with the firestore-exp SDK. We can remove this
263169
// class once we have an actual compat class for FirebaseFirestore.
@@ -667,21 +573,6 @@ export function configureFirestore(firestore: FirestoreCompat): void {
667573
);
668574
}
669575

670-
export function makeDatabaseInfo(
671-
databaseId: DatabaseId,
672-
persistenceKey: string,
673-
settings: FirestoreSettings
674-
): DatabaseInfo {
675-
return new DatabaseInfo(
676-
databaseId,
677-
persistenceKey,
678-
settings.host,
679-
settings.ssl,
680-
settings.experimentalForceLongPolling,
681-
settings.experimentalAutoDetectLongPolling
682-
);
683-
}
684-
685576
export function setLogLevel(level: PublicLogLevel): void {
686577
setClientLogLevel(level);
687578
}

0 commit comments

Comments
 (0)