@tinyactors/scxmljs 0.1.0 → 0.2.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 (51) hide show
  1. package/README.md +11 -4
  2. package/dist/explorer/element.d.ts +5 -3
  3. package/dist/explorer/element.js +523 -262
  4. package/dist/explorer/element.js.map +1 -1
  5. package/dist/explorer/strings.d.ts +41 -23
  6. package/dist/explorer/strings.js +24 -15
  7. package/dist/explorer/strings.js.map +1 -1
  8. package/dist/explorer/styles.d.ts +9 -1
  9. package/dist/explorer/styles.js +291 -150
  10. package/dist/explorer/styles.js.map +1 -1
  11. package/dist/explorer.d.ts +1 -0
  12. package/dist/index.d.ts +1 -0
  13. package/dist/model.d.ts +4 -1
  14. package/dist/model.js +7 -1
  15. package/dist/model.js.map +1 -1
  16. package/dist/session.d.ts +22 -9
  17. package/dist/session.js +53 -13
  18. package/dist/session.js.map +1 -1
  19. package/dist/themes/tinyactors.css +1 -0
  20. package/dist/trusted.d.ts +1 -0
  21. package/dist/ui/icons.d.ts +31 -0
  22. package/dist/ui/icons.js +186 -0
  23. package/dist/ui/icons.js.map +1 -0
  24. package/dist/ui/notation.d.ts +53 -0
  25. package/dist/ui/notation.js +117 -0
  26. package/dist/ui/notation.js.map +1 -0
  27. package/dist/ui/svg.d.ts +4 -0
  28. package/dist/ui/svg.js +26 -0
  29. package/dist/ui/svg.js.map +1 -1
  30. package/dist/ui/theme.d.ts +1 -1
  31. package/dist/ui/theme.js +6 -0
  32. package/dist/ui/theme.js.map +1 -1
  33. package/dist/view/describe.d.ts +17 -0
  34. package/dist/view/describe.js +49 -0
  35. package/dist/view/describe.js.map +1 -0
  36. package/dist/view/element.d.ts +39 -9
  37. package/dist/view/element.js +723 -86
  38. package/dist/view/element.js.map +1 -1
  39. package/dist/view/layout.d.ts +20 -0
  40. package/dist/view/layout.js +205 -29
  41. package/dist/view/layout.js.map +1 -1
  42. package/dist/view/strings.d.ts +34 -8
  43. package/dist/view/strings.js +19 -6
  44. package/dist/view/strings.js.map +1 -1
  45. package/dist/view/styles.d.ts +1 -1
  46. package/dist/view/styles.js +107 -21
  47. package/dist/view/styles.js.map +1 -1
  48. package/dist/view.d.ts +8 -4
  49. package/dist/view.js +6 -3
  50. package/dist/view.js.map +1 -1
  51. package/package.json +2 -2
package/dist/session.js CHANGED
@@ -38,10 +38,12 @@ export function resolveModel(source, opts = {}) {
38
38
  * Parse SCXML text into its `<scxml>` element.
39
39
  *
40
40
  * Browsers have a `DOMParser`; Node, Bun and Deno don't, so pass one there
41
- * (any standards-compliant implementation, e.g. happy-dom's or linkedom's).
41
+ * (any standards-compliant XML implementation, e.g. happy-dom's; linkedom's
42
+ * ignores XML namespaces, so it can't read SCXML).
42
43
  *
43
44
  * @throws {SCXMLParseError} `code: "SCXML_NO_DOMPARSER"` when no parser is
44
- * available, `code: "SCXML_PARSE"` when the text isn't well-formed XML.
45
+ * available, `code: "SCXML_PARSE"` when the text isn't well-formed XML or
46
+ * the parser doesn't implement XML namespaces.
45
47
  */
46
48
  export function parseSCXML(source, domParser) {
47
49
  const parser = domParser ?? (typeof DOMParser !== "undefined" ? new DOMParser() : undefined);
@@ -57,9 +59,15 @@ export function parseSCXML(source, domParser) {
57
59
  ].join("\n"));
58
60
  const doc = parser.parseFromString(source, "application/xml");
59
61
  const err = doc.getElementsByTagName("parsererror")[0];
60
- if (err)
61
- throw new SCXMLParseError("SCXML_PARSE", `SCXML is not well-formed XML: ${err.textContent?.trim()}`);
62
- return doc.documentElement;
62
+ if (err || !doc.documentElement)
63
+ throw new SCXMLParseError("SCXML_PARSE", `SCXML is not well-formed XML${err ? `: ${err.textContent?.trim()}` : ""}`);
64
+ const root = doc.documentElement;
65
+ const declared = root.getAttribute("xmlns");
66
+ // a parser without XML namespaces (linkedom) reads every element as XHTML
67
+ if (declared && root.namespaceURI !== declared)
68
+ throw new SCXMLParseError("SCXML_PARSE", `The DOMParser ignores XML namespaces: <${root.localName} xmlns="${declared}"> came back in ${root.namespaceURI ?? "no namespace"}. ` +
69
+ "SCXML needs a standards-compliant XML parser, such as happy-dom's.");
70
+ return root;
63
71
  }
64
72
  // ──────────────────────────────── session ─────────────────────────────────
65
73
  /**
@@ -203,10 +211,19 @@ export class SCXMLSession extends EventTarget {
203
211
  activeStateIds() {
204
212
  return this.configuration.map((s) => s.id);
205
213
  }
206
- /** Whether the state with this id is active. */
214
+ /**
215
+ * Whether the state with this id is active.
216
+ *
217
+ * @throws {RangeError} when the chart has no state with this id.
218
+ */
207
219
  isActive(id) {
220
+ return this.configurationSet.has(this.stateById(id));
221
+ }
222
+ stateById(id) {
208
223
  const s = this.model.byId.get(id);
209
- return !!s && this.configurationSet.has(s);
224
+ if (!s)
225
+ throw new RangeError(`no state "${id}" in ${this.model.name ? `chart "${this.model.name}"` : "the chart"}`);
226
+ return s;
210
227
  }
211
228
  /** Whether this state node is active. */
212
229
  isActiveNode(s) {
@@ -243,7 +260,11 @@ export class SCXMLSession extends EventTarget {
243
260
  return {};
244
261
  return this.datamodel.snapshot(this.model.allData.map((d) => d.getAttribute("id")));
245
262
  }
246
- /** Enqueue an external event. */
263
+ /**
264
+ * Enqueue an external event. Events sent before `start()` wait in the queue
265
+ * and are processed, in order, once the session starts; events sent after
266
+ * it has terminated are dropped.
267
+ */
247
268
  send(name, data) {
248
269
  this.enqueueExternal({ name, type: "external", data });
249
270
  }
@@ -251,6 +272,8 @@ export class SCXMLSession extends EventTarget {
251
272
  start() {
252
273
  if (this.started)
253
274
  return this;
275
+ if (this.disposedDm)
276
+ throw new Error(`session ${this.sessionId} was disposed before it started: create a new session`);
254
277
  this.started = true;
255
278
  this.running = true;
256
279
  sessions.set(this.sessionId, this);
@@ -293,9 +316,12 @@ export class SCXMLSession extends EventTarget {
293
316
  * listeners installed by `connect()` / `bind()` and attributes set by
294
317
  * `reflect`, frees the data model context and leaves the session registry.
295
318
  * `done` resolves (with `undefined` if it hadn't finished). Safe to call
296
- * more than once; the session can't be used afterwards.
319
+ * more than once; the session can't be used afterwards (`start()` throws).
297
320
  */
298
321
  dispose() {
322
+ // never started: it ends here, cancelled, without having run
323
+ if (!this.started)
324
+ this.exited = this.wasCancelled = true;
299
325
  this.cancel();
300
326
  this.bridge?.cleanup();
301
327
  this.lifetime.abort();
@@ -359,11 +385,20 @@ export class SCXMLSession extends EventTarget {
359
385
  * (the configuration is cleared right after that, per the spec).
360
386
  *
361
387
  * `what`: a state id (active), several ids (all active) or a predicate.
362
- * Rejects with `options.signal.reason` on abort, a `TimeoutError`
388
+ * Rejects with a `RangeError` at once when an id names no state of the
389
+ * chart, with `options.signal.reason` on abort, a `TimeoutError`
363
390
  * DOMException after `timeoutMs` (measured on the session's clock), or an
364
391
  * Error when the session terminates first.
365
392
  */
366
393
  waitFor(what, options = {}) {
394
+ if (typeof what !== "function")
395
+ try {
396
+ for (const id of typeof what === "string" ? [what] : what)
397
+ this.stateById(id);
398
+ }
399
+ catch (e) {
400
+ return Promise.reject(e);
401
+ }
367
402
  const test = typeof what === "function" ? what : (s) => (typeof what === "string" ? [what] : what).every((id) => s.isActive(id));
368
403
  const describe = typeof what === "function" ? "the condition" : `[${typeof what === "string" ? what : what.join(", ")}]`;
369
404
  const { signal, timeoutMs } = options;
@@ -472,7 +507,10 @@ export class SCXMLSession extends EventTarget {
472
507
  return iterator;
473
508
  }
474
509
  isIdle() {
475
- return this.exited || this.disposedDm || (!this.busy && !this.scheduled && !this.externalQueue.length && !this.internalQueue.length);
510
+ return (!this.started ||
511
+ this.exited ||
512
+ this.disposedDm ||
513
+ (!this.busy && !this.scheduled && !this.externalQueue.length && !this.internalQueue.length));
476
514
  }
477
515
  checkSettled() {
478
516
  if (this.settleWaiters.length && this.isIdle())
@@ -577,10 +615,12 @@ export class SCXMLSession extends EventTarget {
577
615
  this.checkSettled();
578
616
  }
579
617
  enqueueExternal(ev) {
580
- if (!this.running)
618
+ // before start() events wait in the queue; once the session has ended they are dropped
619
+ if (this.exited || this.disposedDm || (this.started && !this.running))
581
620
  return;
582
621
  this.externalQueue.push(ev);
583
- this.schedule();
622
+ if (this.started)
623
+ this.schedule();
584
624
  }
585
625
  enqueueInternal(ev) {
586
626
  this.internalQueue.push(ev);