Skip to content

Commit 95c8c15

Browse files
authored
Add new web persistence (#331)
* Add new web persistence * code review changes * fix snippet * run snippets
1 parent 89e03fd commit 95c8c15

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

firestore-next/test.firestore.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,36 @@ describe("firestore", () => {
7474

7575
const db = getFirestore(app);
7676

77+
const {
78+
initializeFirestore,
79+
memoryLocalCache,
80+
persistentLocalCache,
81+
persistentSingleTabManager,
82+
persistentMultipleTabManager
83+
} = require("firebase/firestore");
84+
7785
// [START initialize_persistence]
78-
const { enableIndexedDbPersistence } = require("firebase/firestore");
86+
// Memory cache is the default if no config is specified.
87+
initializeFirestore(app, {});
7988

80-
enableIndexedDbPersistence(db)
81-
.catch((err) => {
82-
if (err.code == 'failed-precondition') {
83-
// Multiple tabs open, persistence can only be enabled
84-
// in one tab at a a time.
85-
// ...
86-
} else if (err.code == 'unimplemented') {
87-
// The current browser does not support all of the
88-
// features required to enable persistence
89-
// ...
90-
}
89+
// This is the default behavior if no persistence is specified.
90+
initializeFirestore(app, {localCache: memoryLocalCache()});
91+
92+
// Defaults to single-tab persistence if no tab manager is specified.
93+
initializeFirestore(app, {localCache: persistentLocalCache(/*settings*/{})});
94+
95+
// Same as `initializeFirestore(app, {localCache: persistentLocalCache(/*settings*/{})})`,
96+
// but more explicit about tab management.
97+
initializeFirestore(app,
98+
{localCache:
99+
persistentLocalCache(/*settings*/{tabManager: persistentSingleTabManager({})})
100+
});
101+
102+
// Use multi-tab IndexedDb persistence.
103+
initializeFirestore(app,
104+
{localCache:
105+
persistentLocalCache(/*settings*/{tabManager: persistentMultipleTabManager()})
91106
});
92-
// Subsequent queries will use persistence, if it was enabled successfully
93107
// [END initialize_persistence]
94108
});
95109

snippets/firestore-next/test-firestore/initialize_persistence.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,25 @@
55
// 'npm run snippets'.
66

77
// [START initialize_persistence_modular]
8-
import { enableIndexedDbPersistence } from "firebase/firestore";
8+
// Memory cache is the default if no config is specified.
9+
initializeFirestore(app, {});
910

10-
enableIndexedDbPersistence(db)
11-
.catch((err) => {
12-
if (err.code == 'failed-precondition') {
13-
// Multiple tabs open, persistence can only be enabled
14-
// in one tab at a a time.
15-
// ...
16-
} else if (err.code == 'unimplemented') {
17-
// The current browser does not support all of the
18-
// features required to enable persistence
19-
// ...
20-
}
11+
// This is the default behavior if no persistence is specified.
12+
initializeFirestore(app, {localCache: memoryLocalCache()});
13+
14+
// Defaults to single-tab persistence if no tab manager is specified.
15+
initializeFirestore(app, {localCache: persistentLocalCache(/*settings*/{})});
16+
17+
// Same as `initializeFirestore(app, {localCache: persistentLocalCache(/*settings*/{})})`,
18+
// but more explicit about tab management.
19+
initializeFirestore(app,
20+
{localCache:
21+
persistentLocalCache(/*settings*/{tabManager: persistentSingleTabManager({})})
22+
});
23+
24+
// Use multi-tab IndexedDb persistence.
25+
initializeFirestore(app,
26+
{localCache:
27+
persistentLocalCache(/*settings*/{tabManager: persistentMultipleTabManager()})
2128
});
22-
// Subsequent queries will use persistence, if it was enabled successfully
2329
// [END initialize_persistence_modular]

0 commit comments

Comments
 (0)