castle-web-sdk 0.4.19 → 0.4.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/storage.js +28 -0
- package/package.json +1 -1
package/dist/storage.js
CHANGED
|
@@ -58,6 +58,7 @@ class PrivateStorageImpl {
|
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
scheduleFlush() {
|
|
61
|
+
hookUnloadFlush();
|
|
61
62
|
if (this.flushTimer)
|
|
62
63
|
return;
|
|
63
64
|
this.flushTimer = setTimeout(() => {
|
|
@@ -65,6 +66,9 @@ class PrivateStorageImpl {
|
|
|
65
66
|
void this.flush();
|
|
66
67
|
}, FLUSH_INTERVAL_MS);
|
|
67
68
|
}
|
|
69
|
+
flushNow() {
|
|
70
|
+
void this.flush();
|
|
71
|
+
}
|
|
68
72
|
}
|
|
69
73
|
// Cross-player storage. Writes always target the current player (the host
|
|
70
74
|
// forces 'user'-scope writes to whoever is signed in), so dirty writes bucket
|
|
@@ -172,6 +176,7 @@ class SharedStorageImpl {
|
|
|
172
176
|
this.dirtyBuckets.delete(bucketKey);
|
|
173
177
|
}
|
|
174
178
|
scheduleFlush() {
|
|
179
|
+
hookUnloadFlush();
|
|
175
180
|
if (this.flushTimer)
|
|
176
181
|
return;
|
|
177
182
|
this.flushTimer = setTimeout(() => {
|
|
@@ -179,6 +184,29 @@ class SharedStorageImpl {
|
|
|
179
184
|
void this.flush().catch(reportSharedStorageError);
|
|
180
185
|
}, FLUSH_INTERVAL_MS);
|
|
181
186
|
}
|
|
187
|
+
flushNow() {
|
|
188
|
+
void this.flush().catch(reportSharedStorageError);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
// Writes batch on a timer, so leaving the page inside that window would lose
|
|
192
|
+
// them -- a deck saving progress as someone quits is exactly when it matters.
|
|
193
|
+
// `pagehide` is the reliable signal on iOS, where unload often never fires, and
|
|
194
|
+
// visibilitychange covers backgrounding without a navigation. Hooked on the
|
|
195
|
+
// first write so a deck that never stores anything registers nothing.
|
|
196
|
+
let unloadFlushHooked = false;
|
|
197
|
+
function hookUnloadFlush() {
|
|
198
|
+
if (unloadFlushHooked || typeof window === "undefined")
|
|
199
|
+
return;
|
|
200
|
+
unloadFlushHooked = true;
|
|
201
|
+
const flush = () => {
|
|
202
|
+
Storage.flushNow();
|
|
203
|
+
SharedStorage.flushNow();
|
|
204
|
+
};
|
|
205
|
+
window.addEventListener("pagehide", flush);
|
|
206
|
+
document.addEventListener("visibilitychange", () => {
|
|
207
|
+
if (document.visibilityState === "hidden")
|
|
208
|
+
flush();
|
|
209
|
+
});
|
|
182
210
|
}
|
|
183
211
|
export const Storage = new PrivateStorageImpl();
|
|
184
212
|
export const SharedStorage = new SharedStorageImpl();
|