@istar-ts/core 0.13.4 → 0.13.6

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/README.md CHANGED
@@ -37,6 +37,9 @@ writeFileSync('model.pistar', toPistar(model));
37
37
  - Pass `{ saveDate: new Date() }` when saving an edited model (piStar updates `saveDate` on save).
38
38
  Omit it to preserve the model’s existing `saveDate` on a load/save cycle; a file that had none
39
39
  keeps none until a save passes one.
40
+ - A model's metamodel and layout are kept beside it, in registries shared by every copy of
41
+ `@istar-ts/core` in the process (ESM and CommonJS builds alike), so a model parsed by one copy
42
+ is read correctly by another.
40
43
  - A parsed file is written back with its own key order at every level (top level, each element,
41
44
  link, hub entry and the diagram), so files other tools wrote stay byte-identical. `inheritSourceLayout`
42
45
  carries that over to an edited model; the store does it on every edit.
package/dist/index.cjs CHANGED
@@ -551,6 +551,26 @@ function elementBehavesLike(metamodel, kind, ancestor) {
551
551
  return false;
552
552
  }
553
553
  //#endregion
554
+ //#region src/shared.ts
555
+ /**
556
+ * Registries the library keeps beside models (their metamodel, their file's layout) are
557
+ * shared across every copy of `@istar-ts/core` loaded in a process. A bundle may load the
558
+ * package twice, once as ESM and once as CommonJS (an application parsing with the ESM build
559
+ * while a CommonJS dependency reads the model with the CJS build): a module-level WeakMap in
560
+ * each copy would make a model parsed by one copy unknown to the other, so `metamodelOf`
561
+ * would fall back to iStar 2.0 there. The registries live on `globalThis` under one symbol
562
+ * instead, keyed by name, and every copy finds the same ones.
563
+ */
564
+ const REGISTRIES = Symbol.for("@istar-ts/core/registries");
565
+ /** The WeakMap named `name`, the same object in every copy of the library in this process. */
566
+ function sharedWeakMap(name) {
567
+ const host = globalThis;
568
+ const registries = host[REGISTRIES] ??= /* @__PURE__ */ new Map();
569
+ let map = registries.get(name);
570
+ if (!map) registries.set(name, map = /* @__PURE__ */ new WeakMap());
571
+ return map;
572
+ }
573
+ //#endregion
554
574
  //#region src/model.ts
555
575
  /** Tool identifier written by piStar 2.1.0, whose file format this library emulates. */
556
576
  const PISTAR_TOOL = "pistar.2.1.0";
@@ -581,7 +601,7 @@ const DEFAULT_DIAGRAM = {
581
601
  * so a model's shape (and `JSON.stringify`, deep equality, snapshots) is the same as before
582
602
  * metamodels existed. Operations and the store carry it to every model they derive.
583
603
  */
584
- const metamodels = /* @__PURE__ */ new WeakMap();
604
+ const metamodels = sharedWeakMap("metamodels");
585
605
  /**
586
606
  * Associates `metamodel` with `model` and returns the same model, typed for it. Use when a
587
607
  * model was built without the library's operations (e.g. with an object spread), which loses
@@ -596,7 +616,7 @@ function withMetamodel(model, metamodel) {
596
616
  * For models whose file declares part of their metamodel (see `withFileMetamodel`): the
597
617
  * metamodel the host supplied, without the file's kinds.
598
618
  */
599
- const hostMetamodels = /* @__PURE__ */ new WeakMap();
619
+ const hostMetamodels = sharedWeakMap("hostMetamodels");
600
620
  /** @internal Records the host part of a model's metamodel (used by serialization). */
601
621
  function setHostMetamodel(model, metamodel) {
602
622
  if (metamodel) hostMetamodels.set(model, metamodel);
@@ -1241,7 +1261,7 @@ function hubEntryLayoutKey(hubId) {
1241
1261
  }
1242
1262
  /** The `objects` key of the diagram in a `SourceLayout`. */
1243
1263
  const DIAGRAM_LAYOUT_KEY = "\0diagram";
1244
- const layouts = /* @__PURE__ */ new WeakMap();
1264
+ const layouts = sharedWeakMap("layouts");
1245
1265
  /** The key order recorded when `model` was parsed, if it came from `parsePistar`. */
1246
1266
  function getSourceLayout(model) {
1247
1267
  return layouts.get(model);