@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.
- package/LICENSE +21 -0
- package/README.md +174 -0
- package/THIRD_PARTY_NOTICES +63 -0
- package/dist/bridge.d.ts +44 -0
- package/dist/bridge.js +254 -0
- package/dist/bridge.js.map +1 -0
- package/dist/clock.d.ts +113 -0
- package/dist/clock.js +310 -0
- package/dist/clock.js.map +1 -0
- package/dist/common.d.ts +15 -0
- package/dist/common.js +14 -0
- package/dist/common.js.map +1 -0
- package/dist/datamodel-base.d.ts +122 -0
- package/dist/datamodel-base.js +268 -0
- package/dist/datamodel-base.js.map +1 -0
- package/dist/datamodel-quickjs.d.ts +21 -0
- package/dist/datamodel-quickjs.js +286 -0
- package/dist/datamodel-quickjs.js.map +1 -0
- package/dist/datamodel-trusted.d.ts +1 -0
- package/dist/datamodel-trusted.js +149 -0
- package/dist/datamodel-trusted.js.map +1 -0
- package/dist/diagnostics.d.ts +39 -0
- package/dist/diagnostics.js +154 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/element-events.d.ts +85 -0
- package/dist/element-events.js +129 -0
- package/dist/element-events.js.map +1 -0
- package/dist/events.d.ts +31 -0
- package/dist/events.js +33 -0
- package/dist/events.js.map +1 -0
- package/dist/explorer/element.d.ts +150 -0
- package/dist/explorer/element.js +1791 -0
- package/dist/explorer/element.js.map +1 -0
- package/dist/explorer/layout.d.ts +43 -0
- package/dist/explorer/layout.js +50 -0
- package/dist/explorer/layout.js.map +1 -0
- package/dist/explorer/strings.d.ts +275 -0
- package/dist/explorer/strings.js +135 -0
- package/dist/explorer/strings.js.map +1 -0
- package/dist/explorer/styles.d.ts +10 -0
- package/dist/explorer/styles.js +265 -0
- package/dist/explorer/styles.js.map +1 -0
- package/dist/explorer/viewmodel.d.ts +284 -0
- package/dist/explorer/viewmodel.js +481 -0
- package/dist/explorer/viewmodel.js.map +1 -0
- package/dist/explorer.d.ts +21 -0
- package/dist/explorer.js +22 -0
- package/dist/explorer.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +39 -0
- package/dist/index.js.map +1 -0
- package/dist/model.d.ts +157 -0
- package/dist/model.js +485 -0
- package/dist/model.js.map +1 -0
- package/dist/session-events.d.ts +136 -0
- package/dist/session-events.js +114 -0
- package/dist/session-events.js.map +1 -0
- package/dist/session.d.ts +412 -0
- package/dist/session.js +1399 -0
- package/dist/session.js.map +1 -0
- package/dist/themes/tinyactors.css +44 -0
- package/dist/trusted.d.ts +15 -0
- package/dist/trusted.js +30 -0
- package/dist/trusted.js.map +1 -0
- package/dist/ui/announcer.d.ts +22 -0
- package/dist/ui/announcer.js +31 -0
- package/dist/ui/announcer.js.map +1 -0
- package/dist/ui/svg.d.ts +8 -0
- package/dist/ui/svg.js +29 -0
- package/dist/ui/svg.js.map +1 -0
- package/dist/ui/theme.d.ts +34 -0
- package/dist/ui/theme.js +151 -0
- package/dist/ui/theme.js.map +1 -0
- package/dist/view/element.d.ts +172 -0
- package/dist/view/element.js +1045 -0
- package/dist/view/element.js.map +1 -0
- package/dist/view/layout.d.ts +154 -0
- package/dist/view/layout.js +1122 -0
- package/dist/view/layout.js.map +1 -0
- package/dist/view/strings.d.ts +72 -0
- package/dist/view/strings.js +48 -0
- package/dist/view/strings.js.map +1 -0
- package/dist/view/styles.d.ts +1 -0
- package/dist/view/styles.js +115 -0
- package/dist/view/styles.js.map +1 -0
- package/dist/view.d.ts +20 -0
- package/dist/view.js +21 -0
- package/dist/view.js.map +1 -0
- package/package.json +87 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dario Hamidi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# @tinyactors/scxmljs
|
|
2
|
+
|
|
3
|
+
Run [SCXML](https://www.w3.org/TR/scxml/) statecharts in the browser, in Node and in Bun, and show them
|
|
4
|
+
running. The interpreter implements SCXML 1.0 with the ECMAScript data model and passes all 160
|
|
5
|
+
automatic mandatory W3C conformance tests. Two custom elements draw running charts: `<scxml-view>`
|
|
6
|
+
shows a whole chart and needs no JavaScript, and `<scxml-explorer>` explores large, multi-machine
|
|
7
|
+
systems one level at a time. They are plain custom elements, so they work with any framework.
|
|
8
|
+
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install @tinyactors/scxmljs
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Node 22.3 or later, Bun, or any current browser. The package is ESM only.
|
|
18
|
+
|
|
19
|
+
## Quick start
|
|
20
|
+
|
|
21
|
+
A chart, `traffic-light.scxml`:
|
|
22
|
+
|
|
23
|
+
<!-- doctest: scxml file=traffic-light.scxml -->
|
|
24
|
+
```xml
|
|
25
|
+
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" datamodel="ecmascript" name="traffic-light" initial="on">
|
|
26
|
+
<datamodel>
|
|
27
|
+
<data id="cycles" expr="0"/>
|
|
28
|
+
</datamodel>
|
|
29
|
+
<state id="on" initial="red">
|
|
30
|
+
<transition event="power.off" target="off"/>
|
|
31
|
+
<state id="red">
|
|
32
|
+
<onentry><send id="timer" event="timer" delay="3s"/></onentry>
|
|
33
|
+
<onexit><cancel sendid="timer"/></onexit>
|
|
34
|
+
<transition event="timer" target="green"/>
|
|
35
|
+
</state>
|
|
36
|
+
<state id="green">
|
|
37
|
+
<onentry><send id="timer" event="timer" delay="3s"/></onentry>
|
|
38
|
+
<onexit><cancel sendid="timer"/></onexit>
|
|
39
|
+
<transition event="timer" target="yellow"/>
|
|
40
|
+
</state>
|
|
41
|
+
<state id="yellow">
|
|
42
|
+
<onentry><send id="timer" event="timer" delay="1s"/></onentry>
|
|
43
|
+
<onexit><cancel sendid="timer"/></onexit>
|
|
44
|
+
<transition event="timer" target="red">
|
|
45
|
+
<assign location="cycles" expr="cycles + 1"/>
|
|
46
|
+
</transition>
|
|
47
|
+
</state>
|
|
48
|
+
</state>
|
|
49
|
+
<final id="off">
|
|
50
|
+
<donedata><param name="cycles" expr="cycles"/></donedata>
|
|
51
|
+
</final>
|
|
52
|
+
</scxml>
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**Draw it and run it**, with no JavaScript of your own. Import the element once (through your
|
|
56
|
+
bundler, or an [import map](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/bundling.md)):
|
|
57
|
+
|
|
58
|
+
<!-- doctest: html files=traffic-light.scxml -->
|
|
59
|
+
```html
|
|
60
|
+
<script type="module">import "@tinyactors/scxmljs/view";</script>
|
|
61
|
+
|
|
62
|
+
<scxml-view src="traffic-light.scxml"></scxml-view>
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
The element loads, validates, runs and draws the chart. Active states are highlighted, and each
|
|
66
|
+
transition label is a button that sends its event.
|
|
67
|
+
|
|
68
|
+
**Explore a running system** with `<scxml-explorer>`: a tree of states, a focus on one level of
|
|
69
|
+
the chart, the events the machine accepts right now, and playback controls.
|
|
70
|
+
|
|
71
|
+
<!-- doctest: check -->
|
|
72
|
+
```ts
|
|
73
|
+
import { createSession, PlaybackClock } from "@tinyactors/scxmljs/trusted";
|
|
74
|
+
import "@tinyactors/scxmljs/explorer";
|
|
75
|
+
|
|
76
|
+
const source = await (await fetch("traffic-light.scxml")).text();
|
|
77
|
+
const clock = new PlaybackClock({ speed: 0.5 }); // adds pause, step and speed controls
|
|
78
|
+
const session = await createSession(source, { clock });
|
|
79
|
+
document.querySelector("scxml-explorer")!.attach({ session, clock });
|
|
80
|
+
session.start();
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
**Run it headless**, for example in Node or in tests:
|
|
84
|
+
|
|
85
|
+
<!-- doctest: run files=traffic-light.scxml -->
|
|
86
|
+
```ts
|
|
87
|
+
import { readFile } from "node:fs/promises";
|
|
88
|
+
import { createSession } from "@tinyactors/scxmljs";
|
|
89
|
+
import { Window } from "happy-dom"; // outside browsers, SCXML text needs a DOMParser
|
|
90
|
+
|
|
91
|
+
const { DOMParser } = new Window();
|
|
92
|
+
const source = await readFile("traffic-light.scxml", "utf8");
|
|
93
|
+
const session = await createSession(source, { domParser: new DOMParser() });
|
|
94
|
+
|
|
95
|
+
session.start();
|
|
96
|
+
console.log(session.activeStateIds().join(" "));
|
|
97
|
+
session.send("timer");
|
|
98
|
+
await session.settled();
|
|
99
|
+
console.log(session.activeStateIds().join(" "));
|
|
100
|
+
session.send("power.off");
|
|
101
|
+
console.log(JSON.stringify(await session.done));
|
|
102
|
+
session.dispose();
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
<!-- doctest: output -->
|
|
106
|
+
```text
|
|
107
|
+
on red
|
|
108
|
+
on green
|
|
109
|
+
{"cycles":0}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
The [getting-started guide](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/getting-started.md) walks through all three.
|
|
113
|
+
|
|
114
|
+
## Entry points
|
|
115
|
+
|
|
116
|
+
| Import | What | Size (min + gzip) |
|
|
117
|
+
|---|---|---|
|
|
118
|
+
| `@tinyactors/scxmljs` | the interpreter, **sandboxed**: chart code runs in QuickJS (WebAssembly) | 20 KB, then 290 KB loaded on demand |
|
|
119
|
+
| `@tinyactors/scxmljs/trusted` | the same API, **trusted**: chart code runs in the host's own JavaScript engine | 17 KB |
|
|
120
|
+
| `@tinyactors/scxmljs/view` | `<scxml-view>`: a whole chart, drawn and running, zero-JS | 33 KB, plus the engine it loads |
|
|
121
|
+
| `@tinyactors/scxmljs/explorer` | `<scxml-explorer>` and its view-model: explore a running system | 25 KB |
|
|
122
|
+
| `@tinyactors/scxmljs/themes/tinyactors.css` | an optional theme stylesheet | 0.3 KB |
|
|
123
|
+
|
|
124
|
+
Sizes are for a browser bundle of each entry on its own (`mise run size` in the repository).
|
|
125
|
+
|
|
126
|
+
## Sandboxed or trusted?
|
|
127
|
+
|
|
128
|
+
Both interpreter entry points have the same API and pass the same conformance suite. They differ
|
|
129
|
+
in where the chart's ECMAScript (`<script>`, `expr`, `cond`) runs:
|
|
130
|
+
|
|
131
|
+
- **Sandboxed** (`@tinyactors/scxmljs`, the default): one QuickJS context per session. The chart
|
|
132
|
+
can't see the page or other sessions, runaway scripts are interrupted, and memory is capped.
|
|
133
|
+
Use it for charts that users write, paste or upload.
|
|
134
|
+
- **Trusted** (`@tinyactors/scxmljs/trusted`): a separate realm of the host engine per session (a
|
|
135
|
+
hidden iframe in browsers, a `node:vm` context in Node and Bun). No WebAssembly to download, but
|
|
136
|
+
the chart can reach the page and a loop can't be interrupted. Use it for charts you wrote.
|
|
137
|
+
|
|
138
|
+
Details: [sandboxed vs trusted](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/sandboxed-vs-trusted.md) and [SECURITY.md](https://github.com/tinyactors-dev/scxmljs/blob/main/SECURITY.md).
|
|
139
|
+
|
|
140
|
+
## Documentation
|
|
141
|
+
|
|
142
|
+
- [Getting started](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/getting-started.md)
|
|
143
|
+
- Elements: [`<scxml-view>`](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/view.md), [`<scxml-explorer>`](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/explorer.md), [theming](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/theming.md), [custom UI and translations](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/custom-ui.md), [large charts](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/large-charts.md), [frameworks](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/frameworks.md)
|
|
144
|
+
- Interpreter: [sandboxed vs trusted](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/sandboxed-vs-trusted.md), [driving charts from the page](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/driving-charts.md), [custom I/O processors](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/io-processors.md), [custom invokers](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/invokers.md), [playback and stepping](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/playback.md), [testing](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/testing.md), [Node and servers](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/node-and-servers.md)
|
|
145
|
+
- Shipping: [bundling, CDNs and classic scripts](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/bundling.md), [Content-Security-Policy](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/csp.md)
|
|
146
|
+
- Reference: [conformance report](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/conformance.md), [deviations from the spec](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/deviations.md), [measurements](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/measurements.md), API reference (`mise run docs:api` in the repository)
|
|
147
|
+
|
|
148
|
+
## Support
|
|
149
|
+
|
|
150
|
+
| | Status |
|
|
151
|
+
|---|---|
|
|
152
|
+
| Chrome / Edge (Chromium) | tested in CI (Chromium 153) |
|
|
153
|
+
| Firefox | tested in CI (Firefox 155) |
|
|
154
|
+
| Safari | tested in CI with WebKit 26.6 (Safari's engine, not Safari itself) |
|
|
155
|
+
| Node | 22.3 or later; tested in CI on 26.10, by hand on 22.3 |
|
|
156
|
+
| Bun | tested in CI (1.4) |
|
|
157
|
+
| Deno | tested in CI (2.9) |
|
|
158
|
+
|
|
159
|
+
The [support table](https://github.com/tinyactors-dev/scxmljs/blob/main/docs/bundling.md#support) lists what each environment needs.
|
|
160
|
+
|
|
161
|
+
## Stability
|
|
162
|
+
|
|
163
|
+
The package is at 0.x. Breaking changes can happen in minor versions until 1.0, and every one is
|
|
164
|
+
listed in the [changelog](https://github.com/tinyactors-dev/scxmljs/blob/main/CHANGELOG.md). Everything the entry points export is public API; anything
|
|
165
|
+
else (and anything marked `@internal`) is not.
|
|
166
|
+
|
|
167
|
+
## Contributions
|
|
168
|
+
|
|
169
|
+
This project doesn't accept outside contributions. Please report security problems as described
|
|
170
|
+
in [SECURITY.md](https://github.com/tinyactors-dev/scxmljs/blob/main/SECURITY.md).
|
|
171
|
+
|
|
172
|
+
## License
|
|
173
|
+
|
|
174
|
+
MIT. QuickJS and quickjs-emscripten are MIT-licensed too; see `THIRD_PARTY_NOTICES`.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
Third-party notices for @tinyactors/scxmljs
|
|
2
|
+
===========================================
|
|
3
|
+
|
|
4
|
+
@tinyactors/scxmljs depends on the following packages at run time. Only the
|
|
5
|
+
sandboxed entry point (`@tinyactors/scxmljs`) loads them; the trusted entry
|
|
6
|
+
point (`@tinyactors/scxmljs/trusted`) does not. They are installed as npm
|
|
7
|
+
dependencies, not copied into this package; their notices are reproduced here
|
|
8
|
+
because bundlers inline them (including the QuickJS WebAssembly binary) into
|
|
9
|
+
applications that use the sandboxed entry point.
|
|
10
|
+
|
|
11
|
+
- quickjs-emscripten-core 0.32.0 (MIT) — https://github.com/justjake/quickjs-emscripten
|
|
12
|
+
- @jitl/quickjs-ffi-types 0.32.0 (MIT) — https://github.com/justjake/quickjs-emscripten
|
|
13
|
+
- @jitl/quickjs-singlefile-browser-release-sync 0.32.0 (MIT) — https://github.com/justjake/quickjs-emscripten
|
|
14
|
+
contains QuickJS (MIT) — https://bellard.org/quickjs/
|
|
15
|
+
|
|
16
|
+
-------------------------------------------------------------------------------
|
|
17
|
+
quickjs-emscripten:
|
|
18
|
+
The MIT License
|
|
19
|
+
|
|
20
|
+
quickjs-emscripten copyright (c) 2019-2024 Jake Teton-Landis
|
|
21
|
+
|
|
22
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
23
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
24
|
+
in the Software without restriction, including without limitation the rights
|
|
25
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
26
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
27
|
+
furnished to do so, subject to the following conditions:
|
|
28
|
+
|
|
29
|
+
The above copyright notice and this permission notice shall be included in all
|
|
30
|
+
copies or substantial portions of the Software.
|
|
31
|
+
|
|
32
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
33
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
34
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
35
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
36
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
37
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
38
|
+
SOFTWARE.
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
quickjs:
|
|
42
|
+
QuickJS Javascript Engine
|
|
43
|
+
|
|
44
|
+
Copyright (c) 2017-2021 Fabrice Bellard
|
|
45
|
+
Copyright (c) 2017-2021 Charlie Gordon
|
|
46
|
+
|
|
47
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
48
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
49
|
+
in the Software without restriction, including without limitation the rights
|
|
50
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
51
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
52
|
+
furnished to do so, subject to the following conditions:
|
|
53
|
+
|
|
54
|
+
The above copyright notice and this permission notice shall be included in
|
|
55
|
+
all copies or substantial portions of the Software.
|
|
56
|
+
|
|
57
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
58
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
59
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
60
|
+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
61
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
62
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
63
|
+
THE SOFTWARE.
|
package/dist/bridge.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { SCXMLSession } from "./session.js";
|
|
2
|
+
export interface ConnectOptions {
|
|
3
|
+
/** Disconnect when this signal aborts. */
|
|
4
|
+
signal?: AbortSignal;
|
|
5
|
+
/** Listen in the capture phase. */
|
|
6
|
+
capture?: boolean;
|
|
7
|
+
}
|
|
8
|
+
/** An SCXML event name, or a function choosing one per DOM event (null/undefined: send nothing). */
|
|
9
|
+
export type EventNameMapper = string | ((e: Event) => string | null | undefined);
|
|
10
|
+
/**
|
|
11
|
+
* Send an SCXML event whenever `target` fires `domType` (one type or several).
|
|
12
|
+
* `data` computes the event data from the DOM event.
|
|
13
|
+
*/
|
|
14
|
+
export declare function connect(session: SCXMLSession, target: EventTarget, domType: string | readonly string[], scxmlEvent: EventNameMapper, data?: (e: Event) => unknown, options?: ConnectOptions): () => void;
|
|
15
|
+
/** Options for `bind()`. */
|
|
16
|
+
export interface BindOptions {
|
|
17
|
+
/** Unbind when this signal aborts (the binding also ends when the session terminates). */
|
|
18
|
+
signal?: AbortSignal;
|
|
19
|
+
/**
|
|
20
|
+
* Keep `data-scxml-enabled` on bound elements whose event some active state
|
|
21
|
+
* has a transition for (conditions are ignored), so CSS can dim controls
|
|
22
|
+
* that would do nothing. Updated after every macrostep.
|
|
23
|
+
*/
|
|
24
|
+
reflectEnabled?: boolean;
|
|
25
|
+
/** Extra DOM event types to listen for, beyond the built-in set and those found in `data-scxml-on` at bind time. */
|
|
26
|
+
events?: readonly string[];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Declarative sends, by event delegation on `root`:
|
|
30
|
+
*
|
|
31
|
+
* - `data-scxml-send="name"`: send `name`. Default trigger: `click`; for a
|
|
32
|
+
* `<form>`, `submit` (default prevented; data = the form's fields);
|
|
33
|
+
* for `<input>/<select>/<textarea>`, `change` (data = `{ name, value }`,
|
|
34
|
+
* plus `checked` for checkboxes and radios).
|
|
35
|
+
* - A submit button with `data-scxml-send` inside a form names the event its
|
|
36
|
+
* submission sends (data = the form's fields): one form, several actions.
|
|
37
|
+
* - `data-scxml-on="dblclick keydown"`: override the trigger(s).
|
|
38
|
+
* - `data-scxml-data='{"json": true}'`: static data, merged over the
|
|
39
|
+
* dynamic data (invalid JSON: nothing is sent).
|
|
40
|
+
* - `data-scxml-session="<sessionId>"` on the element or an ancestor: only
|
|
41
|
+
* that session reacts. Unmarked elements inside `root` go to every session
|
|
42
|
+
* bound to `root`.
|
|
43
|
+
*/
|
|
44
|
+
export declare function bind(session: SCXMLSession, root: Element | Document, options?: BindOptions): () => void;
|
package/dist/bridge.js
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DOM events → SCXML events: drive a statechart from ordinary page UI.
|
|
3
|
+
*
|
|
4
|
+
* connect(session, button, "click", "go") // imperative
|
|
5
|
+
* bind(session, document, { reflectEnabled: true }) // declarative:
|
|
6
|
+
* <button data-scxml-send="go">Go</button>
|
|
7
|
+
* <form data-scxml-send="login"><input name="user"><button>Log in</button></form>
|
|
8
|
+
*
|
|
9
|
+
* Both return a disposer, accept `{ signal }`, and disconnect by themselves
|
|
10
|
+
* when the session terminates (its `signal` aborts).
|
|
11
|
+
*/
|
|
12
|
+
import { nameMatch } from "./events.js";
|
|
13
|
+
/**
|
|
14
|
+
* Send an SCXML event whenever `target` fires `domType` (one type or several).
|
|
15
|
+
* `data` computes the event data from the DOM event.
|
|
16
|
+
*/
|
|
17
|
+
export function connect(session, target, domType, scxmlEvent, data, options = {}) {
|
|
18
|
+
const types = typeof domType === "string" ? domType.split(/\s+/).filter(Boolean) : [...domType];
|
|
19
|
+
const handler = (e) => {
|
|
20
|
+
let name;
|
|
21
|
+
let payload;
|
|
22
|
+
try {
|
|
23
|
+
name = typeof scxmlEvent === "string" ? scxmlEvent : scxmlEvent(e);
|
|
24
|
+
if (!name)
|
|
25
|
+
return;
|
|
26
|
+
payload = data ? data(e) : undefined;
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
console.error("scxml connect: mapping a DOM event failed", err);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
session.send(name, payload);
|
|
33
|
+
};
|
|
34
|
+
return listen(session, options.signal, () => {
|
|
35
|
+
for (const t of types)
|
|
36
|
+
target.addEventListener(t, handler, options.capture);
|
|
37
|
+
return () => {
|
|
38
|
+
for (const t of types)
|
|
39
|
+
target.removeEventListener(t, handler, options.capture);
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
const SEND = "data-scxml-send";
|
|
44
|
+
const ON = "data-scxml-on";
|
|
45
|
+
const DATA = "data-scxml-data";
|
|
46
|
+
const TARGET_SESSION = "data-scxml-session";
|
|
47
|
+
const ENABLED = "data-scxml-enabled";
|
|
48
|
+
const BUILTIN_TYPES = [
|
|
49
|
+
"click",
|
|
50
|
+
"dblclick",
|
|
51
|
+
"submit",
|
|
52
|
+
"change",
|
|
53
|
+
"input",
|
|
54
|
+
"focusin",
|
|
55
|
+
"focusout",
|
|
56
|
+
"keydown",
|
|
57
|
+
"keyup",
|
|
58
|
+
"pointerdown",
|
|
59
|
+
"pointerup",
|
|
60
|
+
"contextmenu",
|
|
61
|
+
];
|
|
62
|
+
/**
|
|
63
|
+
* Declarative sends, by event delegation on `root`:
|
|
64
|
+
*
|
|
65
|
+
* - `data-scxml-send="name"`: send `name`. Default trigger: `click`; for a
|
|
66
|
+
* `<form>`, `submit` (default prevented; data = the form's fields);
|
|
67
|
+
* for `<input>/<select>/<textarea>`, `change` (data = `{ name, value }`,
|
|
68
|
+
* plus `checked` for checkboxes and radios).
|
|
69
|
+
* - A submit button with `data-scxml-send` inside a form names the event its
|
|
70
|
+
* submission sends (data = the form's fields): one form, several actions.
|
|
71
|
+
* - `data-scxml-on="dblclick keydown"`: override the trigger(s).
|
|
72
|
+
* - `data-scxml-data='{"json": true}'`: static data, merged over the
|
|
73
|
+
* dynamic data (invalid JSON: nothing is sent).
|
|
74
|
+
* - `data-scxml-session="<sessionId>"` on the element or an ancestor: only
|
|
75
|
+
* that session reacts. Unmarked elements inside `root` go to every session
|
|
76
|
+
* bound to `root`.
|
|
77
|
+
*/
|
|
78
|
+
export function bind(session, root, options = {}) {
|
|
79
|
+
const types = new Set([...BUILTIN_TYPES, ...(options.events ?? [])]);
|
|
80
|
+
for (const el of Array.from(root.querySelectorAll(`[${ON}]`)))
|
|
81
|
+
for (const t of el.getAttribute(ON).split(/\s+/))
|
|
82
|
+
if (t)
|
|
83
|
+
types.add(t);
|
|
84
|
+
const handler = (e) => {
|
|
85
|
+
if (e.type === "submit")
|
|
86
|
+
return onSubmit(e);
|
|
87
|
+
for (let el = isNode(e.target) ? closestElement(e.target) : null; el; el = el.parentElement) {
|
|
88
|
+
if (!contains(root, el))
|
|
89
|
+
return;
|
|
90
|
+
if (!el.hasAttribute(SEND) || !triggers(el).includes(e.type))
|
|
91
|
+
continue;
|
|
92
|
+
if (!targetsSession(el, session))
|
|
93
|
+
return;
|
|
94
|
+
dispatch(el, el.getAttribute(SEND), controlData(el));
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const onSubmit = (e) => {
|
|
99
|
+
const form = e.target;
|
|
100
|
+
if (!isElement(form) || form.localName !== "form" || !contains(root, form))
|
|
101
|
+
return;
|
|
102
|
+
const submitter = e.submitter ?? null;
|
|
103
|
+
const source = submitter?.hasAttribute(SEND) ? submitter : form.hasAttribute(SEND) ? form : null;
|
|
104
|
+
if (!source || !triggers(source).includes("submit") || !targetsSession(source, session))
|
|
105
|
+
return;
|
|
106
|
+
e.preventDefault();
|
|
107
|
+
const fields = formData(form);
|
|
108
|
+
const formStatic = source !== form && form.hasAttribute(DATA) ? parseStatic(form) : undefined;
|
|
109
|
+
if (formStatic === INVALID)
|
|
110
|
+
return;
|
|
111
|
+
dispatch(source, source.getAttribute(SEND), merge(fields, formStatic));
|
|
112
|
+
};
|
|
113
|
+
const dispatch = (el, name, dynamic) => {
|
|
114
|
+
if (!name)
|
|
115
|
+
return;
|
|
116
|
+
const stat = el.hasAttribute(DATA) ? parseStatic(el) : undefined;
|
|
117
|
+
if (stat === INVALID)
|
|
118
|
+
return;
|
|
119
|
+
session.send(name, merge(dynamic, stat));
|
|
120
|
+
};
|
|
121
|
+
const reflect = () => {
|
|
122
|
+
for (const el of boundElements(root, session)) {
|
|
123
|
+
const name = el.getAttribute(SEND);
|
|
124
|
+
const on = session.configuration.some((s) => s.transitions.some((t) => t.events.length > 0 && nameMatch(t.events, name)));
|
|
125
|
+
if (on)
|
|
126
|
+
el.setAttribute(ENABLED, "");
|
|
127
|
+
else
|
|
128
|
+
el.removeAttribute(ENABLED);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
return listen(session, options.signal, () => {
|
|
132
|
+
for (const t of types)
|
|
133
|
+
root.addEventListener(t, handler, true);
|
|
134
|
+
if (options.reflectEnabled) {
|
|
135
|
+
session.addEventListener("macrostep", reflect);
|
|
136
|
+
reflect();
|
|
137
|
+
}
|
|
138
|
+
return () => {
|
|
139
|
+
for (const t of types)
|
|
140
|
+
root.removeEventListener(t, handler, true);
|
|
141
|
+
if (options.reflectEnabled) {
|
|
142
|
+
session.removeEventListener("macrostep", reflect);
|
|
143
|
+
for (const el of boundElements(root, session))
|
|
144
|
+
el.removeAttribute(ENABLED);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
// ─────────────────────────────── helpers ───────────────────────────────
|
|
150
|
+
/** Runs `attach`; the returned teardown runs once, on dispose, on `signal` abort, or when the session terminates. */
|
|
151
|
+
function listen(session, signal, attach) {
|
|
152
|
+
if (signal?.aborted || session.signal.aborted)
|
|
153
|
+
return () => { };
|
|
154
|
+
const detach = attach();
|
|
155
|
+
let done = false;
|
|
156
|
+
const dispose = () => {
|
|
157
|
+
if (done)
|
|
158
|
+
return;
|
|
159
|
+
done = true;
|
|
160
|
+
detach();
|
|
161
|
+
signal?.removeEventListener("abort", dispose);
|
|
162
|
+
session.signal.removeEventListener("abort", dispose);
|
|
163
|
+
};
|
|
164
|
+
signal?.addEventListener("abort", dispose);
|
|
165
|
+
session.signal.addEventListener("abort", dispose);
|
|
166
|
+
return dispose;
|
|
167
|
+
}
|
|
168
|
+
function triggers(el) {
|
|
169
|
+
const explicit = el.getAttribute(ON);
|
|
170
|
+
if (explicit != null)
|
|
171
|
+
return explicit.split(/\s+/).filter(Boolean);
|
|
172
|
+
if (el.localName === "form")
|
|
173
|
+
return ["submit"];
|
|
174
|
+
if (isSubmitButton(el) && el.form)
|
|
175
|
+
return ["submit"];
|
|
176
|
+
if (el.localName === "input" || el.localName === "select" || el.localName === "textarea")
|
|
177
|
+
return ["change"];
|
|
178
|
+
return ["click"];
|
|
179
|
+
}
|
|
180
|
+
function isSubmitButton(el) {
|
|
181
|
+
const type = (el.getAttribute("type") ?? "").toLowerCase();
|
|
182
|
+
return ((el.localName === "button" && (type === "" || type === "submit")) ||
|
|
183
|
+
(el.localName === "input" && (type === "submit" || type === "image")));
|
|
184
|
+
}
|
|
185
|
+
function targetsSession(el, session) {
|
|
186
|
+
const marked = el.closest(`[${TARGET_SESSION}]`);
|
|
187
|
+
return !marked || marked.getAttribute(TARGET_SESSION) === session.sessionId;
|
|
188
|
+
}
|
|
189
|
+
function boundElements(root, session) {
|
|
190
|
+
const all = Array.from(root.querySelectorAll(`[${SEND}]`));
|
|
191
|
+
if (isElement(root) && root.hasAttribute(SEND))
|
|
192
|
+
all.unshift(root);
|
|
193
|
+
return all.filter((el) => targetsSession(el, session));
|
|
194
|
+
}
|
|
195
|
+
function controlData(el) {
|
|
196
|
+
const n = el.localName;
|
|
197
|
+
if (n !== "input" && n !== "select" && n !== "textarea")
|
|
198
|
+
return undefined;
|
|
199
|
+
const control = el;
|
|
200
|
+
const out = { name: control.name, value: control.value };
|
|
201
|
+
if (n === "input" && (control.type === "checkbox" || control.type === "radio"))
|
|
202
|
+
out.checked = control.checked;
|
|
203
|
+
return out;
|
|
204
|
+
}
|
|
205
|
+
/** The form's fields as a plain object; repeated names become arrays. */
|
|
206
|
+
function formData(form) {
|
|
207
|
+
const FD = form.ownerDocument?.defaultView?.FormData ?? FormData;
|
|
208
|
+
const out = {};
|
|
209
|
+
for (const [k, v] of new FD(form)) {
|
|
210
|
+
const value = typeof v === "string" ? v : v.name;
|
|
211
|
+
if (!(k in out))
|
|
212
|
+
out[k] = value;
|
|
213
|
+
else if (Array.isArray(out[k]))
|
|
214
|
+
out[k].push(value);
|
|
215
|
+
else
|
|
216
|
+
out[k] = [out[k], value];
|
|
217
|
+
}
|
|
218
|
+
return out;
|
|
219
|
+
}
|
|
220
|
+
const INVALID = Symbol("invalid");
|
|
221
|
+
function parseStatic(el) {
|
|
222
|
+
try {
|
|
223
|
+
return JSON.parse(el.getAttribute(DATA));
|
|
224
|
+
}
|
|
225
|
+
catch (err) {
|
|
226
|
+
console.error(`scxml bind: invalid JSON in ${DATA} of`, el, err);
|
|
227
|
+
return INVALID;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
function merge(dynamic, stat) {
|
|
231
|
+
if (stat === undefined)
|
|
232
|
+
return dynamic;
|
|
233
|
+
if (dynamic === undefined)
|
|
234
|
+
return stat;
|
|
235
|
+
if (isPlainObject(dynamic) && isPlainObject(stat))
|
|
236
|
+
return { ...dynamic, ...stat };
|
|
237
|
+
return stat;
|
|
238
|
+
}
|
|
239
|
+
function isPlainObject(v) {
|
|
240
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
241
|
+
}
|
|
242
|
+
function isElement(v) {
|
|
243
|
+
return typeof v === "object" && v !== null && v.nodeType === 1;
|
|
244
|
+
}
|
|
245
|
+
function isNode(v) {
|
|
246
|
+
return typeof v === "object" && v !== null && typeof v.nodeType === "number";
|
|
247
|
+
}
|
|
248
|
+
function closestElement(node) {
|
|
249
|
+
return isElement(node) ? node : node.parentElement;
|
|
250
|
+
}
|
|
251
|
+
function contains(root, el) {
|
|
252
|
+
return root === el || root.contains(el);
|
|
253
|
+
}
|
|
254
|
+
//# sourceMappingURL=bridge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bridge.js","sourceRoot":"","sources":["../src/bridge.ts"],"names":[],"mappings":"AAAA;;;;;;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CAAC,OAAgB,EAAE,IAAa;IAC5C,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,OAAO,CAAC;IACvC,IAAI,OAAO,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IACvC,IAAI,aAAa,CAAC,OAAO,CAAC,IAAI,aAAa,CAAC,IAAI,CAAC;QAAE,OAAO,EAAE,GAAG,OAAO,EAAE,GAAG,IAAI,EAAE,CAAC;IAClF,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,aAAa,CAAC,CAAU;IAC/B,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;AAClE,CAAC;AAED,SAAS,SAAS,CAAC,CAAU;IAC3B,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,IAAI,IAAK,CAAU,CAAC,QAAQ,KAAK,CAAC,CAAC;AAC3E,CAAC;AAED,SAAS,MAAM,CAAC,CAAU;IACxB,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,IAAI,IAAI,OAAQ,CAAU,CAAC,QAAQ,KAAK,QAAQ,CAAC;AACzF,CAAC;AAED,SAAS,cAAc,CAAC,IAAU;IAChC,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC;AACrD,CAAC;AAED,SAAS,QAAQ,CAAC,IAAwB,EAAE,EAAW;IACrD,OAAO,IAAI,KAAK,EAAE,IAAI,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;AAC1C,CAAC","sourcesContent":["/**\n * DOM events → SCXML events: drive a statechart from ordinary page UI.\n *\n * connect(session, button, \"click\", \"go\") // imperative\n * bind(session, document, { reflectEnabled: true }) // declarative:\n * <button data-scxml-send=\"go\">Go</button>\n * <form data-scxml-send=\"login\"><input name=\"user\"><button>Log in</button></form>\n *\n * Both return a disposer, accept `{ signal }`, and disconnect by themselves\n * when the session terminates (its `signal` aborts).\n */\nimport { nameMatch } from \"./events.ts\";\nimport type { SCXMLSession } from \"./session.ts\";\n\nexport interface ConnectOptions {\n /** Disconnect when this signal aborts. */\n signal?: AbortSignal;\n /** Listen in the capture phase. */\n capture?: boolean;\n}\n\n/** An SCXML event name, or a function choosing one per DOM event (null/undefined: send nothing). */\nexport type EventNameMapper = string | ((e: Event) => string | null | undefined);\n\n/**\n * Send an SCXML event whenever `target` fires `domType` (one type or several).\n * `data` computes the event data from the DOM event.\n */\nexport function connect(\n session: SCXMLSession,\n target: EventTarget,\n domType: string | readonly string[],\n scxmlEvent: EventNameMapper,\n data?: (e: Event) => unknown,\n options: ConnectOptions = {},\n): () => void {\n const types = typeof domType === \"string\" ? domType.split(/\\s+/).filter(Boolean) : [...domType];\n const handler = (e: Event) => {\n let name: string | null | undefined;\n let payload: unknown;\n try {\n name = typeof scxmlEvent === \"string\" ? scxmlEvent : scxmlEvent(e);\n if (!name) return;\n payload = data ? data(e) : undefined;\n } catch (err) {\n console.error(\"scxml connect: mapping a DOM event failed\", err);\n return;\n }\n session.send(name, payload);\n };\n return listen(session, options.signal, () => {\n for (const t of types) target.addEventListener(t, handler, options.capture);\n return () => {\n for (const t of types) target.removeEventListener(t, handler, options.capture);\n };\n });\n}\n\n/** Options for `bind()`. */\nexport interface BindOptions {\n /** Unbind when this signal aborts (the binding also ends when the session terminates). */\n signal?: AbortSignal;\n /**\n * Keep `data-scxml-enabled` on bound elements whose event some active state\n * has a transition for (conditions are ignored), so CSS can dim controls\n * that would do nothing. Updated after every macrostep.\n */\n reflectEnabled?: boolean;\n /** Extra DOM event types to listen for, beyond the built-in set and those found in `data-scxml-on` at bind time. */\n events?: readonly string[];\n}\n\nconst SEND = \"data-scxml-send\";\nconst ON = \"data-scxml-on\";\nconst DATA = \"data-scxml-data\";\nconst TARGET_SESSION = \"data-scxml-session\";\nconst ENABLED = \"data-scxml-enabled\";\nconst BUILTIN_TYPES = [\n \"click\",\n \"dblclick\",\n \"submit\",\n \"change\",\n \"input\",\n \"focusin\",\n \"focusout\",\n \"keydown\",\n \"keyup\",\n \"pointerdown\",\n \"pointerup\",\n \"contextmenu\",\n];\n\n/**\n * Declarative sends, by event delegation on `root`:\n *\n * - `data-scxml-send=\"name\"`: send `name`. Default trigger: `click`; for a\n * `<form>`, `submit` (default prevented; data = the form's fields);\n * for `<input>/<select>/<textarea>`, `change` (data = `{ name, value }`,\n * plus `checked` for checkboxes and radios).\n * - A submit button with `data-scxml-send` inside a form names the event its\n * submission sends (data = the form's fields): one form, several actions.\n * - `data-scxml-on=\"dblclick keydown\"`: override the trigger(s).\n * - `data-scxml-data='{\"json\": true}'`: static data, merged over the\n * dynamic data (invalid JSON: nothing is sent).\n * - `data-scxml-session=\"<sessionId>\"` on the element or an ancestor: only\n * that session reacts. Unmarked elements inside `root` go to every session\n * bound to `root`.\n */\nexport function bind(session: SCXMLSession, root: Element | Document, options: BindOptions = {}): () => void {\n const types = new Set([...BUILTIN_TYPES, ...(options.events ?? [])]);\n for (const el of Array.from(root.querySelectorAll(`[${ON}]`))) for (const t of el.getAttribute(ON)!.split(/\\s+/)) if (t) types.add(t);\n\n const handler = (e: Event) => {\n if (e.type === \"submit\") return onSubmit(e);\n for (let el = isNode(e.target) ? closestElement(e.target) : null; el; el = el.parentElement) {\n if (!contains(root, el)) return;\n if (!el.hasAttribute(SEND) || !triggers(el).includes(e.type)) continue;\n if (!targetsSession(el, session)) return;\n dispatch(el, el.getAttribute(SEND)!, controlData(el));\n return;\n }\n };\n\n const onSubmit = (e: Event) => {\n const form = e.target as HTMLFormElement;\n if (!isElement(form) || form.localName !== \"form\" || !contains(root, form)) return;\n const submitter = (e as SubmitEvent).submitter ?? null;\n const source = submitter?.hasAttribute(SEND) ? submitter : form.hasAttribute(SEND) ? form : null;\n if (!source || !triggers(source).includes(\"submit\") || !targetsSession(source, session)) return;\n e.preventDefault();\n const fields = formData(form);\n const formStatic = source !== form && form.hasAttribute(DATA) ? parseStatic(form) : undefined;\n if (formStatic === INVALID) return;\n dispatch(source, source.getAttribute(SEND)!, merge(fields, formStatic));\n };\n\n const dispatch = (el: Element, name: string, dynamic: unknown) => {\n if (!name) return;\n const stat = el.hasAttribute(DATA) ? parseStatic(el) : undefined;\n if (stat === INVALID) return;\n session.send(name, merge(dynamic, stat));\n };\n\n const reflect = () => {\n for (const el of boundElements(root, session)) {\n const name = el.getAttribute(SEND)!;\n const on = session.configuration.some((s) => s.transitions.some((t) => t.events.length > 0 && nameMatch(t.events, name)));\n if (on) el.setAttribute(ENABLED, \"\");\n else el.removeAttribute(ENABLED);\n }\n };\n\n return listen(session, options.signal, () => {\n for (const t of types) root.addEventListener(t, handler, true);\n if (options.reflectEnabled) {\n session.addEventListener(\"macrostep\", reflect);\n reflect();\n }\n return () => {\n for (const t of types) root.removeEventListener(t, handler, true);\n if (options.reflectEnabled) {\n session.removeEventListener(\"macrostep\", reflect);\n for (const el of boundElements(root, session)) el.removeAttribute(ENABLED);\n }\n };\n });\n}\n\n// ─────────────────────────────── helpers ───────────────────────────────\n\n/** Runs `attach`; the returned teardown runs once, on dispose, on `signal` abort, or when the session terminates. */\nfunction listen(session: SCXMLSession, signal: AbortSignal | undefined, attach: () => () => void): () => void {\n if (signal?.aborted || session.signal.aborted) return () => {};\n const detach = attach();\n let done = false;\n const dispose = () => {\n if (done) return;\n done = true;\n detach();\n signal?.removeEventListener(\"abort\", dispose);\n session.signal.removeEventListener(\"abort\", dispose);\n };\n signal?.addEventListener(\"abort\", dispose);\n session.signal.addEventListener(\"abort\", dispose);\n return dispose;\n}\n\nfunction triggers(el: Element): string[] {\n const explicit = el.getAttribute(ON);\n if (explicit != null) return explicit.split(/\\s+/).filter(Boolean);\n if (el.localName === \"form\") return [\"submit\"];\n if (isSubmitButton(el) && (el as HTMLButtonElement).form) return [\"submit\"];\n if (el.localName === \"input\" || el.localName === \"select\" || el.localName === \"textarea\") return [\"change\"];\n return [\"click\"];\n}\n\nfunction isSubmitButton(el: Element) {\n const type = (el.getAttribute(\"type\") ?? \"\").toLowerCase();\n return (\n (el.localName === \"button\" && (type === \"\" || type === \"submit\")) ||\n (el.localName === \"input\" && (type === \"submit\" || type === \"image\"))\n );\n}\n\nfunction targetsSession(el: Element, session: SCXMLSession) {\n const marked = el.closest(`[${TARGET_SESSION}]`);\n return !marked || marked.getAttribute(TARGET_SESSION) === session.sessionId;\n}\n\nfunction boundElements(root: Element | Document, session: SCXMLSession): Element[] {\n const all = Array.from(root.querySelectorAll(`[${SEND}]`));\n if (isElement(root) && root.hasAttribute(SEND)) all.unshift(root);\n return all.filter((el) => targetsSession(el, session));\n}\n\nfunction controlData(el: Element): unknown {\n const n = el.localName;\n if (n !== \"input\" && n !== \"select\" && n !== \"textarea\") return undefined;\n const control = el as HTMLInputElement;\n const out: Record<string, unknown> = { name: control.name, value: control.value };\n if (n === \"input\" && (control.type === \"checkbox\" || control.type === \"radio\")) out.checked = control.checked;\n return out;\n}\n\n/** The form's fields as a plain object; repeated names become arrays. */\nfunction formData(form: HTMLFormElement): Record<string, unknown> {\n const FD: typeof FormData = (form.ownerDocument?.defaultView as { FormData?: typeof FormData } | null)?.FormData ?? FormData;\n const out: Record<string, unknown> = {};\n for (const [k, v] of new FD(form) as unknown as Iterable<[string, FormDataEntryValue]>) {\n const value = typeof v === \"string\" ? v : v.name;\n if (!(k in out)) out[k] = value;\n else if (Array.isArray(out[k])) (out[k] as unknown[]).push(value);\n else out[k] = [out[k], value];\n }\n return out;\n}\n\nconst INVALID = Symbol(\"invalid\");\n\nfunction parseStatic(el: Element): unknown {\n try {\n return JSON.parse(el.getAttribute(DATA)!);\n } catch (err) {\n console.error(`scxml bind: invalid JSON in ${DATA} of`, el, err);\n return INVALID;\n }\n}\n\nfunction merge(dynamic: unknown, stat: unknown): unknown {\n if (stat === undefined) return dynamic;\n if (dynamic === undefined) return stat;\n if (isPlainObject(dynamic) && isPlainObject(stat)) return { ...dynamic, ...stat };\n return stat;\n}\n\nfunction isPlainObject(v: unknown): v is Record<string, unknown> {\n return typeof v === \"object\" && v !== null && !Array.isArray(v);\n}\n\nfunction isElement(v: unknown): v is Element {\n return typeof v === \"object\" && v !== null && (v as Node).nodeType === 1;\n}\n\nfunction isNode(v: unknown): v is Node {\n return typeof v === \"object\" && v !== null && typeof (v as Node).nodeType === \"number\";\n}\n\nfunction closestElement(node: Node): Element | null {\n return isElement(node) ? node : node.parentElement;\n}\n\nfunction contains(root: Element | Document, el: Element) {\n return root === el || root.contains(el);\n}\n"]}
|