@tinyactors/scxmljs 0.1.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.
Files changed (89) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +174 -0
  3. package/THIRD_PARTY_NOTICES +63 -0
  4. package/dist/bridge.d.ts +44 -0
  5. package/dist/bridge.js +254 -0
  6. package/dist/bridge.js.map +1 -0
  7. package/dist/clock.d.ts +113 -0
  8. package/dist/clock.js +310 -0
  9. package/dist/clock.js.map +1 -0
  10. package/dist/common.d.ts +15 -0
  11. package/dist/common.js +14 -0
  12. package/dist/common.js.map +1 -0
  13. package/dist/datamodel-base.d.ts +122 -0
  14. package/dist/datamodel-base.js +268 -0
  15. package/dist/datamodel-base.js.map +1 -0
  16. package/dist/datamodel-quickjs.d.ts +21 -0
  17. package/dist/datamodel-quickjs.js +286 -0
  18. package/dist/datamodel-quickjs.js.map +1 -0
  19. package/dist/datamodel-trusted.d.ts +1 -0
  20. package/dist/datamodel-trusted.js +149 -0
  21. package/dist/datamodel-trusted.js.map +1 -0
  22. package/dist/diagnostics.d.ts +39 -0
  23. package/dist/diagnostics.js +154 -0
  24. package/dist/diagnostics.js.map +1 -0
  25. package/dist/element-events.d.ts +85 -0
  26. package/dist/element-events.js +129 -0
  27. package/dist/element-events.js.map +1 -0
  28. package/dist/events.d.ts +31 -0
  29. package/dist/events.js +33 -0
  30. package/dist/events.js.map +1 -0
  31. package/dist/explorer/element.d.ts +150 -0
  32. package/dist/explorer/element.js +1791 -0
  33. package/dist/explorer/element.js.map +1 -0
  34. package/dist/explorer/layout.d.ts +43 -0
  35. package/dist/explorer/layout.js +50 -0
  36. package/dist/explorer/layout.js.map +1 -0
  37. package/dist/explorer/strings.d.ts +275 -0
  38. package/dist/explorer/strings.js +135 -0
  39. package/dist/explorer/strings.js.map +1 -0
  40. package/dist/explorer/styles.d.ts +10 -0
  41. package/dist/explorer/styles.js +265 -0
  42. package/dist/explorer/styles.js.map +1 -0
  43. package/dist/explorer/viewmodel.d.ts +284 -0
  44. package/dist/explorer/viewmodel.js +481 -0
  45. package/dist/explorer/viewmodel.js.map +1 -0
  46. package/dist/explorer.d.ts +21 -0
  47. package/dist/explorer.js +22 -0
  48. package/dist/explorer.js.map +1 -0
  49. package/dist/index.d.ts +34 -0
  50. package/dist/index.js +39 -0
  51. package/dist/index.js.map +1 -0
  52. package/dist/model.d.ts +157 -0
  53. package/dist/model.js +485 -0
  54. package/dist/model.js.map +1 -0
  55. package/dist/session-events.d.ts +136 -0
  56. package/dist/session-events.js +114 -0
  57. package/dist/session-events.js.map +1 -0
  58. package/dist/session.d.ts +412 -0
  59. package/dist/session.js +1399 -0
  60. package/dist/session.js.map +1 -0
  61. package/dist/themes/tinyactors.css +44 -0
  62. package/dist/trusted.d.ts +15 -0
  63. package/dist/trusted.js +30 -0
  64. package/dist/trusted.js.map +1 -0
  65. package/dist/ui/announcer.d.ts +22 -0
  66. package/dist/ui/announcer.js +31 -0
  67. package/dist/ui/announcer.js.map +1 -0
  68. package/dist/ui/svg.d.ts +8 -0
  69. package/dist/ui/svg.js +29 -0
  70. package/dist/ui/svg.js.map +1 -0
  71. package/dist/ui/theme.d.ts +34 -0
  72. package/dist/ui/theme.js +151 -0
  73. package/dist/ui/theme.js.map +1 -0
  74. package/dist/view/element.d.ts +172 -0
  75. package/dist/view/element.js +1045 -0
  76. package/dist/view/element.js.map +1 -0
  77. package/dist/view/layout.d.ts +154 -0
  78. package/dist/view/layout.js +1122 -0
  79. package/dist/view/layout.js.map +1 -0
  80. package/dist/view/strings.d.ts +72 -0
  81. package/dist/view/strings.js +48 -0
  82. package/dist/view/strings.js.map +1 -0
  83. package/dist/view/styles.d.ts +1 -0
  84. package/dist/view/styles.js +115 -0
  85. package/dist/view/styles.js.map +1 -0
  86. package/dist/view.d.ts +20 -0
  87. package/dist/view.js +21 -0
  88. package/dist/view.js.map +1 -0
  89. package/package.json +87 -0
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Opt-in integration with the chart's own DOM: bubbling events on the source
3
+ * elements (`elementEvents`) and live attributes for CSS (`reflect`).
4
+ *
5
+ * Both are off by default: one compiled model can be shared by many sessions,
6
+ * and their events and attributes would collide on the same elements.
7
+ */
8
+ const ATTRIBUTES = ["data-active", "data-enabled", "data-fired", "data-initial", "data-status"];
9
+ /**
10
+ * Creates the event with the element's own realm, so it can be dispatched on
11
+ * elements from other windows (iframes, happy-dom) as well as the page's.
12
+ */
13
+ function makeEvent(target, type, fields) {
14
+ const Ctor = target.ownerDocument?.defaultView?.Event ?? Event;
15
+ const ev = new Ctor(type, { bubbles: true, composed: true });
16
+ for (const [k, v] of Object.entries(fields))
17
+ Object.defineProperty(ev, k, { value: v, enumerable: true });
18
+ return ev;
19
+ }
20
+ export class ElementBridge {
21
+ session;
22
+ model;
23
+ clock;
24
+ events;
25
+ reflect;
26
+ firedMs;
27
+ timers = new Map();
28
+ constructor(session, model, clock, events, reflect) {
29
+ this.session = session;
30
+ this.model = model;
31
+ this.clock = clock;
32
+ this.events = events;
33
+ this.reflect = reflect;
34
+ this.firedMs = reflect ? (reflect.firedMs ?? 900) : 0;
35
+ }
36
+ /** Before the first microstep. */
37
+ start() {
38
+ if (!this.reflect)
39
+ return;
40
+ for (const s of this.model.states)
41
+ for (const t of s.initial?.targets ?? [])
42
+ t.element.setAttribute("data-initial", "");
43
+ this.model.root.element.setAttribute("data-status", "running");
44
+ }
45
+ /** After each microstep, once the configuration is consistent. */
46
+ microstep(transitions, exited, entered, event) {
47
+ if (this.reflect) {
48
+ for (const s of exited)
49
+ s.element.removeAttribute("data-active");
50
+ for (const s of entered)
51
+ s.element.setAttribute("data-active", "");
52
+ this.syncEnabled();
53
+ for (const t of transitions)
54
+ if (t.element)
55
+ this.fire(t.element);
56
+ }
57
+ if (!this.events)
58
+ return;
59
+ for (const s of exited)
60
+ this.dispatch(s.element, "scxml:exit", { session: this.session, state: s, event });
61
+ for (const t of transitions)
62
+ if (t.element)
63
+ this.dispatch(t.element, "scxml:transition", { session: this.session, transition: t, event });
64
+ for (const s of entered)
65
+ this.dispatch(s.element, "scxml:enter", { session: this.session, state: s, event });
66
+ }
67
+ /** The session terminated. The last configuration stays visible; nothing is enabled any more. */
68
+ done(data) {
69
+ if (this.reflect) {
70
+ for (const s of this.model.states)
71
+ for (const t of s.transitions)
72
+ t.element?.removeAttribute("data-enabled");
73
+ this.model.root.element.setAttribute("data-status", "done");
74
+ }
75
+ if (this.events)
76
+ this.dispatch(this.model.root.element, "scxml:done", { session: this.session, data });
77
+ }
78
+ /** Remove every attribute this session set (on dispose/cancel). */
79
+ cleanup() {
80
+ if (!this.reflect)
81
+ return;
82
+ for (const t of this.timers.values())
83
+ this.clock.clearTimeout(t);
84
+ this.timers.clear();
85
+ const elements = new Set([this.model.root.element]);
86
+ for (const s of this.model.states) {
87
+ elements.add(s.element);
88
+ for (const t of s.transitions)
89
+ if (t.element)
90
+ elements.add(t.element);
91
+ }
92
+ for (const el of elements)
93
+ for (const a of ATTRIBUTES)
94
+ el.removeAttribute(a);
95
+ }
96
+ syncEnabled() {
97
+ for (const s of this.model.states) {
98
+ const on = this.session.isActiveNode(s);
99
+ for (const t of s.transitions) {
100
+ if (!t.element)
101
+ continue;
102
+ if (on)
103
+ t.element.setAttribute("data-enabled", "");
104
+ else
105
+ t.element.removeAttribute("data-enabled");
106
+ }
107
+ }
108
+ }
109
+ fire(el) {
110
+ el.setAttribute("data-fired", "");
111
+ const prev = this.timers.get(el);
112
+ if (prev !== undefined)
113
+ this.clock.clearTimeout(prev);
114
+ this.timers.set(el, this.clock.setTimeout(() => {
115
+ this.timers.delete(el);
116
+ el.removeAttribute("data-fired");
117
+ }, this.firedMs));
118
+ }
119
+ dispatch(target, type, fields) {
120
+ try {
121
+ target.dispatchEvent(makeEvent(target, type, fields));
122
+ }
123
+ catch (e) {
124
+ // a host bug must never corrupt a step
125
+ console.error(`scxml: error while dispatching ${type}:`, e);
126
+ }
127
+ }
128
+ }
129
+ //# sourceMappingURL=element-events.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"element-events.js","sourceRoot":"","sources":["../src/element-events.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AA+DH,MAAM,UAAU,GAAG,CAAC,aAAa,EAAE,cAAc,EAAE,YAAY,EAAE,cAAc,EAAE,aAAa,CAAU,CAAC;AAEzG;;;GAGG;AACH,SAAS,SAAS,CAAkB,MAAe,EAAE,IAAY,EAAE,MAA+B;IAChG,MAAM,IAAI,GAAkB,MAAM,CAAC,aAAa,EAAE,WAA+C,EAAE,KAAK,IAAI,KAAK,CAAC;IAClH,MAAM,EAAE,GAAG,IAAI,IAAI,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;IAC7D,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC;QAAE,MAAM,CAAC,cAAc,CAAC,EAAE,EAAE,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC;IAC1G,OAAO,EAAO,CAAC;AACjB,CAAC;AAED,MAAM,OAAO,aAAa;IAKL,OAAO;IACP,KAAK;IACL,KAAK;IACL,MAAM;IACN,OAAO;IART,OAAO,CAAS;IAChB,MAAM,GAAG,IAAI,GAAG,EAAoB,CAAC;IAEtD,YACmB,OAAqB,EACrB,KAAY,EACZ,KAAY,EACZ,MAAe,EACf,OAA+B;uBAJ/B,OAAO;qBACP,KAAK;qBACL,KAAK;sBACL,MAAM;uBACN,OAAO;QAExB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACxD,CAAC;IAED,kCAAkC;IAClC,KAAK;QACH,IAAI,CAAC,IAAI,CAAC,OAAO;YAAE,OAAO;QAC1B,KAA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C,CAAC;YACvB,EAAE,CAAC,eAAe,CAAC,YAAY,CAAC,CAAC;QACnC,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,CACjB,CAAC;IACJ,CAAC;IAEO,QAAQ,CAAC,MAAe,EAAE,IAA4B,EAAE,MAA+B;QAC7F,IAAI,CAAC;YACH,MAAM,CAAC,aAAa,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC;QACxD,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,uCAAuC;YACvC,OAAO,CAAC,KAAK,CAAC,kCAAkC,IAAI,GAAG,EAAE,CAAC,CAAC,CAAC;QAC9D,CAAC;IACH,CAAC;CACF","sourcesContent":["/**\n * Opt-in integration with the chart's own DOM: bubbling events on the source\n * elements (`elementEvents`) and live attributes for CSS (`reflect`).\n *\n * Both are off by default: one compiled model can be shared by many sessions,\n * and their events and attributes would collide on the same elements.\n */\n\nimport type { Clock } from \"./clock.ts\";\nimport type { SCXMLEvent } from \"./events.ts\";\nimport type { Model, StateNode, TransitionNode } from \"./model.ts\";\nimport type { SCXMLSession } from \"./session.ts\";\n\n/** `scxml:enter` / `scxml:exit`, dispatched on a state's source element. Bubbles, composed. */\nexport interface StateElementEvent extends Event {\n /** The session that entered or exited the state. */\n readonly session: SCXMLSession;\n /** The state, as a model node (its `element` is the event's target). */\n readonly state: StateNode;\n /** The SCXML event that triggered the microstep, if any. */\n readonly event?: SCXMLEvent;\n}\n\n/** `scxml:transition`, dispatched on a fired `<transition>` element. Bubbles, composed. */\nexport interface TransitionElementEvent extends Event {\n /** The session that took the transition. */\n readonly session: SCXMLSession;\n /** The transition, as a model node. */\n readonly transition: TransitionNode;\n /** The SCXML event that triggered it, if any (eventless transitions have none). */\n readonly event?: SCXMLEvent;\n}\n\n/** `scxml:done`, dispatched on the `<scxml>` element when the session terminates. Bubbles, composed. */\nexport interface ChartDoneElementEvent extends Event {\n /** The session that terminated. */\n readonly session: SCXMLSession;\n /** The top-level final state's `<donedata>`, if any. */\n readonly data: unknown;\n}\n\n/**\n * The events `elementEvents: true` dispatches on the chart's own elements.\n * Merged into the DOM's `ElementEventMap`, `DocumentEventMap` and\n * `WindowEventMap`, so `document.addEventListener(\"scxml:enter\", …)` is typed.\n */\nexport interface ElementEventsMap {\n /** A state was entered (on the state's element). */\n \"scxml:enter\": StateElementEvent;\n /** A state was exited (on the state's element). */\n \"scxml:exit\": StateElementEvent;\n /** A transition fired (on the `<transition>` element). */\n \"scxml:transition\": TransitionElementEvent;\n /** The session terminated (on the `<scxml>` element). */\n \"scxml:done\": ChartDoneElementEvent;\n}\n\ndeclare global {\n interface ElementEventMap extends ElementEventsMap {}\n interface DocumentEventMap extends ElementEventsMap {}\n interface WindowEventMap extends ElementEventsMap {}\n}\n\n/** Options for `SessionOptions.reflect` (or pass `true` for the defaults). */\nexport interface ReflectOptions {\n /** How long `data-fired` stays on a transition element (session clock, ms). Default 900. */\n firedMs?: number;\n}\n\nconst ATTRIBUTES = [\"data-active\", \"data-enabled\", \"data-fired\", \"data-initial\", \"data-status\"] as const;\n\n/**\n * Creates the event with the element's own realm, so it can be dispatched on\n * elements from other windows (iframes, happy-dom) as well as the page's.\n */\nfunction makeEvent<T extends Event>(target: Element, type: string, fields: Record<string, unknown>): T {\n const Ctor: typeof Event = (target.ownerDocument?.defaultView as { Event?: typeof Event } | null)?.Event ?? Event;\n const ev = new Ctor(type, { bubbles: true, composed: true });\n for (const [k, v] of Object.entries(fields)) Object.defineProperty(ev, k, { value: v, enumerable: true });\n return ev as T;\n}\n\nexport class ElementBridge {\n private readonly firedMs: number;\n private readonly timers = new Map<Element, unknown>();\n\n constructor(\n private readonly session: SCXMLSession,\n private readonly model: Model,\n private readonly clock: Clock,\n private readonly events: boolean,\n private readonly reflect: false | ReflectOptions,\n ) {\n this.firedMs = reflect ? (reflect.firedMs ?? 900) : 0;\n }\n\n /** Before the first microstep. */\n start() {\n if (!this.reflect) return;\n for (const s of this.model.states) for (const t of s.initial?.targets ?? []) t.element.setAttribute(\"data-initial\", \"\");\n this.model.root.element.setAttribute(\"data-status\", \"running\");\n }\n\n /** After each microstep, once the configuration is consistent. */\n microstep(transitions: TransitionNode[], exited: StateNode[], entered: StateNode[], event: SCXMLEvent | undefined) {\n if (this.reflect) {\n for (const s of exited) s.element.removeAttribute(\"data-active\");\n for (const s of entered) s.element.setAttribute(\"data-active\", \"\");\n this.syncEnabled();\n for (const t of transitions) if (t.element) this.fire(t.element);\n }\n if (!this.events) return;\n for (const s of exited) this.dispatch(s.element, \"scxml:exit\", { session: this.session, state: s, event });\n for (const t of transitions)\n if (t.element) this.dispatch(t.element, \"scxml:transition\", { session: this.session, transition: t, event });\n for (const s of entered) this.dispatch(s.element, \"scxml:enter\", { session: this.session, state: s, event });\n }\n\n /** The session terminated. The last configuration stays visible; nothing is enabled any more. */\n done(data: unknown) {\n if (this.reflect) {\n for (const s of this.model.states) for (const t of s.transitions) t.element?.removeAttribute(\"data-enabled\");\n this.model.root.element.setAttribute(\"data-status\", \"done\");\n }\n if (this.events) this.dispatch(this.model.root.element, \"scxml:done\", { session: this.session, data });\n }\n\n /** Remove every attribute this session set (on dispose/cancel). */\n cleanup() {\n if (!this.reflect) return;\n for (const t of this.timers.values()) this.clock.clearTimeout(t);\n this.timers.clear();\n const elements = new Set<Element>([this.model.root.element]);\n for (const s of this.model.states) {\n elements.add(s.element);\n for (const t of s.transitions) if (t.element) elements.add(t.element);\n }\n for (const el of elements) for (const a of ATTRIBUTES) el.removeAttribute(a);\n }\n\n private syncEnabled() {\n for (const s of this.model.states) {\n const on = this.session.isActiveNode(s);\n for (const t of s.transitions) {\n if (!t.element) continue;\n if (on) t.element.setAttribute(\"data-enabled\", \"\");\n else t.element.removeAttribute(\"data-enabled\");\n }\n }\n }\n\n private fire(el: Element) {\n el.setAttribute(\"data-fired\", \"\");\n const prev = this.timers.get(el);\n if (prev !== undefined) this.clock.clearTimeout(prev);\n this.timers.set(\n el,\n this.clock.setTimeout(() => {\n this.timers.delete(el);\n el.removeAttribute(\"data-fired\");\n }, this.firedMs),\n );\n }\n\n private dispatch(target: Element, type: keyof ElementEventsMap, fields: Record<string, unknown>) {\n try {\n target.dispatchEvent(makeEvent(target, type, fields));\n } catch (e) {\n // a host bug must never corrupt a step\n console.error(`scxml: error while dispatching ${type}:`, e);\n }\n }\n}\n"]}
@@ -0,0 +1,31 @@
1
+ /** Shared constants and the SCXML event shape. */
2
+ export declare const SCXML_NS = "http://www.w3.org/2005/07/scxml";
3
+ /** The SCXML Event I/O Processor type URI (spec §C.1). */
4
+ export declare const SCXML_IOPROCESSOR = "http://www.w3.org/TR/scxml/#SCXMLEventProcessor";
5
+ /** Types accepted by `<invoke>` for SCXML child sessions. */
6
+ export declare const SCXML_INVOKE_TYPES: Set<string>;
7
+ /** The `_event` object (spec §5.10.1). Every field is always present. */
8
+ export interface SCXMLEvent {
9
+ /** The event name, for example `"issues.opened"` or `"done.state.checkout"`. */
10
+ name: string;
11
+ /** `platform` for events the interpreter raises (`done.*`, `error.*`), `internal` for `<raise>` and `#_internal`, `external` for everything else. */
12
+ type: "platform" | "internal" | "external";
13
+ /** The `id` (or generated id) of the `<send>` that produced the event; also set on errors raised by a `<send>`. */
14
+ sendid?: string;
15
+ /** Where to reply: for the SCXML processor, `#_scxml_<sessionid>` of the sender. */
16
+ origin?: string;
17
+ /** The type URI of the I/O processor the event came through. */
18
+ origintype?: string;
19
+ /** Set when the event comes from an invoked child session or service. */
20
+ invokeid?: string;
21
+ /** The payload: namelist/`<param>` values, `<content>`, `<donedata>`, or what the host passed to `send()`. */
22
+ data?: unknown;
23
+ }
24
+ /**
25
+ * Event descriptor matching (spec §3.12.1): a descriptor matches an event
26
+ * name if its tokens are a prefix of the name's tokens. "*" matches every
27
+ * event; a trailing ".*" is ignored.
28
+ */
29
+ export declare function nameMatch(descriptors: readonly string[], name: string): boolean;
30
+ /** CSS2 time values: "1.5s", "250ms". Returns milliseconds, or NaN when invalid. */
31
+ export declare function parseDelay(v: unknown): number;
package/dist/events.js ADDED
@@ -0,0 +1,33 @@
1
+ /** Shared constants and the SCXML event shape. */
2
+ export const SCXML_NS = "http://www.w3.org/2005/07/scxml";
3
+ /** The SCXML Event I/O Processor type URI (spec §C.1). */
4
+ export const SCXML_IOPROCESSOR = "http://www.w3.org/TR/scxml/#SCXMLEventProcessor";
5
+ /** Types accepted by `<invoke>` for SCXML child sessions. */
6
+ export const SCXML_INVOKE_TYPES = new Set(["scxml", "http://www.w3.org/TR/scxml/", "http://www.w3.org/TR/scxml"]);
7
+ /**
8
+ * Event descriptor matching (spec §3.12.1): a descriptor matches an event
9
+ * name if its tokens are a prefix of the name's tokens. "*" matches every
10
+ * event; a trailing ".*" is ignored.
11
+ */
12
+ export function nameMatch(descriptors, name) {
13
+ const tokens = name.split(".");
14
+ return descriptors.some((d) => {
15
+ if (d === "*")
16
+ return true;
17
+ const want = d.replace(/\.\*$/, "").replace(/\.$/, "").split(".");
18
+ if (want.length > tokens.length)
19
+ return false;
20
+ return want.every((t, i) => t === tokens[i]);
21
+ });
22
+ }
23
+ /** CSS2 time values: "1.5s", "250ms". Returns milliseconds, or NaN when invalid. */
24
+ export function parseDelay(v) {
25
+ if (typeof v === "number")
26
+ return v >= 0 ? v : NaN;
27
+ const m = /^\s*(\d*\.?\d+)\s*(ms|s)\s*$/.exec(String(v ?? ""));
28
+ if (!m)
29
+ return NaN;
30
+ const n = parseFloat(m[1]);
31
+ return m[2] === "s" ? n * 1000 : n;
32
+ }
33
+ //# sourceMappingURL=events.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.js","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAAA,kDAAkD;AAElD,MAAM,CAAC,MAAM,QAAQ,GAAG,iCAAiC,CAAC;AAC1D,0DAA0D;AAC1D,MAAM,CAAC,MAAM,iBAAiB,GAAG,iDAAiD,CAAC;AACnF,6DAA6D;AAC7D,MAAM,CAAC,MAAM,kBAAkB,GAAG,IAAI,GAAG,C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":["/** Shared constants and the SCXML event shape. */\n\nexport const SCXML_NS = \"http://www.w3.org/2005/07/scxml\";\n/** The SCXML Event I/O Processor type URI (spec §C.1). */\nexport const SCXML_IOPROCESSOR = \"http://www.w3.org/TR/scxml/#SCXMLEventProcessor\";\n/** Types accepted by `<invoke>` for SCXML child sessions. */\nexport const SCXML_INVOKE_TYPES = new Set([\"scxml\", \"http://www.w3.org/TR/scxml/\", \"http://www.w3.org/TR/scxml\"]);\n\n/** The `_event` object (spec §5.10.1). Every field is always present. */\nexport interface SCXMLEvent {\n /** The event name, for example `\"issues.opened\"` or `\"done.state.checkout\"`. */\n name: string;\n /** `platform` for events the interpreter raises (`done.*`, `error.*`), `internal` for `<raise>` and `#_internal`, `external` for everything else. */\n type: \"platform\" | \"internal\" | \"external\";\n /** The `id` (or generated id) of the `<send>` that produced the event; also set on errors raised by a `<send>`. */\n sendid?: string;\n /** Where to reply: for the SCXML processor, `#_scxml_<sessionid>` of the sender. */\n origin?: string;\n /** The type URI of the I/O processor the event came through. */\n origintype?: string;\n /** Set when the event comes from an invoked child session or service. */\n invokeid?: string;\n /** The payload: namelist/`<param>` values, `<content>`, `<donedata>`, or what the host passed to `send()`. */\n data?: unknown;\n}\n\n/**\n * Event descriptor matching (spec §3.12.1): a descriptor matches an event\n * name if its tokens are a prefix of the name's tokens. \"*\" matches every\n * event; a trailing \".*\" is ignored.\n */\nexport function nameMatch(descriptors: readonly string[], name: string): boolean {\n const tokens = name.split(\".\");\n return descriptors.some((d) => {\n if (d === \"*\") return true;\n const want = d.replace(/\\.\\*$/, \"\").replace(/\\.$/, \"\").split(\".\");\n if (want.length > tokens.length) return false;\n return want.every((t, i) => t === tokens[i]);\n });\n}\n\n/** CSS2 time values: \"1.5s\", \"250ms\". Returns milliseconds, or NaN when invalid. */\nexport function parseDelay(v: unknown): number {\n if (typeof v === \"number\") return v >= 0 ? v : NaN;\n const m = /^\\s*(\\d*\\.?\\d+)\\s*(ms|s)\\s*$/.exec(String(v ?? \"\"));\n if (!m) return NaN;\n const n = parseFloat(m[1]!);\n return m[2] === \"s\" ? n * 1000 : n;\n}\n"]}
@@ -0,0 +1,150 @@
1
+ /**
2
+ * <scxml-explorer> — explore a running SCXML system at C4-style levels:
3
+ *
4
+ * System machines (root + invoked children) and external services, and who talks to whom
5
+ * Machine one machine: a tree of its states, a FOCUS on one compound state
6
+ * (its children and the transitions between them — never the whole
7
+ * chart), doors in/out of the focus, and the events accepted right now
8
+ * State detail for one state: actions, transitions, invokes, data
9
+ *
10
+ * No canvas, no pan/zoom: navigation is tree + breadcrumbs + drill-in, and the
11
+ * layout collapses to one pane with bottom tabs in narrow containers.
12
+ * Big charts: the tree is windowed, lists are grouped/filterable and cut off
13
+ * with "+N more", the focus switches from diagram to list when it gets crowded.
14
+ *
15
+ * Custom UI goes in through named slots (works with any framework):
16
+ * state:<id> in that state's card
17
+ * detail:<id> in that state's detail pane
18
+ * event:<descriptor> in the row of that accepted event (e.g. a form for its data)
19
+ * service:<alias> in that service's card at the System level
20
+ * toolbar in the header, before the Follow toggle
21
+ * empty-tree, empty-events, empty-detail replace the empty-state messages
22
+ *
23
+ * const x = document.querySelector("scxml-explorer");
24
+ * x.attach({ session, processors, clock }); // then session.start()
25
+ *
26
+ * Properties: `session`, `processors`, `clock` (setting one re-attaches),
27
+ * `follow`, `strings` (translations), `announce`, `announceInterval`.
28
+ * Attributes: `follow="false"` starts without following; `announce="off|sends|all"`.
29
+ *
30
+ * Keyboard: the tree follows the ARIA tree pattern (↑ ↓ ← → Home End, Enter,
31
+ * type-ahead); cards, rows, lanes and doors are focusable (Enter drills into a
32
+ * container or selects a state, Space selects); Space / "." play-pause and step
33
+ * when a PlaybackClock is attached. A polite live region announces steps and
34
+ * sent events.
35
+ * Events (bubbling, composed): `scxml-focus`, `scxml-select`, `scxml-send`
36
+ * (cancelable: `preventDefault()` stops the event from being sent).
37
+ * Styling: `--scxml-*` custom properties and `::part()` — see the README.
38
+ */
39
+ import { type Clock } from "../clock.js";
40
+ import { type StateNode } from "../model.js";
41
+ import type { IOProcessor, SCXMLSession as Session } from "../session.js";
42
+ import { type ExplorerStrings } from "./strings.js";
43
+ /** What the live region announces: every step and sent event, only sent events, or nothing. */
44
+ export type AnnounceMode = "all" | "sends" | "off";
45
+ /** What `attach()` (or the `session` / `processors` / `clock` properties) takes. */
46
+ export interface ExplorerAttachOptions {
47
+ /** The root session to show. Invoked child sessions are discovered automatically. */
48
+ session: Session;
49
+ /** The I/O processors the session was created with (shown at the System level). */
50
+ processors?: IOProcessor[];
51
+ /** The session's clock. A `PlaybackClock` adds play / pause / step / speed controls. Defaults to `session.clock`. */
52
+ clock?: Clock;
53
+ }
54
+ /** `scxml-focus`: the focused state changed (user navigation or follow). */
55
+ export interface ExplorerFocusDetail {
56
+ /** the session of the machine being shown (the root or an invoked child) */
57
+ session: Session;
58
+ /** the focused container state (`<scxml>`, compound or parallel) */
59
+ state: StateNode;
60
+ }
61
+ /** `scxml-select`: a state was selected for the detail pane. */
62
+ export interface ExplorerSelectDetail {
63
+ /** the session of the machine the state belongs to */
64
+ session: Session;
65
+ /** the selected state */
66
+ state: StateNode;
67
+ }
68
+ /** `scxml-send`: the UI is about to send an event. Cancelable. */
69
+ export interface ExplorerSendDetail {
70
+ /** the session the event goes to */
71
+ session: Session;
72
+ /** the event name */
73
+ name: string;
74
+ /** the event data (from the data field, parsed as JSON), or `undefined` */
75
+ data: unknown;
76
+ }
77
+ /**
78
+ * Server-side rendering and tests import this module where there is no DOM:
79
+ * extend a stand-in then, and only register the element where it can exist.
80
+ */
81
+ declare const ElementBase: typeof HTMLElement;
82
+ /**
83
+ * `<scxml-explorer>`: explore a running system at the System level (machines,
84
+ * services, messages), the Machine level (a tree, a focus on one compound
85
+ * state, the accepted events) and per state. Registered by importing
86
+ * `@tinyactors/scxmljs/explorer`.
87
+ *
88
+ * @example
89
+ * ```ts
90
+ * import { createSession } from "@tinyactors/scxmljs/trusted";
91
+ * import "@tinyactors/scxmljs/explorer";
92
+ *
93
+ * const session = await createSession(source);
94
+ * document.querySelector("scxml-explorer")!.attach({ session });
95
+ * session.start();
96
+ * ```
97
+ */
98
+ export declare class ScxmlExplorer extends ElementBase {
99
+ #private;
100
+ /** Attributes the element reacts to: `follow` and `announce`. */
101
+ static observedAttributes: string[];
102
+ constructor();
103
+ /** Custom-element lifecycle (called by the browser): detaches, keeping the session to re-attach on reconnect. */
104
+ disconnectedCallback(): void;
105
+ /** Custom-element lifecycle (called by the browser): starts measuring its width and re-attaches after a move. */
106
+ connectedCallback(): void;
107
+ /** Custom-element lifecycle (called by the browser) for `follow` and `announce`. */
108
+ attributeChangedCallback(name: string, _old: string | null, value: string | null): void;
109
+ /**
110
+ * Every user-visible string. Set a partial object to translate or relabel;
111
+ * unset keys keep their English default. See `ExplorerStrings` for the keys.
112
+ */
113
+ get strings(): ExplorerStrings;
114
+ /** Translate: a partial object; unset keys keep their English default. */
115
+ set strings(strings: Partial<ExplorerStrings>);
116
+ /** What the polite live region announces: `"all"` (steps and sent events, default), `"sends"` or `"off"`. */
117
+ get announce(): AnnounceMode;
118
+ /** Sets the `announce` attribute. */
119
+ set announce(mode: AnnounceMode);
120
+ /** While a clock is playing, announce steps at most this often (ms of real time). Default 3000. */
121
+ get announceInterval(): number;
122
+ /** Minimum real-time gap between step announcements while playing, in ms. */
123
+ set announceInterval(ms: number);
124
+ /** The root session shown. Setting it attaches (with the current `processors` and `clock`). */
125
+ get session(): Session | undefined;
126
+ /** Attach this session (or detach with `undefined`). */
127
+ set session(session: Session | undefined);
128
+ /** I/O processors to show at the System level. */
129
+ get processors(): IOProcessor[];
130
+ /** The I/O processors to show; re-attaches if a session is set. */
131
+ set processors(processors: IOProcessor[]);
132
+ /** The session's clock; a `PlaybackClock` gets playback controls. */
133
+ get clock(): Clock | undefined;
134
+ /** The clock to control; re-attaches if a session is set. */
135
+ set clock(clock: Clock | undefined);
136
+ /** Keep the focus on the part of the machine that is moving. Reflected as `follow="true|false"`. */
137
+ get follow(): boolean;
138
+ /** Turn following on or off (also reflected as the `follow` attribute). */
139
+ set follow(on: boolean);
140
+ /** Show a (root) session. Call before or after it starts. */
141
+ attach({ session, processors, clock }: ExplorerAttachOptions): void;
142
+ /** Stop showing the session and release all listeners (the session itself is left alone). */
143
+ detach(): void;
144
+ }
145
+ declare global {
146
+ interface HTMLElementTagNameMap {
147
+ "scxml-explorer": ScxmlExplorer;
148
+ }
149
+ }
150
+ export {};