@storylet-studio/runtime 0.3.0 → 0.4.1

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/dist/index.cjs CHANGED
@@ -633,6 +633,7 @@ var loadPartition = (p, values) => {
633
633
  var Engine = class {
634
634
  internals;
635
635
  seed;
636
+ onReplacedFlow;
636
637
  flowsById = /* @__PURE__ */ new Map();
637
638
  engineTraceHandlers = /* @__PURE__ */ new Set();
638
639
  /** The host's @world binding, if the engine was built with one: it
@@ -641,6 +642,7 @@ var Engine = class {
641
642
  hostWorld;
642
643
  constructor(bundle, opts = {}) {
643
644
  this.seed = opts.seed ?? 0;
645
+ this.onReplacedFlow = opts.onReplacedFlow;
644
646
  if (opts.world !== void 0) this.hostWorld = opts.world;
645
647
  const internals = {
646
648
  bundle,
@@ -661,6 +663,7 @@ var Engine = class {
661
663
  flowDecls: { story: [], box: /* @__PURE__ */ new Map(), deck: /* @__PURE__ */ new Map(), hand: /* @__PURE__ */ new Map(), value: /* @__PURE__ */ new Map() },
662
664
  shared: void 0,
663
665
  worldResolver: void 0,
666
+ worldReadOnly: /* @__PURE__ */ new Set(),
664
667
  emitEngine: (flow, event, turn) => {
665
668
  if (this.internals.logCap !== void 0) {
666
669
  this.engineLog.push({ ...event, flow, seq: this.engineSeq++, ...turn !== void 0 ? { turn } : {} });
@@ -718,6 +721,7 @@ var Engine = class {
718
721
  initShared(hostWorld) {
719
722
  const internals = this.internals;
720
723
  internals.shared = buildPartition(internals, sharedHalf);
724
+ internals.worldReadOnly = new Set(internals.bundle.world.properties.filter((d) => d.writable === false).map((d) => d.name));
721
725
  if (hostWorld !== void 0) {
722
726
  internals.worldResolver = hostWorld;
723
727
  } else {
@@ -761,7 +765,12 @@ var Engine = class {
761
765
  * state; shared state is untouched. There is no default flow: "main" is
762
766
  * a caller convention, not an engine rule. */
763
767
  openFlow(id, opts = {}) {
764
- this.flowsById.get(id)?.markClosed();
768
+ const existing = this.flowsById.get(id);
769
+ if (existing) {
770
+ const dealt = existing.heldCardIds().length;
771
+ if (dealt > 0) this.onReplacedFlow?.(id, dealt);
772
+ existing.markClosed();
773
+ }
765
774
  const flow = new Flow(this, this.internals, id, opts.seed ?? this.seed);
766
775
  this.flowsById.set(id, flow);
767
776
  return flow;
@@ -1727,6 +1736,7 @@ var Flow = class {
1727
1736
  case "world": {
1728
1737
  const resolver = this.internals.worldResolver;
1729
1738
  if (!resolver.set) throw new Error(`@world.${name} cannot be written: the host bound @world read-only`);
1739
+ if (this.internals.worldReadOnly.has(name)) throw new Error(`'@world.${name}' is read-only (writable: false)`);
1730
1740
  const prev = resolver.get(name);
1731
1741
  resolver.set(name, value);
1732
1742
  return { path: `world.${name}`, ...prev !== void 0 ? { prev } : {} };