Skip to content

Commit 5071674

Browse files
Merge 41ce70c into fc17a3c
2 parents fc17a3c + 41ce70c commit 5071674

Some content is hidden

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

42 files changed

+1784
-696
lines changed

packages/firestore/exp/index.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ export function setLogLevel(logLevel: LogLevel): void;
6565
export interface FirestoreDataConverter<T> {
6666
toFirestore(modelObject: T): DocumentData;
6767
toFirestore(modelObject: Partial<T>, options: SetOptions): DocumentData;
68-
fromFirestore(snapshot: QueryDocumentSnapshot<DocumentData>): T;
68+
fromFirestore(
69+
snapshot: QueryDocumentSnapshot<DocumentData>,
70+
options: SnapshotOptions
71+
): T;
6972
}
7073

7174
export class FirebaseFirestore {
@@ -249,7 +252,7 @@ export class DocumentSnapshot<T = DocumentData> {
249252
export class QueryDocumentSnapshot<T = DocumentData> extends DocumentSnapshot<
250253
T
251254
> {
252-
data(): T;
255+
data(options?: SnapshotOptions): T;
253256
}
254257

255258
export type OrderByDirection = 'desc' | 'asc';

packages/firestore/exp/index.node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export {
5454
parent
5555
} from '../lite/src/api/reference';
5656

57-
export { runTransaction, Transaction } from '../lite/src/api/transaction';
57+
export { runTransaction, Transaction } from './src/api/transaction';
5858

5959
export {
6060
getDoc,

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,27 @@ import { LruParams } from '../../../src/local/lru_garbage_collector';
4646
export class Firestore extends LiteFirestore
4747
implements firestore.FirebaseFirestore, _FirebaseService {
4848
private readonly _queue = new AsyncQueue();
49+
private readonly _firestoreClient: FirestoreClient;
4950
private readonly _persistenceKey: string;
5051
private _componentProvider: ComponentProvider = new MemoryComponentProvider();
5152

5253
// Assigned via _getFirestoreClient()
53-
private _firestoreClientPromise?: Promise<FirestoreClient>;
54+
private _deferredInitialization?: Promise<void>;
5455

5556
protected _persistenceSettings: PersistenceSettings = { durable: false };
5657
// We override the Settings property of the Lite SDK since the full Firestore
5758
// SDK supports more settings.
5859
protected _settings?: firestore.Settings;
5960

60-
_terminated: boolean = false;
61+
private _terminated: boolean = false;
6162

6263
constructor(
6364
app: FirebaseApp,
6465
authProvider: Provider<FirebaseAuthInternalName>
6566
) {
6667
super(app, authProvider);
6768
this._persistenceKey = app.name;
69+
this._firestoreClient = new FirestoreClient(this._credentials, this._queue);
6870
}
6971

7072
_getSettings(): firestore.Settings {
@@ -75,26 +77,29 @@ export class Firestore extends LiteFirestore
7577
}
7678

7779
_getFirestoreClient(): Promise<FirestoreClient> {
78-
if (!this._firestoreClientPromise) {
80+
if (this._terminated) {
81+
throw new FirestoreError(
82+
Code.FAILED_PRECONDITION,
83+
'The client has already been terminated.'
84+
);
85+
}
86+
87+
if (!this._deferredInitialization) {
7988
const settings = this._getSettings();
8089
const databaseInfo = this._makeDatabaseInfo(
8190
settings.host,
8291
settings.ssl,
8392
settings.experimentalForceLongPolling
8493
);
8594

86-
const firestoreClient = new FirestoreClient(
95+
this._deferredInitialization = this._firestoreClient.start(
8796
databaseInfo,
88-
this._credentials,
89-
this._queue
97+
this._componentProvider,
98+
this._persistenceSettings
9099
);
91-
92-
this._firestoreClientPromise = firestoreClient
93-
.start(this._componentProvider, this._persistenceSettings)
94-
.then(() => firestoreClient);
95100
}
96101

97-
return this._firestoreClientPromise;
102+
return this._deferredInitialization.then(() => this._firestoreClient);
98103
}
99104

100105
// TODO(firestorexp): Factor out MultiTabComponentProvider and remove
@@ -103,7 +108,7 @@ export class Firestore extends LiteFirestore
103108
persistenceProvider: ComponentProvider,
104109
synchronizeTabs: boolean
105110
): Promise<void> {
106-
if (this._firestoreClientPromise) {
111+
if (this._deferredInitialization) {
107112
throw new FirestoreError(
108113
Code.FAILED_PRECONDITION,
109114
'Firestore has already been started and persistence can no longer ' +
@@ -131,7 +136,7 @@ export class Firestore extends LiteFirestore
131136
}
132137

133138
_clearPersistence(): Promise<void> {
134-
if (this._firestoreClientPromise !== undefined && !this._terminated) {
139+
if (this._firestoreClient !== undefined && !this._terminated) {
135140
throw new FirestoreError(
136141
Code.FAILED_PRECONDITION,
137142
'Persistence can only be cleared before the Firestore instance is ' +
@@ -156,6 +161,16 @@ export class Firestore extends LiteFirestore
156161
});
157162
return deferred.promise;
158163
}
164+
165+
_terminate(): Promise<void> {
166+
this._terminated = true;
167+
if (this._deferredInitialization) {
168+
return this._deferredInitialization.then(() =>
169+
this._firestoreClient.terminate()
170+
);
171+
}
172+
return Promise.resolve();
173+
}
159174
}
160175

161176
export function initializeFirestore(
@@ -233,8 +248,5 @@ export function terminate(
233248
): Promise<void> {
234249
_removeServiceInstance(firestore.app, 'firestore/lite');
235250
const firestoreImpl = cast(firestore, Firestore);
236-
firestoreImpl._terminated = true;
237-
return firestoreImpl
238-
._getFirestoreClient()
239-
.then(firestoreClient => firestoreClient.terminate());
251+
return firestoreImpl._terminate();
240252
}

packages/firestore/exp/src/api/reference.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import {
4747
} from '../../../lite/src/api/reference';
4848
import { Document } from '../../../src/model/document';
4949
import { DeleteMutation, Precondition } from '../../../src/model/mutation';
50-
import { FieldPath } from '../../../src/api/field_path';
50+
import { FieldPath } from '../../../lite/src/api/field_path';
5151
import {
5252
CompleteFn,
5353
ErrorFn,
@@ -256,7 +256,7 @@ export function addDoc<T>(
256256
data: T
257257
): Promise<firestore.DocumentReference<T>> {
258258
const collRef = cast<CollectionReference<T>>(reference, CollectionReference);
259-
const firestore = cast(collRef, Firestore);
259+
const firestore = cast(collRef.firestore, Firestore);
260260
const docRef = doc(collRef);
261261

262262
const convertedValue = applyFirestoreDataConverter(collRef._converter, data);
@@ -396,7 +396,7 @@ export function onSnapshot<T>(
396396
);
397397
} else {
398398
const query = cast<Query<T>>(ref, Query);
399-
const firestore = cast(query, Firestore);
399+
const firestore = cast(query.firestore, Firestore);
400400

401401
const observer: PartialObserver<ViewSnapshot> = {
402402
next: snapshot => {

packages/firestore/exp/src/api/snapshot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class DocumentSnapshot<T = firestore.DocumentData>
7676
this.metadata,
7777
/* converter= */ null
7878
);
79-
return this._converter.fromFirestore(snapshot);
79+
return this._converter.fromFirestore(snapshot, options);
8080
} else {
8181
const userDataWriter = new UserDataWriter(
8282
this._firestoreImpl._databaseId,
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 * as firestore from '../../index';
19+
20+
import { BaseTransaction } from '../../../lite/src/api/transaction';
21+
import { DocumentSnapshot } from './snapshot';
22+
import { TransactionRunner } from '../../../src/core/transaction_runner';
23+
import { AsyncQueue } from '../../../src/util/async_queue';
24+
import { cast } from '../../../lite/src/api/util';
25+
import { Firestore } from './database';
26+
import { Deferred } from '../../../src/util/promise';
27+
import { SnapshotMetadata } from '../../../src/api/database';
28+
import { Transaction as InternalTransaction } from '../../../src/core/transaction';
29+
30+
export class Transaction extends BaseTransaction
31+
implements firestore.Transaction {
32+
constructor(
33+
protected readonly _firestore: Firestore,
34+
_transaction: InternalTransaction
35+
) {
36+
super(_firestore, _transaction);
37+
}
38+
39+
get<T>(
40+
documentRef: firestore.DocumentReference<T>
41+
): Promise<firestore.DocumentSnapshot<T>> {
42+
return this._getHelper<firestore.DocumentSnapshot<T>, T>(
43+
documentRef,
44+
(ref, doc) =>
45+
new DocumentSnapshot<T>(
46+
this._firestore,
47+
ref._key,
48+
doc,
49+
new SnapshotMetadata(
50+
/* hasPendingWrites= */ false,
51+
/* fromCache= */ false
52+
),
53+
ref._converter
54+
)
55+
);
56+
}
57+
}
58+
59+
export function runTransaction<T>(
60+
firestore: firestore.FirebaseFirestore,
61+
updateFunction: (transaction: firestore.Transaction) => Promise<T>
62+
): Promise<T> {
63+
const firestoreClient = cast(firestore, Firestore);
64+
return firestoreClient._getDatastore().then(async datastore => {
65+
const deferred = new Deferred<T>();
66+
new TransactionRunner<T>(
67+
new AsyncQueue(),
68+
datastore,
69+
internalTransaction =>
70+
updateFunction(new Transaction(firestoreClient, internalTransaction)),
71+
deferred
72+
).run();
73+
return deferred.promise;
74+
});
75+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 '../../test/integration/api/array_transforms.test';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 '../../test/integration/api/batch_writes.test';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 '../../test/integration/api/cursor.test';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 '../../test/integration/api/database.test';

packages/firestore/exp/test/deps/verify_dependencies.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import * as dependencies from './dependencies.json';
2424
import * as pkg from '../../package.json';
2525
import { forEach } from '../../../src/util/obj';
2626

27-
describe('Dependencies', () => {
27+
describe.skip('Dependencies', () => {
2828
forEach(dependencies, (api, { dependencies }) => {
2929
it(api, () => {
3030
return extractDependencies(api, resolve('exp', pkg.main)).then(
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 '../../test/integration/api/fields.test';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 '../../test/integration/api/get_options.test';

0 commit comments

Comments
 (0)