@rohal12/spindle 0.61.5 → 0.61.7

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/src/index.tsx CHANGED
@@ -101,9 +101,10 @@ function storeVarMacroNames(): string[] {
101
101
  function variableErrors(
102
102
  storyData: StoryData,
103
103
  schema: Parameters<typeof validatePassages>[1],
104
+ only: (passage: MarkupPassage) => boolean = () => true,
104
105
  ): string[] {
105
106
  return validatePassages(
106
- storyData.passages,
107
+ new Map([...storyData.passages].filter(([, passage]) => only(passage))),
107
108
  schema,
108
109
  storeVarMacroNames(),
109
110
  knownParameters(),
@@ -168,7 +169,7 @@ export function boot() {
168
169
  // External scripts can register custom macros (including block macros) here.
169
170
  document.dispatchEvent(new CustomEvent(':storystartup'));
170
171
 
171
- // Parse StoryVariables and validate all passages
172
+ // Parse StoryVariables and validate StoryInit before it executes
172
173
  let defaults: Record<string, unknown> = {};
173
174
  const storyVarsPassage = storyData.passages.get('StoryVariables');
174
175
 
@@ -192,7 +193,11 @@ export function boot() {
192
193
  setBlockWidget(name, block);
193
194
  }
194
195
 
195
- const errors = variableErrors(storyData, schema);
196
+ const errors = variableErrors(
197
+ storyData,
198
+ schema,
199
+ (p) => p.name === 'StoryInit',
200
+ );
196
201
  // StoryInit's markup must be valid before it runs; the other passages are
197
202
  // validated once it has run, as it may define macros (see below).
198
203
  errors.push(...markupErrors(storyData, (p) => p.name === 'StoryInit'));
package/src/store.ts CHANGED
@@ -765,17 +765,25 @@ export const subscribeStateReplacement = replacementListeners.subscribe;
765
765
  // Runtime handler cleanup (auto-unsub on restart)
766
766
  // ---------------------------------------------------------------------------
767
767
 
768
- let runtimeUnsubs: Array<() => void> = [];
768
+ let runtimeUnsubs = new Set<() => void>();
769
769
  let inRuntimePhase = false;
770
770
 
771
771
  /**
772
772
  * Track an unsubscribe function for automatic cleanup on restart.
773
- * No-op if called during the startup phase (before enterRuntimePhase).
773
+ * Return its manual cleanup, which also releases the tracked entry.
774
+ * Startup subscriptions (before enterRuntimePhase) survive restart.
774
775
  */
775
- export function trackRuntimeUnsub(unsub: () => void): void {
776
- if (inRuntimePhase) {
777
- runtimeUnsubs.push(unsub);
778
- }
776
+ export function trackRuntimeUnsub(unsub: () => void): () => void {
777
+ if (!inRuntimePhase) return unsub;
778
+ let cleanup: (() => void) | undefined = unsub;
779
+ const tracked = () => {
780
+ const call = cleanup;
781
+ cleanup = undefined;
782
+ runtimeUnsubs.delete(tracked);
783
+ call?.();
784
+ };
785
+ runtimeUnsubs.add(tracked);
786
+ return tracked;
779
787
  }
780
788
 
781
789
  /** Mark the start of the runtime phase. Called before executeStoryInit(). */
@@ -786,13 +794,13 @@ export function enterRuntimePhase(): void {
786
794
  /** Call all tracked unsubs and reset the runtime phase. */
787
795
  function cleanupRuntimeHandlers(): void {
788
796
  for (const unsub of runtimeUnsubs) unsub();
789
- runtimeUnsubs = [];
797
+ runtimeUnsubs.clear();
790
798
  inRuntimePhase = false;
791
799
  }
792
800
 
793
801
  /** Test-only: reset runtime phase state between tests. */
794
802
  export function _resetRuntimePhase(): void {
795
- runtimeUnsubs = [];
803
+ runtimeUnsubs = new Set();
796
804
  inRuntimePhase = false;
797
805
  }
798
806
 
@@ -1417,9 +1425,14 @@ export const useStoryStore = create<StoryState>()(
1417
1425
  }
1418
1426
 
1419
1427
  const previousPassage = get().currentPassage;
1420
- // A callback that restarted or loaded replaced the game this
1421
- // navigation was requested in: it does not carry on into the new one
1422
- if (replacesState(() => emit('beforenavigate', passageName))) return;
1428
+ // A callback may replace the game or move elsewhere. Like a history
1429
+ // traversal, this navigation must then give way to the callback's.
1430
+ const navigation = get().navigationId;
1431
+ if (
1432
+ replacesState(() => emit('beforenavigate', passageName)) ||
1433
+ get().navigationId !== navigation
1434
+ )
1435
+ return;
1423
1436
 
1424
1437
  // Compute variable delta before Immer set(). Read the variables after
1425
1438
  // beforenavigate so changes made by its handlers are recorded.
package/src/story-api.ts CHANGED
@@ -447,8 +447,7 @@ function createStoryAPI(): StoryAPI {
447
447
  ensureVariableChangedSubscription();
448
448
  }
449
449
  const unsub = emitterOn(event, callback);
450
- trackRuntimeUnsub(unsub);
451
- return unsub;
450
+ return trackRuntimeUnsub(unsub);
452
451
  },
453
452
 
454
453
  async waitForActions(): Promise<StoryAction[]> {
package/src/tooling.ts CHANGED
@@ -192,11 +192,11 @@ export function validateStoryMarkup(
192
192
  const all = [...macros];
193
193
  const known = new Set<string>();
194
194
  // The calls of widgets that declare what their arguments hold are checked
195
- const widgets = readWidgetDefinitions(list, parameterLookup(all));
195
+ const widgets = effectiveWidgetDefinitions(
196
+ readWidgetDefinitions(list, parameterLookup(all)),
197
+ );
196
198
  const blocks = new Set(
197
- effectiveWidgetDefinitions(widgets)
198
- .filter((def) => def.block)
199
- .map((def) => def.name.toLowerCase()),
199
+ widgets.filter((def) => def.block).map((def) => def.name.toLowerCase()),
200
200
  );
201
201
  const parametersOf = parameterLookup([
202
202
  ...all,
@@ -315,7 +315,9 @@ function referenceOptions(
315
315
  passages: readonly MarkupPassage[] = [],
316
316
  ) {
317
317
  const list = [...macros];
318
- const widgets = readWidgetDefinitions(passages, parameterLookup(list));
318
+ const widgets = effectiveWidgetDefinitions(
319
+ readWidgetDefinitions(passages, parameterLookup(list)),
320
+ );
319
321
  return {
320
322
  all,
321
323
  storeVarMacros: new Set(
@@ -100,7 +100,18 @@ export function setByPath(
100
100
  options: SetByPathOptions = {},
101
101
  ): void {
102
102
  const parent = walkToParent(root, segments, options.createMissing ?? false);
103
- parent[segments[segments.length - 1]!] = value;
103
+ const key = segments[segments.length - 1]!;
104
+ if (
105
+ value === 0 &&
106
+ isDraft(parent) &&
107
+ parent[key] === 0 &&
108
+ !Object.is(parent[key], value) &&
109
+ !(Array.isArray(parent) && key === 'length')
110
+ ) {
111
+ // Immer's assignment uses === after copying, losing a zero's changed sign.
112
+ parent[key] = undefined;
113
+ }
114
+ parent[key] = value;
104
115
  }
105
116
 
106
117
  /**