/
main.js
81 lines (70 loc) · 2.59 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* This is an example using LocalStorage in your main page to to keep track of
* whether its underlying web worker can force using persistence without
* breaking other tabs/web workers. Since only one tab can use persistence at a
* time, we're using LocalStorage to restrict persistence.
*
* You can invoke Firestore functionality from your main page by passing messages
* to the web worker running Firebase, as shown in the enableNetwork/
* disableNetwork example.
*/
const dedicatedWorker = new Worker("firebase-worker.js");
function main() {
const persistenceAvailable = isPersistenceAvailable();
// Whether the current tab is using persistence.
let isUsingPersistence = false;
// Tell the Firestore worker if it can enable persistence.
if (persistenceAvailable) {
dedicatedWorker.postMessage('persistenceAvailable');
} else {
dedicatedWorker.postMessage('persistenceUnavailable');
}
dedicatedWorker.onmessage = function(event) {
if (event.data === 'persistenceTaken') {
// Mark persistence as unavailable to other tabs, if this tab has
// successfully enabled persistence.
localStorage.setItem('persistenceAvailable', 'false');
isUsingPersistence = true;
}
}
// If the worker in this tab is using persistence, release persistence
// before the page terminates. This allows the next worker that loads
// to use persistence.
window.addEventListener("beforeunload", function() {
console.log('Terminating. Releasing persistence.');
if (isUsingPersistence) {
localStorage.setItem('persistenceAvailable', 'true');
}
})
}
/**
* Checks if persistence is available in LocalStorage, or initializes the
* field if it has not yet been set.
*
* TODO: Currently, if the page crashes, persistence could be permanently
* marked as unavailable in LocalStorage. Implementing some sort of time-based
* expiration would make this more robust.
*/
function isPersistenceAvailable() {
if (localStorage.getItem('persistenceAvailable') === 'false') {
return false;
} else {
if (localStorage.getItem('persistenceAvailable') === null) {
localStorage.setItem('persistenceAvailable', 'true');
}
return true;
}
}
/** Enable the network in Firestore by posting a message to the worker. */
function enableNetwork() {
dedicatedWorker.postMessage('enableNetwork');
}
/** Disable the network in Firestore by posting a message to the worker. */
function disableNetwork() {
dedicatedWorker.postMessage('disableNetwork');
}
/** Writes data to Firestore. */
function writeData() {
dedicatedWorker.postMessage('writeData');
}
main();