castle-web-cli 0.4.155 → 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.
@@ -289,17 +289,20 @@ function unavailableOutcome() {
289
289
  }
290
290
  async function deckStorageLoad(ctx, gql) {
291
291
  const deckId = requireDeckId(ctx, "deckStorage.load");
292
- const data = await graphql(gql, DECK_STORAGE_QUERY, { deckId, sessionId: ctx.sessionId }, "deckStorage.load");
292
+ const data = await graphql(gql, DECK_STORAGE_QUERY,
293
+ // Same reason as shared storage: a play session is not a save slot. With one
294
+ // the key becomes per-play, so a deck's own save data is gone next time.
295
+ { deckId, sessionId: null }, "deckStorage.load");
293
296
  return { blob: data.deckStorage ?? {} };
294
297
  }
295
298
  async function deckStorageUpdate(ctx, params, gql) {
296
299
  const deckId = requireDeckId(ctx, "deckStorage.update");
297
- const data = await graphql(gql, UPDATE_DECK_STORAGE_MUTATION, {
298
- deckId,
299
- sessionId: ctx.sessionId,
300
- updates: asUpdates(params.updates),
301
- }, "deckStorage.update");
302
- 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: {} };
303
306
  }
304
307
  async function sharedDeckStorageLoad(ctx, params, gql) {
305
308
  const deckId = requireDeckId(ctx, "sharedDeckStorage.load");
@@ -314,13 +317,117 @@ async function sharedDeckStorageLoad(ctx, params, gql) {
314
317
  { deckId, sessionId: null, userId, keys: asKeys(params.keys) }, "sharedDeckStorage.load");
315
318
  return { blob: data.sharedDeckStorage ?? {} };
316
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
+ }
317
422
  async function sharedDeckStorageUpdate(ctx, params, gql) {
318
423
  const deckId = requireDeckId(ctx, "sharedDeckStorage.update");
319
424
  const scope = asScope(params.scope, "sharedDeckStorage.update");
320
425
  // 'user'-scope writes are forced to the current player — the deck cannot
321
426
  // write another player's bucket.
322
427
  const userId = scope === "user" ? requireUserId(ctx, "sharedDeckStorage.update") : null;
323
- await graphql(gql, UPDATE_SHARED_DECK_STORAGE_MUTATION, { deckId, sessionId: null, userId, updates: asUpdates(params.updates) }, "sharedDeckStorage.update");
428
+ stage(deckId, scope, userId, asUpdates(params.updates), gql);
429
+ if (params.unload === true)
430
+ await sendStaged(true);
324
431
  return { ok: true };
325
432
  }
326
433
  async function leaderboardFetch(ctx, params, caps) {
@@ -494,8 +601,8 @@ async function timeGetServerTime(gql) {
494
601
  castleEpochData: data.serverTime.castleEpochData,
495
602
  };
496
603
  }
497
- async function graphql(gql, query, variables, command) {
498
- const result = await gql(query, variables);
604
+ async function graphql(gql, query, variables, command, options) {
605
+ const result = await gql(query, variables, options);
499
606
  if (result.errors && result.errors.length > 0) {
500
607
  const first = result.errors[0];
501
608
  const code = first?.extensions?.code;
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.20';
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-30T21:21:38.191Z"
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-30T21:21:40.235Z"
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-30T21:21:42.774Z"
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.155",
3
+ "version": "0.4.157",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "castle-web": "./dist/index.js"