Skip to content

Commit 2d8ec89

Browse files
Merge Components Framework
2 parents 64870bf + 45ea646 commit 2d8ec89

File tree

12 files changed

+2682
-11682
lines changed

12 files changed

+2682
-11682
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { Firestore } from './database';
19+
import { DatabaseInfo } from '../../../src/core/database_info';
20+
import {
21+
FirestoreClient,
22+
PersistenceSettings
23+
} from '../../../src/core/firestore_client';
24+
import { Code, FirestoreError } from '../../../src/util/error';
25+
import {
26+
MemoryOfflineComponentProvider,
27+
OfflineComponentProvider,
28+
OnlineComponentProvider
29+
} from '../../../src/core/component_provider';
30+
import { DEFAULT_HOST, DEFAULT_SSL } from '../../../lite/src/api/components';
31+
32+
// The components module manages the lifetime of dependencies of the Firestore
33+
// client. Dependencies can be lazily constructed and only one exists per
34+
// Firestore instance.
35+
36+
/**
37+
* An instance map that ensures only one FirestoreClient exists per Firestore
38+
* instance.
39+
*/
40+
const firestoreClientInstances = new Map<Firestore, Promise<FirestoreClient>>();
41+
42+
/**
43+
* Returns the initialized and started FirestoreClient for the given Firestore
44+
* instance. If none exists, creates a new FirestoreClient with memory
45+
* persistence. Callers must invoke removeFirestoreClient() when the Firestore
46+
* instance is terminated.
47+
*/
48+
export function getFirestoreClient(
49+
firestore: Firestore
50+
): Promise<FirestoreClient> {
51+
if (firestore._terminated) {
52+
throw new FirestoreError(
53+
Code.FAILED_PRECONDITION,
54+
'The client has already been terminated.'
55+
);
56+
}
57+
if (!firestoreClientInstances.has(firestore)) {
58+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
59+
initializeFirestoreClient(
60+
firestore,
61+
new MemoryOfflineComponentProvider(),
62+
new OnlineComponentProvider(),
63+
{
64+
durable: false
65+
}
66+
);
67+
}
68+
return firestoreClientInstances.get(firestore)!;
69+
}
70+
71+
/**
72+
* Creates a new FirestoreClient for the given Firestore instance. Throws if the
73+
* instance exists.
74+
*
75+
* @param firestore The Firestore instance for which to create the
76+
* FirestoreClient.
77+
* @param componentProvider The component provider to use.
78+
* @param persistenceSettings Settings for the component provider.
79+
*/
80+
export function initializeFirestoreClient(
81+
firestore: Firestore,
82+
offlineComponentProvider: OfflineComponentProvider,
83+
onlineComponentProvider: OnlineComponentProvider,
84+
persistenceSettings: PersistenceSettings
85+
): Promise<void> {
86+
if (firestore._initialized) {
87+
throw new FirestoreError(
88+
Code.FAILED_PRECONDITION,
89+
'Firestore has already been started and persistence can no longer ' +
90+
'be enabled. You can only enable persistence before calling ' +
91+
'any other methods on a Firestore object.'
92+
);
93+
}
94+
95+
const settings = firestore._getSettings();
96+
const databaseInfo = new DatabaseInfo(
97+
firestore._databaseId,
98+
firestore._persistenceKey,
99+
settings.host ?? DEFAULT_HOST,
100+
settings.ssl ?? DEFAULT_SSL,
101+
/** forceLongPolling= */ false
102+
);
103+
const firestoreClient = new FirestoreClient(
104+
firestore._credentials,
105+
firestore._queue
106+
);
107+
const initializationPromise = firestoreClient.start(
108+
databaseInfo,
109+
offlineComponentProvider,
110+
onlineComponentProvider,
111+
persistenceSettings
112+
);
113+
firestoreClientInstances.set(
114+
firestore,
115+
initializationPromise.then(() => firestoreClient)
116+
);
117+
return initializationPromise;
118+
}
119+
120+
/**
121+
* Removes and terminates the FirestoreClient for the given instance if it has
122+
* been started.
123+
*/
124+
export async function removeFirestoreClient(
125+
firestore: Firestore
126+
): Promise<void> {
127+
const firestoreClient = await firestoreClientInstances.get(firestore);
128+
if (firestoreClient) {
129+
firestoreClientInstances.delete(firestore);
130+
return firestoreClient.terminate();
131+
}
132+
}

0 commit comments

Comments
 (0)