@rohal12/spindle 0.59.21 → 0.59.22

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.
@@ -130,9 +130,10 @@ var NO_SELECTORS = {};
130
130
  var EMAIL_AUTOLINK = /^<[a-zA-Z0-9.!#$%&'*+/=?^_`|~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*>$/;
131
131
  /**
132
132
  * A link's display text and target, from its text `inner` (at `at` in the
133
- * input), with where the target is written.
133
+ * input), with where the target is written. `indexOf(separator)` finds a
134
+ * separator that is written outside markup (see linkSeparatorIndex).
134
135
  */
135
- function parseLink(inner, at) {
136
+ function parseLink(inner, at, indexOf) {
136
137
  /** The trimmed text from `start` to `end` of inner, and where it is. */
137
138
  const part = (start, end) => {
138
139
  const raw = inner.slice(start, end);
@@ -145,7 +146,7 @@ function parseLink(inner, at) {
145
146
  };
146
147
  };
147
148
  for (const [separator, targetFirst] of LINK_SEPARATORS) {
148
- const idx = inner.indexOf(separator);
149
+ const idx = indexOf(separator);
149
150
  if (idx === -1) continue;
150
151
  const before = part(0, idx);
151
152
  const after = part(idx + separator.length, inner.length);
@@ -827,7 +828,7 @@ function peg$parse(input, options) {
827
828
  return !textMode;
828
829
  }
829
830
  function peg$f50(s, at, inner) {
830
- return withSelectors(lexeme("link", location(), parseLink(inner, at)), s);
831
+ return withSelectors(lexeme("link", location(), parseLink(inner, at, (separator) => linkSeparatorIndex(inner, at, separator))), s);
831
832
  }
832
833
  function peg$f51() {
833
834
  return !textMode;
@@ -3730,6 +3731,24 @@ function peg$parse(input, options) {
3730
3731
  let attrCode = false;
3731
3732
  /** The macro whose raw body is being read. */
3732
3733
  let rawName = "";
3734
+ /**
3735
+ * The index in `inner` (at `at` in the input) of the first `separator` that
3736
+ * is not inside a {…} of the label: its code (`{if $a || $b}`, a string with
3737
+ * a pipe) belongs to the label, not to the link's syntax.
3738
+ */
3739
+ function linkSeparatorIndex(inner, at, separator) {
3740
+ for (let i = 0; i < inner.length; i++) {
3741
+ const c = inner.charAt(i);
3742
+ if (c === "\\" && /[{}]/.test(inner.charAt(i + 1))) i++;
3743
+ else if (c === LB) {
3744
+ const head = /^(?:[.#][\w-]*)* ?(\/?[a-zA-Z][\w-]*|(?=[$_@%(!]))/.exec(inner.slice(i + 1));
3745
+ if (!head) continue;
3746
+ const close = hooks.closeBrace(input, at + i + 1 + head[0].length, at + i + 1);
3747
+ if (close !== -1 && close < at + inner.length) i = close - at;
3748
+ } else if (inner.startsWith(separator, i)) return i;
3749
+ }
3750
+ return -1;
3751
+ }
3733
3752
  function skipTo(end) {
3734
3753
  skipEnd = end;
3735
3754
  return end >= 0;
@@ -14646,6 +14665,13 @@ function lsSet(key, value) {
14646
14665
  function lsDel(key) {
14647
14666
  localStorage.removeItem(key);
14648
14667
  }
14668
+ /** Put an item back as it was (`raw` null: absent); best effort. */
14669
+ function lsRestore(key, raw) {
14670
+ try {
14671
+ if (raw === null) localStorage.removeItem(key);
14672
+ else localStorage.setItem(key, raw);
14673
+ } catch {}
14674
+ }
14649
14675
  function lsIndex(key) {
14650
14676
  return lsGet(key) ?? [];
14651
14677
  }
@@ -14679,11 +14705,21 @@ function createLocalStorageBackend() {
14679
14705
  async put(key, value) {
14680
14706
  const previous = lsGet(`${prefix}${key}`);
14681
14707
  const lists = listsOf(value);
14682
- if (previous !== void 0) {
14683
- for (const list of listsOf(previous)) if (!lists.includes(list)) lsIndexRemove(list, key);
14708
+ const obsolete = previous === void 0 ? [] : listsOf(previous).filter((list) => !lists.includes(list));
14709
+ const touched = [
14710
+ `${prefix}${key}`,
14711
+ ...obsolete,
14712
+ ...lists
14713
+ ];
14714
+ const before = touched.map((k) => localStorage.getItem(k));
14715
+ try {
14716
+ for (const list of obsolete) lsIndexRemove(list, key);
14717
+ lsSet(`${prefix}${key}`, value);
14718
+ for (const list of lists) lsIndexAdd(list, key);
14719
+ } catch (err) {
14720
+ touched.forEach((k, i) => lsRestore(k, before[i] ?? null));
14721
+ throw err;
14684
14722
  }
14685
- lsSet(`${prefix}${key}`, value);
14686
- for (const list of lists) lsIndexAdd(list, key);
14687
14723
  },
14688
14724
  async delete(key) {
14689
14725
  const record = lsGet(`${prefix}${key}`);
@@ -15980,6 +16016,7 @@ async function createSaveNow(ifid, playthroughPromise, payload, custom = {}) {
15980
16016
  custom
15981
16017
  }, payload);
15982
16018
  await (await getBackend()).putSave(record);
16019
+ announceSlots(ifid);
15983
16020
  return record;
15984
16021
  }
15985
16022
  /**
@@ -16044,6 +16081,7 @@ function deleteSaveById(saveId) {
16044
16081
  await backend.deleteSave(saveId);
16045
16082
  if (!record) return;
16046
16083
  await emptySlotsHolding(record.meta.ifid, (id) => id === saveId);
16084
+ announceSlots(record.meta.ifid);
16047
16085
  });
16048
16086
  }
16049
16087
  function renameSave(saveId, newTitle) {
@@ -16061,6 +16099,7 @@ function renameSave(saveId, newTitle) {
16061
16099
  }
16062
16100
  };
16063
16101
  await backend.putSave(updated);
16102
+ announceSlots(updated.meta.ifid);
16064
16103
  });
16065
16104
  }
16066
16105
  var getSavesGrouped = queued(async function getSavesGroupedNow(ifid) {
@@ -16424,6 +16463,7 @@ function importSave(data, ifid) {
16424
16463
  return inOrder(async () => {
16425
16464
  const record = await prepareImport(data, ifid);
16426
16465
  await (await getBackend()).putSave(record);
16466
+ announceSlots(ifid);
16427
16467
  return record;
16428
16468
  });
16429
16469
  }
@@ -16513,6 +16553,7 @@ function deletePlaythroughData(ifid, playthroughId, replacement) {
16513
16553
  await backend.deletePlaythroughById(playthroughId);
16514
16554
  const deletedSet = new Set(deletedSaveIds);
16515
16555
  await emptySlotsHolding(ifid, (id) => deletedSet.has(id));
16556
+ announceSlots(ifid);
16516
16557
  if (replacement && await replacement.current === playthroughId) {
16517
16558
  await startNewPlaythroughNow(ifid, replacement.id);
16518
16559
  return true;
@@ -18366,8 +18407,8 @@ var useStoryStore = create()(storyStore((set, get) => ({
18366
18407
  return payload;
18367
18408
  };
18368
18409
  },
18369
- loadFromPayload: (payload, slot, playthroughId) => {
18370
- const applying = slotLoadApplying;
18410
+ loadFromPayload: (payload, slot, playthroughId, issued) => {
18411
+ const applying = slotLoadApplying ?? issued;
18371
18412
  slotLoadApplying = null;
18372
18413
  const leaving = slotLoadLeaving;
18373
18414
  slotLoadLeaving = null;
@@ -18606,6 +18647,7 @@ function useModalFocus(panelRef, bodySelector, dismissible, onClose) {
18606
18647
  } else next = backwards ? last : first;
18607
18648
  (next ?? (backwards ? last : first)).focus();
18608
18649
  };
18650
+ let steppingMedia = null;
18609
18651
  const onKeyDown = (e) => {
18610
18652
  if (!top()) return;
18611
18653
  if (e.key === "Escape") {
@@ -18616,8 +18658,19 @@ function useModalFocus(panelRef, bodySelector, dismissible, onClose) {
18616
18658
  return;
18617
18659
  }
18618
18660
  if (e.key !== "Tab") return;
18661
+ const active = document.activeElement;
18662
+ if (active instanceof HTMLMediaElement && active.controls) {
18663
+ steppingMedia = {
18664
+ el: active,
18665
+ backwards: e.shiftKey
18666
+ };
18667
+ return;
18668
+ }
18619
18669
  e.preventDefault();
18620
- moveFocus(document.activeElement, e.shiftKey);
18670
+ moveFocus(active, e.shiftKey);
18671
+ };
18672
+ const onPointerDown = () => {
18673
+ steppingMedia = null;
18621
18674
  };
18622
18675
  const controller = new AbortController();
18623
18676
  const { signal } = controller;
@@ -18660,11 +18713,28 @@ function useModalFocus(panelRef, bodySelector, dismissible, onClose) {
18660
18713
  attachFrames();
18661
18714
  const onFocusIn = (e) => {
18662
18715
  const target = e.target;
18716
+ if (steppingMedia && target !== steppingMedia.el && top()) {
18717
+ const { el, backwards } = steppingMedia;
18718
+ steppingMedia = null;
18719
+ moveFocus(el, backwards);
18720
+ return;
18721
+ }
18663
18722
  if (!top() || !(target instanceof Node) || panel.contains(target)) return;
18664
18723
  if (target === document.body || target === document.documentElement) return;
18665
18724
  moveFocus(null, false);
18666
18725
  };
18667
18726
  document.addEventListener("keydown", onKeyDown, { signal });
18727
+ document.addEventListener("pointerdown", onPointerDown, {
18728
+ capture: true,
18729
+ signal
18730
+ });
18731
+ const onFocusOut = (e) => {
18732
+ const pending = steppingMedia;
18733
+ if (!pending || e.target !== pending.el || e.relatedTarget || !top()) return;
18734
+ steppingMedia = null;
18735
+ moveFocus(pending.el, pending.backwards);
18736
+ };
18737
+ document.addEventListener("focusout", onFocusOut, { signal });
18668
18738
  document.addEventListener("focusin", onFocusIn, { signal });
18669
18739
  return () => {
18670
18740
  controller.abort();
@@ -21859,7 +21929,7 @@ function SaveManagerContent() {
21859
21929
  try {
21860
21930
  const current = await getSaveRecord(save.meta.id) ?? save;
21861
21931
  if (isStateReplacementSuperseded(replacement)) return;
21862
- loadFromPayload(decodeSavePayload(current.payload), void 0, current.meta.playthroughId);
21932
+ loadFromPayload(decodeSavePayload(current.payload), void 0, current.meta.playthroughId, replacement);
21863
21933
  showStatus("Game loaded");
21864
21934
  if (closeDialog) closeTimer.current = window.setTimeout(closeDialog, 500);
21865
21935
  await refresh();
@@ -75,9 +75,10 @@ var NO_SELECTORS = {};
75
75
  var EMAIL_AUTOLINK = /^<[a-zA-Z0-9.!#$%&'*+/=?^_`|~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*>$/;
76
76
  /**
77
77
  * A link's display text and target, from its text `inner` (at `at` in the
78
- * input), with where the target is written.
78
+ * input), with where the target is written. `indexOf(separator)` finds a
79
+ * separator that is written outside markup (see linkSeparatorIndex).
79
80
  */
80
- function parseLink(inner, at) {
81
+ function parseLink(inner, at, indexOf) {
81
82
  /** The trimmed text from `start` to `end` of inner, and where it is. */
82
83
  const part = (start, end) => {
83
84
  const raw = inner.slice(start, end);
@@ -90,7 +91,7 @@ function parseLink(inner, at) {
90
91
  };
91
92
  };
92
93
  for (const [separator, targetFirst] of LINK_SEPARATORS) {
93
- const idx = inner.indexOf(separator);
94
+ const idx = indexOf(separator);
94
95
  if (idx === -1) continue;
95
96
  const before = part(0, idx);
96
97
  const after = part(idx + separator.length, inner.length);
@@ -772,7 +773,7 @@ function peg$parse(input, options) {
772
773
  return !textMode;
773
774
  }
774
775
  function peg$f50(s, at, inner) {
775
- return withSelectors(lexeme("link", location(), parseLink(inner, at)), s);
776
+ return withSelectors(lexeme("link", location(), parseLink(inner, at, (separator) => linkSeparatorIndex(inner, at, separator))), s);
776
777
  }
777
778
  function peg$f51() {
778
779
  return !textMode;
@@ -3675,6 +3676,24 @@ function peg$parse(input, options) {
3675
3676
  let attrCode = false;
3676
3677
  /** The macro whose raw body is being read. */
3677
3678
  let rawName = "";
3679
+ /**
3680
+ * The index in `inner` (at `at` in the input) of the first `separator` that
3681
+ * is not inside a {…} of the label: its code (`{if $a || $b}`, a string with
3682
+ * a pipe) belongs to the label, not to the link's syntax.
3683
+ */
3684
+ function linkSeparatorIndex(inner, at, separator) {
3685
+ for (let i = 0; i < inner.length; i++) {
3686
+ const c = inner.charAt(i);
3687
+ if (c === "\\" && /[{}]/.test(inner.charAt(i + 1))) i++;
3688
+ else if (c === LB) {
3689
+ const head = /^(?:[.#][\w-]*)* ?(\/?[a-zA-Z][\w-]*|(?=[$_@%(!]))/.exec(inner.slice(i + 1));
3690
+ if (!head) continue;
3691
+ const close = hooks.closeBrace(input, at + i + 1 + head[0].length, at + i + 1);
3692
+ if (close !== -1 && close < at + inner.length) i = close - at;
3693
+ } else if (inner.startsWith(separator, i)) return i;
3694
+ }
3695
+ return -1;
3696
+ }
3678
3697
  function skipTo(end) {
3679
3698
  skipEnd = end;
3680
3699
  return end >= 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rohal12/spindle",
3
- "version": "0.59.21",
3
+ "version": "0.59.22",
4
4
  "type": "module",
5
5
  "description": "A Preact-based story format for Twine 2.",
6
6
  "license": "Unlicense",
@@ -188,6 +188,7 @@ export function SaveManagerContent() {
188
188
  decodeSavePayload(current.payload),
189
189
  undefined,
190
190
  current.meta.playthroughId,
191
+ replacement,
191
192
  );
192
193
  showStatus('Game loaded');
193
194
  if (closeDialog) {
@@ -227,6 +227,10 @@ export function useModalFocus(
227
227
  (next ?? (backwards ? last : first)).focus();
228
228
  };
229
229
 
230
+ // The media element whose native controls Tab is stepping through
231
+ let steppingMedia: { el: HTMLMediaElement; backwards: boolean } | null =
232
+ null;
233
+
230
234
  const onKeyDown = (e: KeyboardEvent) => {
231
235
  if (!top()) return;
232
236
 
@@ -239,8 +243,21 @@ export function useModalFocus(
239
243
  }
240
244
 
241
245
  if (e.key !== 'Tab') return;
246
+ // The controls of a media element are in its user-agent shadow tree,
247
+ // where the element stays the active one: they step through them
248
+ // natively, and onFocusIn takes over where focus leaves the element
249
+ // (#444).
250
+ const active = document.activeElement;
251
+ if (active instanceof HTMLMediaElement && active.controls) {
252
+ steppingMedia = { el: active, backwards: e.shiftKey };
253
+ return;
254
+ }
242
255
  e.preventDefault();
243
- moveFocus(document.activeElement, e.shiftKey);
256
+ moveFocus(active, e.shiftKey);
257
+ };
258
+ // Clicking elsewhere ends the stepping through the controls
259
+ const onPointerDown = () => {
260
+ steppingMedia = null;
244
261
  };
245
262
 
246
263
  // Key events inside an embedded document never reach this one (#425).
@@ -290,6 +307,13 @@ export function useModalFocus(
290
307
  // document can't be reached, is brought back in.
291
308
  const onFocusIn = (e: FocusEvent) => {
292
309
  const target = e.target;
310
+ if (steppingMedia && target !== steppingMedia.el && top()) {
311
+ // Focus left the media controls: on to the modal's next control
312
+ const { el, backwards } = steppingMedia;
313
+ steppingMedia = null;
314
+ moveFocus(el, backwards);
315
+ return;
316
+ }
293
317
  if (!top() || !(target instanceof Node) || panel.contains(target)) return;
294
318
  if (target === document.body || target === document.documentElement) {
295
319
  return;
@@ -298,6 +322,22 @@ export function useModalFocus(
298
322
  };
299
323
 
300
324
  document.addEventListener('keydown', onKeyDown, { signal });
325
+ document.addEventListener('pointerdown', onPointerDown, {
326
+ capture: true,
327
+ signal,
328
+ });
329
+ // Focus that leaves the media element for nothing (past its last control,
330
+ // with no tab stop after it in the document) sends no focusin: the
331
+ // modal's next control takes it at once.
332
+ const onFocusOut = (e: FocusEvent) => {
333
+ const pending = steppingMedia;
334
+ if (!pending || e.target !== pending.el || e.relatedTarget || !top()) {
335
+ return;
336
+ }
337
+ steppingMedia = null;
338
+ moveFocus(pending.el, pending.backwards);
339
+ };
340
+ document.addEventListener('focusout', onFocusOut, { signal });
301
341
  document.addEventListener('focusin', onFocusIn, { signal });
302
342
  return () => {
303
343
  controller.abort();
@@ -39,9 +39,10 @@ const EMAIL_AUTOLINK = /^<[a-zA-Z0-9.!#$%&'*+/=?^_`|~-]+@[a-zA-Z0-9](?:[a-zA-Z0-
39
39
 
40
40
  /**
41
41
  * A link's display text and target, from its text `inner` (at `at` in the
42
- * input), with where the target is written.
42
+ * input), with where the target is written. `indexOf(separator)` finds a
43
+ * separator that is written outside markup (see linkSeparatorIndex).
43
44
  */
44
- function parseLink(inner, at) {
45
+ function parseLink(inner, at, indexOf) {
45
46
  /** The trimmed text from `start` to `end` of inner, and where it is. */
46
47
  const part = (start, end) => {
47
48
  const raw = inner.slice(start, end);
@@ -50,7 +51,7 @@ function parseLink(inner, at) {
50
51
  return { value, start: from, end: from + value.length };
51
52
  };
52
53
  for (const [separator, targetFirst] of LINK_SEPARATORS) {
53
- const idx = inner.indexOf(separator);
54
+ const idx = indexOf(separator);
54
55
  if (idx === -1) continue;
55
56
  const before = part(0, idx);
56
57
  const after = part(idx + separator.length, inner.length);
@@ -168,6 +169,29 @@ let attrCode = false;
168
169
  /** The macro whose raw body is being read. */
169
170
  let rawName = '';
170
171
 
172
+ /**
173
+ * The index in `inner` (at `at` in the input) of the first `separator` that
174
+ * is not inside a {…} of the label: its code (`{if $a || $b}`, a string with
175
+ * a pipe) belongs to the label, not to the link's syntax.
176
+ */
177
+ function linkSeparatorIndex(inner, at, separator) {
178
+ for (let i = 0; i < inner.length; i++) {
179
+ const c = inner.charAt(i);
180
+ if (c === '\\' && /[{}]/.test(inner.charAt(i + 1))) {
181
+ i++;
182
+ } else if (c === LB) {
183
+ // Selectors, then a macro name or the sigil of a variable or expression
184
+ const head = /^(?:[.#][\w-]*)* ?(\/?[a-zA-Z][\w-]*|(?=[$_@%(!]))/.exec(inner.slice(i + 1));
185
+ if (!head) continue;
186
+ const close = hooks.closeBrace(input, at + i + 1 + head[0].length, at + i + 1);
187
+ // One that does not end inside the label is no markup of it
188
+ if (close !== -1 && close < at + inner.length) i = close - at;
189
+ } else if (inner.startsWith(separator, i)) {
190
+ return i;
191
+ }
192
+ }
193
+ return -1;
194
+ }
171
195
  function skipTo(end) {
172
196
  skipEnd = end;
173
197
  return end >= 0;
@@ -369,7 +393,7 @@ Literal
369
393
 
370
394
  LinkLexeme
371
395
  = &{ return !textMode; } "[[" s:Selectors at:Here inner:$LinkText "]]" {
372
- return withSelectors(lexeme('link', location(), parseLink(inner, at)), s);
396
+ return withSelectors(lexeme('link', location(), parseLink(inner, at, (separator) => linkSeparatorIndex(inner, at, separator))), s);
373
397
  }
374
398
  / &{ return !textMode; } "[[" { fail('Unclosed link: [[ without ]]', location()); }
375
399
 
@@ -337,6 +337,8 @@ async function createSaveNow(
337
337
 
338
338
  const record = storedRecord(meta, payload);
339
339
  await (await getBackend()).putSave(record);
340
+ // Save dialogs open in other tabs list it (#445)
341
+ announceSlots(ifid);
340
342
  return record;
341
343
  }
342
344
 
@@ -423,6 +425,8 @@ export function deleteSaveById(saveId: string): Promise<void> {
423
425
  if (!record) return;
424
426
 
425
427
  await emptySlotsHolding(record.meta.ifid, (id) => id === saveId);
428
+ // A save in no slot is gone from the other tabs' dialogs too (#445)
429
+ announceSlots(record.meta.ifid);
426
430
  });
427
431
  }
428
432
 
@@ -441,6 +445,7 @@ export function renameSave(saveId: string, newTitle: string): Promise<void> {
441
445
  },
442
446
  };
443
447
  await backend.putSave(updated);
448
+ announceSlots(updated.meta.ifid);
444
449
  });
445
450
  }
446
451
 
@@ -980,6 +985,7 @@ export function importSave(data: unknown, ifid: string): Promise<SaveRecord> {
980
985
  return inOrder(async () => {
981
986
  const record = await prepareImport(data, ifid);
982
987
  await (await getBackend()).putSave(record);
988
+ announceSlots(ifid);
983
989
  return record;
984
990
  });
985
991
  }
@@ -1105,6 +1111,8 @@ export function deletePlaythroughData(
1105
1111
  // Clear the slots that held deleted saves (pointer and slot index)
1106
1112
  const deletedSet = new Set(deletedSaveIds);
1107
1113
  await emptySlotsHolding(ifid, (id) => deletedSet.has(id));
1114
+ // Saves in no slot are gone from other tabs' dialogs too
1115
+ announceSlots(ifid);
1108
1116
 
1109
1117
  if (replacement && (await replacement.current) === playthroughId) {
1110
1118
  await startNewPlaythroughNow(ifid, replacement.id);
@@ -197,6 +197,16 @@ function lsDel(key: string): void {
197
197
  localStorage.removeItem(key);
198
198
  }
199
199
 
200
+ /** Put an item back as it was (`raw` null: absent); best effort. */
201
+ function lsRestore(key: string, raw: string | null): void {
202
+ try {
203
+ if (raw === null) localStorage.removeItem(key);
204
+ else localStorage.setItem(key, raw);
205
+ } catch {
206
+ // The quota forbids even the old value: nothing more to do
207
+ }
208
+ }
209
+
200
210
  function lsIndex(key: string): string[] {
201
211
  return lsGet<string[]>(key) ?? [];
202
212
  }
@@ -239,13 +249,22 @@ export function createLocalStorageBackend(): StorageBackend {
239
249
  // after a restart, under the new playthrough) leaves the old list
240
250
  const previous = lsGet<T>(`${prefix}${key}`);
241
251
  const lists = listsOf(value);
242
- if (previous !== undefined) {
243
- for (const list of listsOf(previous)) {
244
- if (!lists.includes(list)) lsIndexRemove(list, key);
245
- }
252
+ const obsolete =
253
+ previous === undefined
254
+ ? []
255
+ : listsOf(previous).filter((list) => !lists.includes(list));
256
+ // A write that fails (the quota) puts back what it changed: a record
257
+ // no index lists is neither shown nor cleared with its story (#443)
258
+ const touched = [`${prefix}${key}`, ...obsolete, ...lists];
259
+ const before = touched.map((k) => localStorage.getItem(k));
260
+ try {
261
+ for (const list of obsolete) lsIndexRemove(list, key);
262
+ lsSet(`${prefix}${key}`, value);
263
+ for (const list of lists) lsIndexAdd(list, key);
264
+ } catch (err) {
265
+ touched.forEach((k, i) => lsRestore(k, before[i] ?? null));
266
+ throw err;
246
267
  }
247
- lsSet(`${prefix}${key}`, value);
248
- for (const list of lists) lsIndexAdd(list, key);
249
268
  },
250
269
  async delete(key) {
251
270
  const record = lsGet<T>(`${prefix}${key}`);
package/src/store.ts CHANGED
@@ -989,11 +989,14 @@ export interface StoryState {
989
989
  * passed to the `beforeload`/`afterload` events. Pass the save's
990
990
  * `playthroughId` when loading a save: the game moves to that playthrough
991
991
  * (no change if it is the current one). Restoring the session passes none.
992
+ * `issued` is the place in the order of replacements a load that read
993
+ * before applying took at its call (see issueStateReplacement).
992
994
  */
993
995
  loadFromPayload: (
994
996
  payload: SavePayload,
995
997
  slot?: string,
996
998
  playthroughId?: string,
999
+ issued?: number,
997
1000
  ) => void;
998
1001
  getHistoryVariables: (index: number) => Record<string, unknown>;
999
1002
  setTransition: (config: TransitionConfig | null) => void;
@@ -1722,8 +1725,9 @@ export const useStoryStore = create<StoryState>()(
1722
1725
  payload: SavePayload,
1723
1726
  slot?: string,
1724
1727
  playthroughId?: string,
1728
+ issued?: number,
1725
1729
  ) => {
1726
- const applying = slotLoadApplying;
1730
+ const applying = slotLoadApplying ?? issued;
1727
1731
  slotLoadApplying = null;
1728
1732
  const leaving = slotLoadLeaving;
1729
1733
  slotLoadLeaving = null;