jssm 5.133.0 → 5.134.0

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/jssm.es6.d.ts CHANGED
@@ -582,6 +582,213 @@ declare type JssmHistory<mDT> = circular_buffer<[StateType$1, mDT]>;
582
582
  * made reproducible.
583
583
  */
584
584
  declare type JssmRng = () => number;
585
+ /**
586
+ * All event names that {@link Machine.on} accepts. These are observation
587
+ * events fired by the machine in addition to (not in place of) the hook
588
+ * system. Hooks intercept; events observe.
589
+ *
590
+ * @see Machine.on
591
+ */
592
+ declare type JssmEventName = 'transition' | 'rejection' | 'action' | 'entry' | 'exit' | 'terminal' | 'complete' | 'error' | 'data-change' | 'override' | 'timeout' | 'hook-registration' | 'hook-removal';
593
+ /**
594
+ * Detail payload fired with a `transition` event. Carries the resolved
595
+ * source and target, the action name (if the transition was driven by an
596
+ * action), the data observed before and after the change, the edge kind,
597
+ * and whether the call was a forced transition.
598
+ */
599
+ declare type JssmTransitionEventDetail<mDT> = {
600
+ from: StateType$1;
601
+ to: StateType$1;
602
+ action?: StateType$1;
603
+ data: mDT;
604
+ next_data?: mDT;
605
+ trans_type: string | undefined;
606
+ forced: boolean;
607
+ };
608
+ /**
609
+ * Detail payload fired with a `rejection` event. Carries the resolved
610
+ * source and target plus an indication of who rejected the transition
611
+ * and why. `reason` is `'invalid'` when no edge existed, `'hook'` when
612
+ * a hook handler vetoed; `hook_name` is set when `reason` is `'hook'`.
613
+ */
614
+ declare type JssmRejectionEventDetail<mDT> = {
615
+ from: StateType$1;
616
+ to: StateType$1;
617
+ action?: StateType$1;
618
+ data: mDT;
619
+ next_data?: mDT;
620
+ reason: 'invalid' | 'hook';
621
+ hook_name?: string;
622
+ forced: boolean;
623
+ };
624
+ /**
625
+ * Detail payload fired with an `action` event. Fires when an action is
626
+ * attempted, before transition validation runs.
627
+ */
628
+ declare type JssmActionEventDetail<mDT> = {
629
+ action: StateType$1;
630
+ from: StateType$1;
631
+ to?: StateType$1;
632
+ data: mDT;
633
+ next_data?: mDT;
634
+ };
635
+ /**
636
+ * Detail payload fired with an `entry` event. `state` is the entered
637
+ * state. `from` is the predecessor state, if any. `action` is the
638
+ * action that drove the entry, if any.
639
+ */
640
+ declare type JssmEntryEventDetail<mDT> = {
641
+ state: StateType$1;
642
+ from?: StateType$1;
643
+ action?: StateType$1;
644
+ data: mDT;
645
+ };
646
+ /**
647
+ * Detail payload fired with an `exit` event. `state` is the exited
648
+ * state. `to` is the next state, if any. `action` is the action that
649
+ * drove the exit, if any.
650
+ */
651
+ declare type JssmExitEventDetail<mDT> = {
652
+ state: StateType$1;
653
+ to?: StateType$1;
654
+ action?: StateType$1;
655
+ data: mDT;
656
+ };
657
+ /**
658
+ * Detail payload fired with a `terminal` event. Indicates that the
659
+ * machine has reached a state with no outgoing edges.
660
+ */
661
+ declare type JssmTerminalEventDetail<mDT> = {
662
+ state: StateType$1;
663
+ data: mDT;
664
+ };
665
+ /**
666
+ * Detail payload fired with a `complete` event. Indicates that the
667
+ * machine has reached a FSL `complete` state.
668
+ */
669
+ declare type JssmCompleteEventDetail<mDT> = {
670
+ state: StateType$1;
671
+ data: mDT;
672
+ };
673
+ /**
674
+ * Detail payload fired with an `error` event. Wraps an exception caught
675
+ * while running an event handler; `source_event` and `source_detail`
676
+ * identify the event whose handler threw, and `handler` is the offending
677
+ * function so consumers can correlate / blame.
678
+ */
679
+ declare type JssmErrorEventDetail = {
680
+ error: unknown;
681
+ source_event: JssmEventName;
682
+ source_detail: unknown;
683
+ handler: Function;
684
+ };
685
+ /**
686
+ * Detail payload fired with a `data-change` event. Fires whenever the
687
+ * machine's data payload is replaced. `old_data` is the value before the
688
+ * change; `new_data` is the value after.
689
+ */
690
+ declare type JssmDataChangeEventDetail<mDT> = {
691
+ from?: StateType$1;
692
+ to?: StateType$1;
693
+ action?: StateType$1;
694
+ old_data: mDT;
695
+ new_data: mDT;
696
+ cause: 'transition' | 'override';
697
+ };
698
+ /**
699
+ * Detail payload fired with an `override` event. Distinguishes a forced
700
+ * state replacement from a normal transition.
701
+ */
702
+ declare type JssmOverrideEventDetail<mDT> = {
703
+ from: StateType$1;
704
+ to: StateType$1;
705
+ old_data: mDT;
706
+ new_data?: mDT;
707
+ };
708
+ /**
709
+ * Detail payload fired with a `timeout` event. Fires when a configured
710
+ * `after` clause causes an automatic transition.
711
+ */
712
+ declare type JssmTimeoutEventDetail = {
713
+ from: StateType$1;
714
+ to: StateType$1;
715
+ after_time: number;
716
+ };
717
+ /**
718
+ * Detail payload fired with `hook-registration` and `hook-removal` events.
719
+ * Mirrors the {@link HookDescription} so inspector tools can mirror the
720
+ * current hook set.
721
+ */
722
+ declare type JssmHookLifecycleEventDetail<mDT> = {
723
+ description: HookDescription<mDT>;
724
+ };
725
+ /**
726
+ * Mapped type from {@link JssmEventName} to the corresponding detail
727
+ * payload. Drives the discriminated-union typing of {@link Machine.on},
728
+ * so `e.action` and friends only exist where they're meaningful.
729
+ */
730
+ declare type JssmEventDetailMap<mDT> = {
731
+ 'transition': JssmTransitionEventDetail<mDT>;
732
+ 'rejection': JssmRejectionEventDetail<mDT>;
733
+ 'action': JssmActionEventDetail<mDT>;
734
+ 'entry': JssmEntryEventDetail<mDT>;
735
+ 'exit': JssmExitEventDetail<mDT>;
736
+ 'terminal': JssmTerminalEventDetail<mDT>;
737
+ 'complete': JssmCompleteEventDetail<mDT>;
738
+ 'error': JssmErrorEventDetail;
739
+ 'data-change': JssmDataChangeEventDetail<mDT>;
740
+ 'override': JssmOverrideEventDetail<mDT>;
741
+ 'timeout': JssmTimeoutEventDetail;
742
+ 'hook-registration': JssmHookLifecycleEventDetail<mDT>;
743
+ 'hook-removal': JssmHookLifecycleEventDetail<mDT>;
744
+ };
745
+ /**
746
+ * Filter accepted by {@link Machine.on} / {@link Machine.once} for an
747
+ * individual event name. Only events whose detail key matches every
748
+ * filter entry fire the handler. Events that don't list a filter key in
749
+ * v1 take no filter properties.
750
+ */
751
+ declare type JssmEventFilterMap<mDT> = {
752
+ 'transition': {
753
+ from?: StateType$1;
754
+ to?: StateType$1;
755
+ };
756
+ 'rejection': Record<string, never>;
757
+ 'action': Record<string, never>;
758
+ 'entry': {
759
+ state?: StateType$1;
760
+ };
761
+ 'exit': {
762
+ state?: StateType$1;
763
+ };
764
+ 'terminal': Record<string, never>;
765
+ 'complete': Record<string, never>;
766
+ 'error': Record<string, never>;
767
+ 'data-change': Record<string, never>;
768
+ 'override': Record<string, never>;
769
+ 'timeout': Record<string, never>;
770
+ 'hook-registration': Record<string, never>;
771
+ 'hook-removal': Record<string, never>;
772
+ };
773
+ /**
774
+ * Per-event filter object (as passed to {@link Machine.on}). Use
775
+ * `JssmEventDetailMap<mDT>[Ev]` to find the matching detail type.
776
+ * @typeparam mDT The type of the machine data member.
777
+ * @typeparam Ev The event name.
778
+ */
779
+ declare type JssmEventFilter<mDT, Ev extends JssmEventName> = JssmEventFilterMap<mDT>[Ev];
780
+ /**
781
+ * Per-event handler signature. Receives a detail object typed by event
782
+ * name, so `e.action` (etc.) only exist where they're meaningful.
783
+ * @typeparam mDT The type of the machine data member.
784
+ * @typeparam Ev The event name.
785
+ */
786
+ declare type JssmEventHandler<mDT, Ev extends JssmEventName> = (detail: JssmEventDetailMap<mDT>[Ev]) => void;
787
+ /**
788
+ * Function returned by {@link Machine.on} and {@link Machine.once} that
789
+ * removes the subscription. Calling it more than once is a no-op.
790
+ */
791
+ declare type JssmUnsubscribe = () => void;
585
792
 
586
793
  /*********
587
794
  *
@@ -1116,6 +1323,18 @@ declare const action_label_chars: readonly {
1116
1323
  to: string;
1117
1324
  }[];
1118
1325
 
1326
+ /**
1327
+ * Internal record holding a single registered event subscription: the
1328
+ * handler, its optional filter, and a flag for `once` semantics. Not
1329
+ * exported.
1330
+ *
1331
+ * @internal
1332
+ */
1333
+ declare type JssmEventEntry<mDT, Ev extends JssmEventName> = {
1334
+ handler: JssmEventHandler<mDT, Ev>;
1335
+ filter?: JssmEventFilter<mDT, Ev>;
1336
+ once: boolean;
1337
+ };
1119
1338
  /*********
1120
1339
  *
1121
1340
  * An internal method meant to take a series of declarations and fold them into
@@ -1288,6 +1507,8 @@ declare class Machine<mDT> {
1288
1507
  _timeout_handle: number | undefined;
1289
1508
  _timeout_target: string | undefined;
1290
1509
  _timeout_target_time: number | undefined;
1510
+ _event_handlers: Map<JssmEventName, Set<JssmEventEntry<any, any>>>;
1511
+ _firing_error: boolean;
1291
1512
  constructor({ start_states, end_states, initial_state, start_states_no_enforce, complete, transitions, machine_author, machine_comment, machine_contributor, machine_definition, machine_language, machine_license, machine_name, machine_version, state_declaration, property_definition, state_property, fsl_version, dot_preamble, arrange_declaration, arrange_start_declaration, arrange_end_declaration, theme, flow, graph_layout, instance_name, history, data, default_state_config, default_active_state_config, default_hooked_state_config, default_terminal_state_config, default_start_state_config, default_end_state_config, allows_override, config_allows_override, rng_seed, time_source, timeout_source, clear_timeout_source }: JssmGenericConfig<StateType, mDT>);
1292
1513
  /********
1293
1514
  *
@@ -2096,6 +2317,100 @@ declare class Machine<mDT> {
2096
2317
  * @returns `true` if at least one state is complete.
2097
2318
  */
2098
2319
  has_completes(): boolean;
2320
+ /**
2321
+ * Subscribe to a typed observation event. Hooks (`set_hook` and friends)
2322
+ * intercept and may cancel a transition; events fire alongside the same
2323
+ * state-machine moments but cannot influence the outcome. This is the
2324
+ * surface most users actually want for "tell me when state changes".
2325
+ *
2326
+ * Handlers run synchronously, in registration order. A throwing handler
2327
+ * does not block subsequent handlers — its exception is caught and
2328
+ * re-emitted as an `error` event whose detail names the original event
2329
+ * and the offending handler.
2330
+ *
2331
+ * ```typescript
2332
+ * const m = sm`a -> b -> c;`;
2333
+ *
2334
+ * m.on('transition', e => console.log(`${e.from} -> ${e.to}`));
2335
+ * m.on('entry', { state: 'b' }, e => console.log(`entered ${e.state}`));
2336
+ *
2337
+ * const off = m.on('transition', () => {});
2338
+ * off(); // unsubscribe
2339
+ * ```
2340
+ *
2341
+ * @typeparam Ev The event name (drives the detail type).
2342
+ * @param name The event name to subscribe to.
2343
+ * @param filterOrFn Either a filter object or, when calling the no-filter
2344
+ * form, the handler itself.
2345
+ * @param maybeFn The handler, when a filter object was supplied.
2346
+ * @returns A function that unsubscribes when called.
2347
+ *
2348
+ * @see Machine.off
2349
+ * @see Machine.once
2350
+ */
2351
+ on<Ev extends JssmEventName>(name: Ev, handler: JssmEventHandler<mDT, Ev>): JssmUnsubscribe;
2352
+ on<Ev extends JssmEventName>(name: Ev, filter: JssmEventFilter<mDT, Ev>, handler: JssmEventHandler<mDT, Ev>): JssmUnsubscribe;
2353
+ /**
2354
+ * Subscribe to a typed observation event for one matching delivery, then
2355
+ * auto-remove. Accepts the same `(name, handler)` and `(name, filter,
2356
+ * handler)` shapes as {@link Machine.on}.
2357
+ *
2358
+ * ```typescript
2359
+ * m.once('terminal', e => console.log(`done at ${e.state}`));
2360
+ * ```
2361
+ *
2362
+ * @typeparam Ev The event name.
2363
+ * @param name The event name.
2364
+ * @param filterOrFn A filter object or the handler (no-filter form).
2365
+ * @param maybeFn The handler, when a filter was supplied.
2366
+ * @returns A function that unsubscribes early if called before the
2367
+ * handler has fired.
2368
+ *
2369
+ * @see Machine.on
2370
+ * @see Machine.off
2371
+ */
2372
+ once<Ev extends JssmEventName>(name: Ev, handler: JssmEventHandler<mDT, Ev>): JssmUnsubscribe;
2373
+ once<Ev extends JssmEventName>(name: Ev, filter: JssmEventFilter<mDT, Ev>, handler: JssmEventHandler<mDT, Ev>): JssmUnsubscribe;
2374
+ /**
2375
+ * Remove a previously-registered event handler. Match is by reference —
2376
+ * the same function value passed to {@link Machine.on} or
2377
+ * {@link Machine.once}. Returns `true` if a subscription was found and
2378
+ * removed, `false` otherwise.
2379
+ *
2380
+ * ```typescript
2381
+ * const fn = (e: any) => console.log(e);
2382
+ * m.on('transition', fn);
2383
+ * m.off('transition', fn); // true
2384
+ * m.off('transition', fn); // false
2385
+ * ```
2386
+ *
2387
+ * @param name The event name.
2388
+ * @param handler The handler reference to remove.
2389
+ * @returns `true` if removed, `false` if no match was registered.
2390
+ */
2391
+ off<Ev extends JssmEventName>(name: Ev, handler: JssmEventHandler<mDT, Ev>): boolean;
2392
+ /**
2393
+ * Shared registration core used by {@link Machine.on} and
2394
+ * {@link Machine.once}. Normalizes the optional filter argument and
2395
+ * installs the entry into the per-event subscription set.
2396
+ *
2397
+ * @internal
2398
+ */
2399
+ _subscribe<Ev extends JssmEventName>(name: Ev, filterOrFn: JssmEventFilter<mDT, Ev> | JssmEventHandler<mDT, Ev>, maybeFn: JssmEventHandler<mDT, Ev> | undefined, once: boolean): JssmUnsubscribe;
2400
+ /**
2401
+ * Dispatch an event to every registered subscriber in registration
2402
+ * order. Filters are checked first; non-matching handlers are skipped
2403
+ * without invoking the handler. Exceptions thrown by a handler are
2404
+ * caught and re-emitted as an `error` event so subsequent handlers
2405
+ * still run.
2406
+ *
2407
+ * Re-entry into the `error` event itself is guarded — if an `error`
2408
+ * handler throws, the new exception is swallowed rather than rebroadcast
2409
+ * to avoid an infinite loop.
2410
+ *
2411
+ * @internal
2412
+ */
2413
+ _fire<Ev extends JssmEventName>(name: Ev, detail: JssmEventDetailMap<mDT>[Ev]): void;
2099
2414
  /** Low-level hook registration. Installs a handler described by a
2100
2415
  * {@link HookDescription} into the appropriate internal map. Prefer the
2101
2416
  * convenience wrappers ({@link hook}, {@link hook_entry}, etc.) over
@@ -2103,6 +2418,28 @@ declare class Machine<mDT> {
2103
2418
  * @param HookDesc - A hook descriptor specifying kind, states, and handler.
2104
2419
  */
2105
2420
  set_hook(HookDesc: HookDescription<mDT>): void;
2421
+ /**
2422
+ * Remove a previously-registered hook described by a
2423
+ * {@link HookDescription}. Match is by `kind` + identifying keys
2424
+ * (`from`/`to`/`action`/etc.), not by handler reference — there is one
2425
+ * hook per slot in the registry, so the description uniquely identifies
2426
+ * which one to clear. Fires a `hook-removal` event for inspector tools.
2427
+ *
2428
+ * This is the symmetric counterpart of {@link Machine.set_hook} for the
2429
+ * event-bridging use case (#638). Reasoning about hooks via observation
2430
+ * events requires being able to observe their disappearance too.
2431
+ *
2432
+ * ```typescript
2433
+ * const m = sm`a -> b;`;
2434
+ * const fn = () => true;
2435
+ * m.set_hook({ kind: 'hook', from: 'a', to: 'b', handler: fn });
2436
+ * m.remove_hook({ kind: 'hook', from: 'a', to: 'b', handler: fn });
2437
+ * ```
2438
+ *
2439
+ * @param HookDesc - A hook descriptor identifying the hook to remove.
2440
+ * @returns `true` if a hook was removed, `false` otherwise.
2441
+ */
2442
+ remove_hook(HookDesc: HookDescription<mDT>): boolean;
2106
2443
  /** Register a pre-transition hook on a specific edge. Fires before
2107
2444
  * transitioning from `from` to `to`. If the handler returns `false`, the
2108
2445
  * transition is blocked.