castle-web-cli 0.4.156 → 0.4.157

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.
@@ -297,12 +297,12 @@ async function deckStorageLoad(ctx, gql) {
297
297
  }
298
298
  async function deckStorageUpdate(ctx, params, gql) {
299
299
  const deckId = requireDeckId(ctx, "deckStorage.update");
300
- const data = await graphql(gql, UPDATE_DECK_STORAGE_MUTATION, {
301
- deckId,
302
- sessionId: null,
303
- updates: asUpdates(params.updates),
304
- }, "deckStorage.update", { unload: params.unload === true });
305
- return { blob: data.updateDeckStorage ?? {} };
300
+ stage(deckId, null, null, asUpdates(params.updates), gql);
301
+ if (params.unload === true)
302
+ await sendStaged(true);
303
+ // Nothing has been sent yet, so there is no merged blob to hand back; the
304
+ // deck's own cache already reflects the write.
305
+ return { blob: {} };
306
306
  }
307
307
  async function sharedDeckStorageLoad(ctx, params, gql) {
308
308
  const deckId = requireDeckId(ctx, "sharedDeckStorage.load");
@@ -317,13 +317,117 @@ async function sharedDeckStorageLoad(ctx, params, gql) {
317
317
  { deckId, sessionId: null, userId, keys: asKeys(params.keys) }, "sharedDeckStorage.load");
318
318
  return { blob: data.sharedDeckStorage ?? {} };
319
319
  }
320
+ // ---------------------------------------------------------------------------
321
+ // Staged writes.
322
+ //
323
+ // The deck coalesces per frame and hands writes over every 200ms; from here on
324
+ // they are the host's. That matters because the host runs in the PAGE: when a
325
+ // deck frame is torn down, or the page itself starts to unload, this code is
326
+ // still alive to finish the request, and the deck's own attempt would not be --
327
+ // its message would never be processed.
328
+ //
329
+ // Server traffic stays at one request per second per bucket. A failed send keeps
330
+ // its writes pending and the next tick retries; the last attempt goes out with
331
+ // `unload` so the host can use fetch keepalive, which the browser finishes after
332
+ // the page is gone.
333
+ const STAGE_SEND_INTERVAL_MS = 1000;
334
+ const staged = new Map();
335
+ let stageTimer = null;
336
+ let stageFetch = null;
337
+ let unloadHooked = false;
338
+ function stageKey(deckId, scope, userId) {
339
+ return `${deckId}|${scope ?? "private"}|${userId ?? "self"}`;
340
+ }
341
+ function stage(deckId, scope, userId, updates, gql) {
342
+ const key = stageKey(deckId, scope, userId);
343
+ const bucket = staged.get(key) ?? {
344
+ deckId,
345
+ scope,
346
+ userId,
347
+ updates: new Map(),
348
+ };
349
+ for (const update of updates)
350
+ bucket.updates.set(update.key, update.value ?? null);
351
+ staged.set(key, bucket);
352
+ stageFetch = gql;
353
+ hookUnload();
354
+ if (!stageTimer) {
355
+ stageTimer = setTimeout(() => {
356
+ stageTimer = null;
357
+ void sendStaged(false);
358
+ }, STAGE_SEND_INTERVAL_MS);
359
+ }
360
+ }
361
+ async function sendStaged(unload) {
362
+ const gql = stageFetch;
363
+ if (!gql || staged.size === 0)
364
+ return;
365
+ for (const [key, bucket] of Array.from(staged)) {
366
+ if (bucket.updates.size === 0) {
367
+ staged.delete(key);
368
+ continue;
369
+ }
370
+ const sending = Array.from(bucket.updates, ([k, value]) => ({ key: k, value }));
371
+ // Cleared before the send, and put back if it fails: a write that arrives
372
+ // mid-flight must not be dropped by a later success.
373
+ staged.delete(key);
374
+ try {
375
+ if (bucket.scope === null) {
376
+ await graphql(gql, UPDATE_DECK_STORAGE_MUTATION, { deckId: bucket.deckId, sessionId: null, updates: sending }, "deckStorage.update", { unload });
377
+ }
378
+ else {
379
+ await graphql(gql, UPDATE_SHARED_DECK_STORAGE_MUTATION, {
380
+ deckId: bucket.deckId,
381
+ sessionId: null,
382
+ userId: bucket.userId,
383
+ updates: sending,
384
+ }, "sharedDeckStorage.update", { unload });
385
+ }
386
+ }
387
+ catch {
388
+ // Keep them pending for the next tick. Nothing to report to the deck --
389
+ // it handed these over and moved on.
390
+ const current = staged.get(key);
391
+ if (current) {
392
+ for (const update of sending) {
393
+ if (!current.updates.has(update.key))
394
+ current.updates.set(update.key, update.value);
395
+ }
396
+ }
397
+ else {
398
+ staged.set(key, { ...bucket, updates: new Map(sending.map((u) => [u.key, u.value])) });
399
+ }
400
+ if (!unload && !stageTimer) {
401
+ stageTimer = setTimeout(() => {
402
+ stageTimer = null;
403
+ void sendStaged(false);
404
+ }, STAGE_SEND_INTERVAL_MS);
405
+ }
406
+ }
407
+ }
408
+ }
409
+ function hookUnload() {
410
+ if (unloadHooked || typeof window === "undefined")
411
+ return;
412
+ unloadHooked = true;
413
+ const flush = () => {
414
+ void sendStaged(true);
415
+ };
416
+ window.addEventListener("pagehide", flush);
417
+ document.addEventListener("visibilitychange", () => {
418
+ if (document.visibilityState === "hidden")
419
+ flush();
420
+ });
421
+ }
320
422
  async function sharedDeckStorageUpdate(ctx, params, gql) {
321
423
  const deckId = requireDeckId(ctx, "sharedDeckStorage.update");
322
424
  const scope = asScope(params.scope, "sharedDeckStorage.update");
323
425
  // 'user'-scope writes are forced to the current player — the deck cannot
324
426
  // write another player's bucket.
325
427
  const userId = scope === "user" ? requireUserId(ctx, "sharedDeckStorage.update") : null;
326
- await graphql(gql, UPDATE_SHARED_DECK_STORAGE_MUTATION, { deckId, sessionId: null, userId, updates: asUpdates(params.updates) }, "sharedDeckStorage.update", { unload: params.unload === true });
428
+ stage(deckId, scope, userId, asUpdates(params.updates), gql);
429
+ if (params.unload === true)
430
+ await sendStaged(true);
327
431
  return { ok: true };
328
432
  }
329
433
  async function leaderboardFetch(ctx, params, caps) {
package/dist/init.js CHANGED
@@ -46,7 +46,7 @@ export const BARE_DOCS_KIT = 'blank';
46
46
  // Registry version of castle-web-sdk to inject when scaffolding from a
47
47
  // globally-installed castle-web (not from inside the workspace). Bumped
48
48
  // alongside cli/sdk version bumps.
49
- const PUBLISHED_SDK_VERSION = '0.4.21';
49
+ const PUBLISHED_SDK_VERSION = '0.4.22';
50
50
  // Range, not a pin: `castle-web-fonts` is its own package precisely so a tenth
51
51
  // face can ship without a cli release, and a caret range means an existing
52
52
  // deck's next install picks it up. Nothing here has to move when it does.
@@ -7,5 +7,5 @@
7
7
  "title": "blank",
8
8
  "deckId": "yRcmH4_aYllE",
9
9
  "cardId": "zkFdingmIm3m",
10
- "publishedVersion": "2026-08-30T22:05:14.995Z"
10
+ "publishedVersion": "2026-08-30T22:21:19.361Z"
11
11
  }
@@ -132,5 +132,5 @@
132
132
  "main": "main.jsx",
133
133
  "autoUpdateWhenImported": true,
134
134
  "title": "physics-2d",
135
- "publishedVersion": "2026-08-30T22:05:17.419Z"
135
+ "publishedVersion": "2026-08-30T22:21:20.938Z"
136
136
  }
@@ -72,5 +72,5 @@
72
72
  "autoUpdateWhenImported": true,
73
73
  "deckId": "JH0SclbPVP0y",
74
74
  "cardId": "eXfAaUdbSU5A",
75
- "publishedVersion": "2026-08-30T22:05:20.169Z"
75
+ "publishedVersion": "2026-08-30T22:21:23.701Z"
76
76
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "castle-web-cli",
3
- "version": "0.4.156",
3
+ "version": "0.4.157",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "castle-web": "./dist/index.js"