Skip to content

Commit 397bddc

Browse files
Cleanup
1 parent 07ff26e commit 397bddc

File tree

18 files changed

+156
-138
lines changed

18 files changed

+156
-138
lines changed

integration/firestore/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.1",
44
"private": true,
55
"scripts": {
6-
"build:deps": "cd ../../ ; yarn build",
6+
"build:deps": "cd ../../packages/firestore ; yarn build",
77
"build:persistence": "INCLUDE_FIRESTORE_PERSISTENCE=true gulp compile-tests",
88
"build:memory": "INCLUDE_FIRESTORE_PERSISTENCE=false gulp compile-tests",
99
"test": "yarn build:memory; karma start --single-run; yarn build:persistence; karma start --single-run;",

packages/firebase/firestore/memory/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@
1515
* limitations under the License.
1616
*/
1717

18+
/**
19+
* This file serves as the public entrypoint for users that import
20+
* `firebase/firestore/memory`.
21+
*/
22+
1823
import '../../../firestore/dist/index.memory.esm';
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "firebase/firestore/memory",
3+
"description": "A memory-only build of the Cloud Firestore JS SDK.",
34
"main": "dist/index.cjs.js",
45
"module": "dist/index.esm.js",
5-
"typings": "../../empty-import.d.ts"
6+
"typings": "../../empty-import.d.ts",
67
}

packages/firebase/firestore/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "firebase/firestore",
3+
"description": "The Cloud Firestore component of the Firebase JS SDK.",
34
"main": "dist/index.cjs.js",
45
"module": "dist/index.esm.js",
56
"typings": "../empty-import.d.ts"

packages/firestore/index.memory.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
import firebase from '@firebase/app';
1919
import { FirebaseNamespace } from '@firebase/app-types';
2020

21-
import {MemoryPersistenceProvider} from "./src/local/memory_persistence";
21+
import { Firestore } from './src/api/database';
22+
import { MemoryPersistenceProvider } from './src/local/memory_persistence';
2223
import { configureForFirebase } from './src/platform/config';
2324

2425
import './register-module';
@@ -30,7 +31,10 @@ import { name, version } from './package.json';
3031
* Registers the memory-only Firestore build with the components framework.
3132
*/
3233
export function registerFirestore(instance: FirebaseNamespace): void {
33-
configureForFirebase(instance, () => new MemoryPersistenceProvider());
34+
configureForFirebase(
35+
instance,
36+
(app, auth) => new Firestore(app, auth, new MemoryPersistenceProvider())
37+
);
3438
instance.registerVersion(name, version);
3539
}
3640

packages/firestore/index.node.memory.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,23 @@
1818
import firebase from '@firebase/app';
1919
import { FirebaseNamespace } from '@firebase/app-types';
2020

21-
import {MemoryPersistenceProvider} from "./src/local/memory_persistence";
21+
import { Firestore } from './src/api/database';
22+
import { MemoryPersistenceProvider } from './src/local/memory_persistence';
2223
import { configureForFirebase } from './src/platform/config';
2324
import './register-module';
2425
import './src/platform_node/node_init';
2526

2627
import { name, version } from './package.json';
2728

2829
/**
29-
* Registers the memory-only Firestore build for Node with the components
30+
* Registers the memory-only Firestore build for Node with the components
3031
* framework.
3132
*/
3233
export function registerFirestore(instance: FirebaseNamespace): void {
33-
configureForFirebase(instance, () => new MemoryPersistenceProvider());
34+
configureForFirebase(
35+
instance,
36+
(app, auth) => new Firestore(app, auth, new MemoryPersistenceProvider())
37+
);
3438
instance.registerVersion(name, version);
3539
}
3640

packages/firestore/index.node.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import firebase from '@firebase/app';
1818
import { FirebaseNamespace } from '@firebase/app-types';
1919

20-
import {IndexedDbPersistenceProvider} from "./src/local/indexeddb_persistence";
20+
import { Firestore } from './src/api/database';
21+
import { IndexedDbPersistenceProvider } from './src/local/indexeddb_persistence';
2122
import { configureForFirebase } from './src/platform/config';
2223

2324
import './register-module';
@@ -32,7 +33,7 @@ import { name, version } from './package.json';
3233
export function registerFirestore(instance: FirebaseNamespace): void {
3334
configureForFirebase(
3435
instance,
35-
() => new IndexedDbPersistenceProvider()
36+
(app, auth) => new Firestore(app, auth, new IndexedDbPersistenceProvider())
3637
);
3738
instance.registerVersion(name, version, 'node');
3839
}

packages/firestore/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
import firebase from '@firebase/app';
1919
import { FirebaseNamespace } from '@firebase/app-types';
2020

21-
import {IndexedDbPersistenceProvider} from "./src/local/indexeddb_persistence";
21+
import { Firestore } from './src/api/database';
22+
import { IndexedDbPersistenceProvider } from './src/local/indexeddb_persistence';
2223
import { configureForFirebase } from './src/platform/config';
2324
import { name, version } from './package.json';
2425

@@ -31,7 +32,8 @@ import './src/platform_browser/browser_init';
3132
*/
3233
export function registerFirestore(instance: FirebaseNamespace): void {
3334
configureForFirebase(
34-
instance, () => new IndexedDbPersistenceProvider()
35+
instance,
36+
(app, auth) => new Firestore(app, auth, new IndexedDbPersistenceProvider())
3537
);
3638
instance.registerVersion(name, version);
3739
}

packages/firestore/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"dev": "rollup -c -w",
1111
"lint": "eslint -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
1212
"lint:fix": "eslint --fix -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
13-
"prettier": "prettier --write 'src/**/*.js' 'test/**/*.js' 'src/**/*.ts' 'test/**/*.ts'",
13+
"prettier": "prettier --write '*.ts' '*.js' 'src/**/*.js' 'test/**/*.js' 'src/**/*.ts' 'test/**/*.ts'",
1414
"test": "run-s lint test:all",
1515
"test:all": "run-p test:browser test:travis test:minified",
1616
"test:browser": "karma start --single-run",

packages/firestore/rollup.config.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import memoryPkg from './memory/package.json';
3030

3131
import {
3232
appendPrivatePrefixTransformers,
33-
manglePrivatePropertiesOptions,
33+
manglePrivatePropertiesOptions
3434
} from './terser.config';
3535

3636
// This Firestore Rollup configuration provides a number of different builds:
@@ -45,8 +45,11 @@ import {
4545
// The in-memory builds are roughly 130 KB smaller, but throw an exception
4646
// for calls to `enablePersistence()` or `clearPersistence()`.
4747
//
48-
// All browser builds rely on Terser's property name mangling to reduce code
48+
// All browser builds rely on Terser's property name mangling to reduce code
4949
// size.
50+
//
51+
// See https://g3doc.corp.google.com/firebase/jscore/g3doc/contributing/builds.md
52+
// for a description of the various JavaScript formats used in our SDKs.
5053

5154
// MARK: Browser builds
5255

@@ -67,8 +70,8 @@ function resolveNodeExterns(id) {
6770
}
6871

6972
/**
70-
* Resolves the external dependencies for the Memory-based Firestore
71-
* implementation. Verifies that no persistence sources are used by Firestore's
73+
* Resolves the external dependencies for the Memory-based Firestore
74+
* implementation. Verifies that no persistence sources are used by Firestore's
7275
* memory-only implementation.
7376
*/
7477
export function resolveMemoryExterns(deps, externsId, referencedBy) {
@@ -138,7 +141,8 @@ const browserBuilds = [
138141
sourcemap: true
139142
},
140143
plugins: es5BuildPlugins,
141-
external: (id, referencedBy) => resolveMemoryExterns(browserDeps, id, referencedBy)
144+
external: (id, referencedBy) =>
145+
resolveMemoryExterns(browserDeps, id, referencedBy)
142146
},
143147
// ES2017 ESM build (with persistence)
144148
{
@@ -160,7 +164,8 @@ const browserBuilds = [
160164
sourcemap: true
161165
},
162166
plugins: es2017BuildPlugins,
163-
external: (id, referencedBy) => resolveMemoryExterns(browserDeps, id, referencedBy)
167+
external: (id, referencedBy) =>
168+
resolveMemoryExterns(browserDeps, id, referencedBy)
164169
},
165170
// ES5 CJS Build (with persistence)
166171
//

packages/firestore/src/api/database.ts

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ import { FirebaseApp } from '@firebase/app-types';
2121
import { FirebaseService, _FirebaseApp } from '@firebase/app-types/private';
2222
import { DatabaseId, DatabaseInfo } from '../core/database_info';
2323
import { ListenOptions } from '../core/event_manager';
24-
import {
25-
FirestoreClient,
26-
IndexedDbPersistenceSettings,
27-
InternalPersistenceSettings,
28-
MemoryPersistenceSettings
29-
} from '../core/firestore_client';
24+
import { FirestoreClient, PersistenceSettings } from '../core/firestore_client';
3025
import {
3126
Bound,
3227
Direction,
@@ -82,7 +77,7 @@ import * as log from '../util/log';
8277
import { LogLevel } from '../util/log';
8378
import { AutoId } from '../util/misc';
8479
import * as objUtils from '../util/obj';
85-
import { Rejecter, Resolver } from '../util/promise';
80+
import { Deferred, Rejecter, Resolver } from '../util/promise';
8681
import { FieldPath as ExternalFieldPath } from './field_path';
8782

8883
import {
@@ -121,11 +116,6 @@ const DEFAULT_FORCE_LONG_POLLING = false;
121116
*/
122117
export const CACHE_SIZE_UNLIMITED = LruParams.COLLECTION_DISABLED;
123118

124-
const PERSISTENCE_MISSING_ERROR_MSG =
125-
'You are using the memory-only build of Firestore. Persistence support is ' +
126-
'only available via the @firebase/firestore bundle or the ' +
127-
'firebase-firestore.js build.';
128-
129119
// enablePersistence() defaults:
130120
const DEFAULT_SYNCHRONIZE_TABS = false;
131121

@@ -393,13 +383,6 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
393383
}
394384

395385
enablePersistence(settings?: firestore.PersistenceSettings): Promise<void> {
396-
if (!this._persistenceProvider.isDurable) {
397-
throw new FirestoreError(
398-
Code.FAILED_PRECONDITION,
399-
PERSISTENCE_MISSING_ERROR_MSG
400-
);
401-
}
402-
403386
if (this._firestoreClient) {
404387
throw new FirestoreError(
405388
Code.FAILED_PRECONDITION,
@@ -428,23 +411,14 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
428411
);
429412
}
430413

431-
return this.configureClient(
432-
this._persistenceProvider,
433-
new IndexedDbPersistenceSettings(
434-
this._settings.cacheSizeBytes,
435-
synchronizeTabs
436-
)
437-
);
414+
return this.configureClient(this._persistenceProvider, {
415+
durable: true,
416+
cacheSizeBytes: this._settings.cacheSizeBytes,
417+
synchronizeTabs
418+
});
438419
}
439420

440421
async clearPersistence(): Promise<void> {
441-
if (!this._persistenceProvider.isDurable) {
442-
throw new FirestoreError(
443-
Code.FAILED_PRECONDITION,
444-
PERSISTENCE_MISSING_ERROR_MSG
445-
);
446-
}
447-
448422
if (
449423
this._firestoreClient !== undefined &&
450424
!this._firestoreClient.clientTerminated
@@ -455,7 +429,17 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
455429
);
456430
}
457431

458-
return this._persistenceProvider.clearPersistence();
432+
const deferred = new Deferred<void>();
433+
this._queue.enqueueAndForgetEvenAfterShutdown(async () => {
434+
try {
435+
const databaseInfo = this.makeDatabaseInfo();
436+
await this._persistenceProvider.clearPersistence(databaseInfo);
437+
deferred.resolve();
438+
} catch (e) {
439+
deferred.reject(e);
440+
}
441+
});
442+
return deferred.promise;
459443
}
460444

461445
terminate(): Promise<void> {
@@ -514,10 +498,9 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
514498
if (!this._firestoreClient) {
515499
// Kick off starting the client but don't actually wait for it.
516500
// eslint-disable-next-line @typescript-eslint/no-floating-promises
517-
this.configureClient(
518-
new MemoryPersistenceProvider(),
519-
new MemoryPersistenceSettings()
520-
);
501+
this.configureClient(new MemoryPersistenceProvider(), {
502+
durable: false
503+
});
521504
}
522505
return this._firestoreClient as FirestoreClient;
523506
}
@@ -534,7 +517,7 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
534517

535518
private configureClient(
536519
persistenceProvider: PersistenceProvider,
537-
persistenceSettings: InternalPersistenceSettings
520+
persistenceSettings: PersistenceSettings
538521
): Promise<void> {
539522
assert(!!this._settings.host, 'FirestoreSettings.host is not set');
540523

packages/firestore/src/core/firestore_client.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,15 @@ const DOM_EXCEPTION_INVALID_STATE = 11;
5959
const DOM_EXCEPTION_ABORTED = 20;
6060
const DOM_EXCEPTION_QUOTA_EXCEEDED = 22;
6161

62-
export class IndexedDbPersistenceSettings {
63-
constructor(
64-
readonly cacheSizeBytes: number,
65-
readonly synchronizeTabs: boolean
66-
) {}
67-
}
68-
69-
export class MemoryPersistenceSettings {}
70-
71-
export type InternalPersistenceSettings =
72-
| IndexedDbPersistenceSettings
73-
| MemoryPersistenceSettings;
62+
export type PersistenceSettings =
63+
| {
64+
readonly durable: false;
65+
}
66+
| {
67+
readonly durable: true;
68+
readonly cacheSizeBytes: number;
69+
readonly synchronizeTabs: boolean;
70+
};
7471

7572
/**
7673
* FirestoreClient is a top-level class that constructs and owns all of the
@@ -139,6 +136,8 @@ export class FirestoreClient {
139136
* fallback succeeds we signal success to the async queue even though the
140137
* start() itself signals failure.
141138
*
139+
* @param persistenceProvider Provider that returns the persistence
140+
* implementation.
142141
* @param persistenceSettings Settings object to configure offline
143142
* persistence.
144143
* @returns A deferred result indicating the user-visible result of enabling
@@ -148,7 +147,7 @@ export class FirestoreClient {
148147
*/
149148
start(
150149
persistenceProvider: PersistenceProvider,
151-
persistenceSettings: InternalPersistenceSettings
150+
persistenceSettings: PersistenceSettings
152151
): Promise<void> {
153152
this.verifyNotTerminated();
154153
// We defer our initialization until we get the current user from
@@ -229,12 +228,12 @@ export class FirestoreClient {
229228
*/
230229
private async initializePersistence(
231230
persistenceProvider: PersistenceProvider,
232-
persistenceSettings: InternalPersistenceSettings,
231+
persistenceSettings: PersistenceSettings,
233232
user: User,
234233
persistenceResult: Deferred<void>
235234
): Promise<void> {
236235
try {
237-
await persistenceProvider.configure(
236+
await persistenceProvider.initialize(
238237
this.asyncQueue,
239238
this.databaseInfo,
240239
this.platform,
@@ -263,7 +262,7 @@ export class FirestoreClient {
263262
);
264263
return this.initializePersistence(
265264
new MemoryPersistenceProvider(),
266-
persistenceSettings,
265+
{ durable: false },
267266
user,
268267
persistenceResult
269268
);
@@ -275,7 +274,7 @@ export class FirestoreClient {
275274
* persistence (as opposed to crashing the client).
276275
*/
277276
private canFallback(error: FirestoreError | DOMException): boolean {
278-
if (error instanceof FirestoreError) {
277+
if (error.name === 'FirebaseError') {
279278
return (
280279
error.code === Code.FAILED_PRECONDITION ||
281280
error.code === Code.UNIMPLEMENTED

0 commit comments

Comments
 (0)