castle-web-sdk 0.4.20 → 0.4.21
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/commands.d.ts +2 -0
- package/dist/storage.js +9 -7
- package/package.json +1 -1
package/dist/commands.d.ts
CHANGED
|
@@ -62,6 +62,7 @@ export interface CommandParams {
|
|
|
62
62
|
"deckStorage.load": Record<string, never>;
|
|
63
63
|
"deckStorage.update": {
|
|
64
64
|
updates: StorageUpdate[];
|
|
65
|
+
unload?: boolean;
|
|
65
66
|
};
|
|
66
67
|
"sharedDeckStorage.load": {
|
|
67
68
|
scope: SharedScope;
|
|
@@ -71,6 +72,7 @@ export interface CommandParams {
|
|
|
71
72
|
"sharedDeckStorage.update": {
|
|
72
73
|
scope: SharedScope;
|
|
73
74
|
updates: StorageUpdate[];
|
|
75
|
+
unload?: boolean;
|
|
74
76
|
};
|
|
75
77
|
"leaderboard.fetch": {
|
|
76
78
|
variable: string;
|
package/dist/storage.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CastleError } from "./errors";
|
|
2
2
|
import { hostRequest } from "./transport";
|
|
3
|
-
const FLUSH_INTERVAL_MS =
|
|
3
|
+
const FLUSH_INTERVAL_MS = 1000;
|
|
4
4
|
// Per-player private storage. The host stamps deckId/sessionId, so one deck
|
|
5
5
|
// session = one stable blob: load once, then serve reads from cache and batch
|
|
6
6
|
// writes on a 2s flush. Dirty writes overlay the server blob so an in-flight
|
|
@@ -38,12 +38,13 @@ class PrivateStorageImpl {
|
|
|
38
38
|
this.cache = decodeStorageBlob(blob, "Storage.get");
|
|
39
39
|
this.overlayDirty();
|
|
40
40
|
}
|
|
41
|
-
async flush() {
|
|
41
|
+
async flush(unload = false) {
|
|
42
42
|
if (this.dirty.size === 0)
|
|
43
43
|
return;
|
|
44
44
|
const snapshot = new Map(this.dirty);
|
|
45
45
|
const { blob } = await hostRequest("deckStorage.update", {
|
|
46
46
|
updates: updatesFromDirty(snapshot),
|
|
47
|
+
unload,
|
|
47
48
|
});
|
|
48
49
|
this.cache = decodeStorageBlob(blob, "Storage.flush");
|
|
49
50
|
clearAcknowledgedDirty(this.dirty, snapshot);
|
|
@@ -67,7 +68,7 @@ class PrivateStorageImpl {
|
|
|
67
68
|
}, FLUSH_INTERVAL_MS);
|
|
68
69
|
}
|
|
69
70
|
flushNow() {
|
|
70
|
-
void this.flush();
|
|
71
|
+
void this.flush(true);
|
|
71
72
|
}
|
|
72
73
|
}
|
|
73
74
|
// Cross-player storage. Writes always target the current player (the host
|
|
@@ -155,18 +156,19 @@ class SharedStorageImpl {
|
|
|
155
156
|
read.resolve(value);
|
|
156
157
|
}
|
|
157
158
|
}
|
|
158
|
-
async flush() {
|
|
159
|
+
async flush(unload = false) {
|
|
159
160
|
if (this.dirtyBuckets.size === 0)
|
|
160
161
|
return;
|
|
161
|
-
const tasks = Array.from(this.dirtyBuckets, ([bucketKey, dirty]) => this.flushBucket(bucketKey, new Map(dirty)));
|
|
162
|
+
const tasks = Array.from(this.dirtyBuckets, ([bucketKey, dirty]) => this.flushBucket(bucketKey, new Map(dirty), unload));
|
|
162
163
|
await Promise.all(tasks);
|
|
163
164
|
}
|
|
164
|
-
async flushBucket(bucketKey, snapshot) {
|
|
165
|
+
async flushBucket(bucketKey, snapshot, unload = false) {
|
|
165
166
|
if (snapshot.size === 0)
|
|
166
167
|
return;
|
|
167
168
|
await hostRequest("sharedDeckStorage.update", {
|
|
168
169
|
scope: scopeFromWriteKey(bucketKey),
|
|
169
170
|
updates: updatesFromDirty(snapshot),
|
|
171
|
+
unload,
|
|
170
172
|
});
|
|
171
173
|
const dirty = this.dirtyBuckets.get(bucketKey);
|
|
172
174
|
if (!dirty)
|
|
@@ -185,7 +187,7 @@ class SharedStorageImpl {
|
|
|
185
187
|
}, FLUSH_INTERVAL_MS);
|
|
186
188
|
}
|
|
187
189
|
flushNow() {
|
|
188
|
-
void this.flush().catch(reportSharedStorageError);
|
|
190
|
+
void this.flush(true).catch(reportSharedStorageError);
|
|
189
191
|
}
|
|
190
192
|
}
|
|
191
193
|
// Writes batch on a timer, so leaving the page inside that window would lose
|