@alloy-js/core 0.11.0 → 0.12.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 (40) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/src/binder.d.ts.map +1 -1
  3. package/dist/src/binder.js +67 -17
  4. package/dist/src/components/For.d.ts +1 -1
  5. package/dist/src/components/For.d.ts.map +1 -1
  6. package/dist/src/jsx-runtime.d.ts.map +1 -1
  7. package/dist/src/jsx-runtime.js +9 -3
  8. package/dist/src/render.d.ts.map +1 -1
  9. package/dist/src/render.js +5 -0
  10. package/dist/src/scheduler.d.ts +8 -0
  11. package/dist/src/scheduler.d.ts.map +1 -0
  12. package/dist/src/scheduler.js +17 -0
  13. package/dist/test/components/declaration.test.js +2 -0
  14. package/dist/test/control-flow/for.test.js +36 -2
  15. package/dist/test/reactivity/circular-reactives.test.d.ts +2 -0
  16. package/dist/test/reactivity/circular-reactives.test.d.ts.map +1 -0
  17. package/dist/test/reactivity/circular-reactives.test.js +31 -0
  18. package/dist/test/reactivity/cleanup.test.js +5 -0
  19. package/dist/test/reactivity/untrack.test.js +3 -0
  20. package/dist/test/rendering/memoization.test.js +2 -0
  21. package/dist/test/symbols.test.js +384 -11
  22. package/dist/test/utils.test.d.ts.map +1 -1
  23. package/dist/test/utils.test.js +2 -0
  24. package/dist/tsconfig.tsbuildinfo +1 -1
  25. package/package.json +2 -2
  26. package/src/binder.ts +100 -17
  27. package/src/components/For.tsx +4 -4
  28. package/src/jsx-runtime.ts +22 -12
  29. package/src/render.ts +5 -0
  30. package/src/scheduler.ts +24 -0
  31. package/temp/api.json +8 -8
  32. package/test/components/declaration.test.tsx +2 -0
  33. package/test/components/list.test.tsx +0 -1
  34. package/test/control-flow/for.test.tsx +34 -4
  35. package/test/reactivity/circular-reactives.test.tsx +32 -0
  36. package/test/reactivity/cleanup.test.tsx +5 -0
  37. package/test/reactivity/untrack.test.ts +3 -0
  38. package/test/rendering/memoization.test.tsx +2 -0
  39. package/test/symbols.test.ts +392 -13
  40. package/test/utils.test.tsx +2 -0
package/src/binder.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import {
2
2
  computed,
3
- effect,
4
3
  reactive,
5
4
  ref,
6
5
  Ref,
@@ -10,8 +9,9 @@ import {
10
9
  import { useBinder } from "./context/binder.js";
11
10
  import { useMemberScope } from "./context/member-scope.js";
12
11
  import { useScope } from "./context/scope.js";
13
- import { memo, untrack } from "./jsx-runtime.js";
12
+ import { effect, memo, onCleanup, untrack } from "./jsx-runtime.js";
14
13
  import { refkey, Refkey } from "./refkey.js";
14
+ import { queueJob, QueueJob } from "./scheduler.js";
15
15
  export type Metadata = object;
16
16
 
17
17
  /**
@@ -464,6 +464,7 @@ export function createOutputBinder(options: BinderOptions = {}): Binder {
464
464
  OutputScope,
465
465
  Map<string, Ref<OutputScope | undefined>>
466
466
  >();
467
+ const deconflictJobs = new Map<OutputScope, Map<string, QueueJob>>();
467
468
 
468
469
  return binder;
469
470
 
@@ -622,26 +623,77 @@ export function createOutputBinder(options: BinderOptions = {}): Binder {
622
623
  }
623
624
 
624
625
  function instantiateSymbolInto(source: OutputSymbol, target: OutputSymbol) {
625
- if (~source.flags & OutputSymbolFlags.InstanceMemberContainer) {
626
- throw new Error("Can only instantiate symbols with instance members");
626
+ if (target.staticMemberScope) {
627
+ return;
627
628
  }
628
629
 
629
- addInstanceMembersToSymbol(target);
630
+ // Ensure static member scope exists
631
+ addStaticMembersToSymbol(target);
630
632
 
631
633
  effect(() => {
632
- for (const sym of source.instanceMemberScope!.symbols) {
633
- if (target.instanceMemberScope!.symbols.has(sym)) {
634
- continue;
635
- }
634
+ // copy instance members if it's an instance‐container
635
+ if (source.flags & OutputSymbolFlags.InstanceMemberContainer) {
636
+ copyMembers(
637
+ source.instanceMemberScope!.symbols,
638
+ target,
639
+ target.staticMemberScope!,
640
+ );
641
+ }
636
642
 
637
- createSymbol({
638
- name: sym.name,
639
- scope: target.instanceMemberScope!,
640
- refkey: [refkey(target.refkeys[0], sym.refkeys[0])],
641
- flags: sym.flags | OutputSymbolFlags.InstanceMember,
642
- });
643
+ // copy static members if it's a static‐container
644
+ if (source.flags & OutputSymbolFlags.StaticMemberContainer) {
645
+ copyMembers(
646
+ source.staticMemberScope!.symbols,
647
+ target,
648
+ target.staticMemberScope!,
649
+ );
643
650
  }
644
651
  });
652
+
653
+ /**
654
+ * Recursively copy `symbols` from `sourceSym` into `intoScope` of `targetSym`.
655
+ * Always marks each instantiation as StaticMember so lookups use dot notation (e.g. Parent.child)
656
+ * and preserves any StaticMemberContainer flag to auto create newSym.staticMemberScope.
657
+ */
658
+ function copyMembers(
659
+ symbols: Set<OutputSymbol>,
660
+ targetSym: OutputSymbol,
661
+ intoScope: OutputScope,
662
+ ) {
663
+ for (const srcSym of symbols) {
664
+ untrack(() => {
665
+ const wantKey = refkey(targetSym.refkeys[0], srcSym.refkeys[0]);
666
+
667
+ // create the new symbol. Preserve StaticMemberContainer if present
668
+ const newSym = createSymbol({
669
+ name: srcSym.name,
670
+ scope: intoScope,
671
+ refkey: wantKey,
672
+ flags: srcSym.flags | OutputSymbolFlags.StaticMember,
673
+ });
674
+
675
+ onCleanup(() => {
676
+ binder.deleteSymbol(newSym);
677
+ });
678
+
679
+ // if the source symbol itself was a container of static members,
680
+ // recurse into the newSym.staticMemberScope that createSymbol just gave us
681
+ if (
682
+ srcSym.staticMemberScope &&
683
+ srcSym.staticMemberScope.symbols.size > 0
684
+ ) {
685
+ // ensure we have that scope
686
+ addStaticMembersToSymbol(newSym);
687
+
688
+ copyMembers(
689
+ srcSym.staticMemberScope.symbols,
690
+ newSym,
691
+ newSym.staticMemberScope!,
692
+ );
693
+ }
694
+ });
695
+ }
696
+ }
645
697
  }
646
698
 
647
699
  function addStaticMembersToSymbol(symbol: OutputSymbol) {
@@ -789,7 +841,7 @@ export function createOutputBinder(options: BinderOptions = {}): Binder {
789
841
  function deconflict(symbol: OutputSymbol) {
790
842
  const scope = symbol.scope;
791
843
  const existingNames = [...scope.symbols].filter(
792
- (sym) => sym.originalName === symbol.name,
844
+ (sym) => sym.name === symbol.name,
793
845
  );
794
846
 
795
847
  if (existingNames.length < 2) {
@@ -797,7 +849,13 @@ export function createOutputBinder(options: BinderOptions = {}): Binder {
797
849
  }
798
850
 
799
851
  if (options.nameConflictResolver) {
800
- options.nameConflictResolver(symbol.name, existingNames);
852
+ queueJob(
853
+ deconflictJobForScopeAndName(
854
+ scope,
855
+ symbol.name,
856
+ options.nameConflictResolver,
857
+ ),
858
+ );
801
859
  } else {
802
860
  // default disambiguation is first-wins
803
861
  for (let i = 1; i < existingNames.length; i++) {
@@ -806,6 +864,31 @@ export function createOutputBinder(options: BinderOptions = {}): Binder {
806
864
  }
807
865
  }
808
866
 
867
+ function deconflictJobForScopeAndName(
868
+ scope: OutputScope,
869
+ name: string,
870
+ handler: NameConflictResolver,
871
+ ) {
872
+ if (!deconflictJobs.has(scope)) {
873
+ deconflictJobs.set(scope, new Map());
874
+ }
875
+
876
+ const jobs = deconflictJobs.get(scope)!;
877
+ if (jobs.has(name)) {
878
+ return jobs.get(name)!;
879
+ }
880
+ const job = () => {
881
+ const conflictedSymbols = [...scope.symbols].filter(
882
+ (sym) => sym.name === name,
883
+ );
884
+ handler(name, conflictedSymbols);
885
+ jobs.delete(name);
886
+ };
887
+
888
+ jobs.set(name, job);
889
+ return job;
890
+ }
891
+
809
892
  function getSymbolForRefkey<TSymbol extends OutputSymbol>(
810
893
  refkey: Refkey,
811
894
  ): Ref<TSymbol | undefined> {
@@ -38,10 +38,10 @@ export interface ForProps<
38
38
  }
39
39
 
40
40
  export type ForSupportedCollections =
41
- | any[]
42
- | Map<any, any>
43
- | Set<any>
44
- | IterableIterator<any>;
41
+ | readonly unknown[]
42
+ | ReadonlyMap<unknown, unknown>
43
+ | ReadonlySet<unknown>
44
+ | IterableIterator<unknown>;
45
45
  /**
46
46
  * The For component iterates over the provided array and invokes the child
47
47
  * callback for each item. It can optionally be provided with a `joiner` which
@@ -5,6 +5,7 @@ import {
5
5
  isReactive,
6
6
  pauseTracking,
7
7
  proxyRefs,
8
+ ReactiveEffectRunner,
8
9
  Ref,
9
10
  resetTracking,
10
11
  shallowRef,
@@ -14,6 +15,7 @@ import {
14
15
  } from "@vue/reactivity";
15
16
  import { Refkey } from "./refkey.js";
16
17
  import { RenderedTextTree } from "./render.js";
18
+ import { scheduler } from "./scheduler.js";
17
19
 
18
20
  if ((globalThis as any).ALLOY) {
19
21
  throw new Error(
@@ -125,21 +127,29 @@ export function effect<T>(fn: (prev?: T) => T, current?: T) {
125
127
  for (let k = 0, len = d.length; k < len; k++) d[k]();
126
128
 
127
129
  // eslint-disable-next-line @typescript-eslint/no-unused-expressions
128
- final && stop(c);
130
+ final && stop(runner);
129
131
  };
130
132
 
131
133
  onCleanup(() => cleanupFn(true));
132
- const c = vueEffect(() => {
133
- cleanupFn(false);
134
-
135
- const oldContext = globalContext;
136
- globalContext = context;
137
- try {
138
- current = fn(current);
139
- } finally {
140
- globalContext = oldContext;
141
- }
142
- }, {});
134
+ const runner: ReactiveEffectRunner<void> = vueEffect(
135
+ () => {
136
+ cleanupFn(false);
137
+
138
+ const oldContext = globalContext;
139
+ globalContext = context;
140
+ try {
141
+ current = fn(current);
142
+ } finally {
143
+ globalContext = oldContext;
144
+ }
145
+ },
146
+ {
147
+ scheduler: scheduler(() => runner),
148
+ },
149
+ );
150
+
151
+ // allow recursive effects (recursive option does nothing, possible bug)
152
+ (runner as any).effect.flags |= 1 << 5;
143
153
  }
144
154
 
145
155
  /**
package/src/render.ts CHANGED
@@ -22,6 +22,7 @@ import {
22
22
  untrack,
23
23
  } from "./jsx-runtime.js";
24
24
  import { isRefkey } from "./refkey.js";
25
+ import { flushJobs } from "./scheduler.js";
25
26
  const {
26
27
  builders: {
27
28
  align,
@@ -181,6 +182,7 @@ export function render(
181
182
  options?: PrintTreeOptions,
182
183
  ): OutputDirectory {
183
184
  const tree = renderTree(children);
185
+ flushJobs();
184
186
  let rootDirectory: OutputDirectory | undefined = undefined;
185
187
 
186
188
  // when passing Output, the first render tree child is the Output component.
@@ -559,6 +561,9 @@ export function printTree(tree: RenderedTextTree, options?: PrintTreeOptions) {
559
561
  ),
560
562
  };
561
563
 
564
+ // make sure queue is empty
565
+ flushJobs();
566
+
562
567
  const d = printTreeWorker(tree);
563
568
  return doc.printer.printDocToString(d, options as doc.printer.Options)
564
569
  .formatted;
@@ -0,0 +1,24 @@
1
+ import { ReactiveEffectRunner } from "@vue/reactivity";
2
+
3
+ export interface QueueJob {
4
+ (): any;
5
+ }
6
+ const queue = new Set<QueueJob>();
7
+
8
+ export function scheduler(jobGetter: () => ReactiveEffectRunner) {
9
+ return () => {
10
+ queueJob(jobGetter());
11
+ };
12
+ }
13
+ export function queueJob(job: QueueJob) {
14
+ // the set is serving an important purpose here in deduping the effects we run
15
+ // (which in effect coalesces multiple update effects together).
16
+ queue.add(job);
17
+ }
18
+
19
+ export function flushJobs() {
20
+ for (const job of queue) {
21
+ queue.delete(job);
22
+ job();
23
+ }
24
+ }
package/temp/api.json CHANGED
@@ -6003,25 +6003,25 @@
6003
6003
  },
6004
6004
  {
6005
6005
  "kind": "Content",
6006
- "text": "any[] | "
6006
+ "text": "readonly unknown[] | "
6007
6007
  },
6008
6008
  {
6009
6009
  "kind": "Reference",
6010
- "text": "Map",
6011
- "canonicalReference": "!Map:interface"
6010
+ "text": "ReadonlyMap",
6011
+ "canonicalReference": "!ReadonlyMap:interface"
6012
6012
  },
6013
6013
  {
6014
6014
  "kind": "Content",
6015
- "text": "<any, any> | "
6015
+ "text": "<unknown, unknown> | "
6016
6016
  },
6017
6017
  {
6018
6018
  "kind": "Reference",
6019
- "text": "Set",
6020
- "canonicalReference": "!Set:interface"
6019
+ "text": "ReadonlySet",
6020
+ "canonicalReference": "!ReadonlySet:interface"
6021
6021
  },
6022
6022
  {
6023
6023
  "kind": "Content",
6024
- "text": "<any> | "
6024
+ "text": "<unknown> | "
6025
6025
  },
6026
6026
  {
6027
6027
  "kind": "Reference",
@@ -6030,7 +6030,7 @@
6030
6030
  },
6031
6031
  {
6032
6032
  "kind": "Content",
6033
- "text": "<any>"
6033
+ "text": "<unknown>"
6034
6034
  },
6035
6035
  {
6036
6036
  "kind": "Content",
@@ -7,6 +7,7 @@ import {
7
7
  Scope,
8
8
  useBinder,
9
9
  } from "../../src/index.js";
10
+ import { flushJobs } from "../../src/scheduler.js";
10
11
  import { createTap } from "../../src/tap.js";
11
12
 
12
13
  it("creates and cleans up a symbol", () => {
@@ -33,5 +34,6 @@ it("creates and cleans up a symbol", () => {
33
34
  const subScope = [...binder.globalScope.children][0];
34
35
  expect(subScope.symbols.size).toBe(1);
35
36
  doDecl.value = false;
37
+ flushJobs();
36
38
  expect(subScope.symbols.size).toBe(0);
37
39
  });
@@ -73,7 +73,6 @@ it("is useful for statements", () => {
73
73
  <Statement />
74
74
  </List>,
75
75
  );
76
-
77
76
  expect(printTree(tree)).toEqual(d`
78
77
  console.log(true);
79
78
  console.log(true);
@@ -1,8 +1,9 @@
1
1
  import "@alloy-js/core/testing";
2
2
  import { d } from "@alloy-js/core/testing";
3
- import { expect, it } from "vitest";
3
+ import { describe, expect, it } from "vitest";
4
4
  import { For } from "../../src/components/For.jsx";
5
5
  import { onCleanup, printTree, reactive, renderTree } from "../../src/index.js";
6
+ import { flushJobs } from "../../src/scheduler.js";
6
7
 
7
8
  it("works", () => {
8
9
  const messages = ["hi", "bye"];
@@ -17,6 +18,30 @@ it("works", () => {
17
18
  `);
18
19
  });
19
20
 
21
+ describe("readonly collections", () => {
22
+ const out = d`
23
+ a
24
+ b
25
+ `;
26
+ it("array", () => {
27
+ const messages: readonly string[] = ["a", "b"];
28
+ expect(<For each={messages}>{(x) => <>{x}</>}</For>).toRenderTo(out);
29
+ });
30
+
31
+ it("map", () => {
32
+ const messages: ReadonlyMap<string, string> = new Map([
33
+ ["a", "a"],
34
+ ["b", "b"],
35
+ ]);
36
+ expect(<For each={messages}>{(x) => <>{x}</>}</For>).toRenderTo(out);
37
+ });
38
+
39
+ it("set", () => {
40
+ const messages: ReadonlySet<string> = new Set(["a", "b"]);
41
+ expect(<For each={messages}>{(x) => <>{x}</>}</For>).toRenderTo(out);
42
+ });
43
+ });
44
+
20
45
  it("handles map entries", () => {
21
46
  const map = new Map([["a", { name: "foo" }]]);
22
47
  const entries = Array.from(map.entries());
@@ -76,6 +101,7 @@ it("doesn't rerender mappers", () => {
76
101
  expect(count).toBe(2);
77
102
 
78
103
  messages.push("maybe");
104
+ flushJobs();
79
105
 
80
106
  expect(count).toBe(3);
81
107
  expect(printTree(tree)).toBe(d`
@@ -93,7 +119,7 @@ it("doesn't rerender mappers with sets", () => {
93
119
  expect(count).toBe(2);
94
120
 
95
121
  messages.add("maybe");
96
-
122
+ flushJobs();
97
123
  expect(count).toBe(3);
98
124
  expect(printTree(tree)).toBe(d`
99
125
  item 0
@@ -116,7 +142,7 @@ it("doesn't rerender mappers with maps", () => {
116
142
  expect(count).toBe(2);
117
143
 
118
144
  messages.set("maybe", "three");
119
-
145
+ flushJobs();
120
146
  expect(count).toBe(3);
121
147
  expect(printTree(tree)).toBe(d`
122
148
  item 0
@@ -132,9 +158,11 @@ it("doesn't rerender mappers (with splice)", () => {
132
158
  const tree = renderTree(template);
133
159
  expect(count).toBe(3);
134
160
  messages.splice(1, 1);
161
+ flushJobs();
135
162
  // A sufficiently smart mapJoin would be able to handle this case...
136
163
  // but for now we re-render everything after the splice point.
137
164
  expect(count).toBe(4);
165
+
138
166
  expect(printTree(tree)).toBe(d`
139
167
  item 0
140
168
  item 3
@@ -165,12 +193,14 @@ it("cleans up things which end up removed (with push)", () => {
165
193
  `);
166
194
 
167
195
  items.pop();
196
+ flushJobs();
168
197
  expect(cleanups).toEqual(["b"]);
169
198
  expect(printTree(tree)).toBe(d`
170
199
  Letter a
171
200
  `);
172
201
 
173
202
  items.pop();
203
+ flushJobs();
174
204
  expect(cleanups).toEqual(["b", "a"]);
175
205
  expect(printTree(tree)).toBe("");
176
206
  });
@@ -200,7 +230,7 @@ it("cleans up things which end up removed (with splice)", () => {
200
230
  `);
201
231
 
202
232
  items.splice(1, 1);
203
-
233
+ flushJobs();
204
234
  // A sufficiently smart mapJoin would be able to handle this case...
205
235
  // but for now we re-render everything after the splice point.
206
236
  expect(cleanups).toEqual(["b", "c"]);
@@ -0,0 +1,32 @@
1
+ import { shallowReactive } from "@vue/reactivity";
2
+ import { expect, it } from "vitest";
3
+ import { For } from "../../src/index.js";
4
+ import { printTree, renderTree } from "../../src/render.js";
5
+ import { d } from "../../testing/render.js";
6
+
7
+ it("it should work with circular reactives", () => {
8
+ const items: string[] = shallowReactive([]);
9
+ let added = false;
10
+ function MaybeAddString(props: any) {
11
+ if (!added) {
12
+ items.push("item " + items.length);
13
+ added = true;
14
+ }
15
+ return <>{props.item}</>;
16
+ }
17
+ const template = (
18
+ <>
19
+ <For each={items}>
20
+ {(item) => {
21
+ return <MaybeAddString item={item} />;
22
+ }}
23
+ </For>
24
+ </>
25
+ );
26
+ const tree = renderTree(template);
27
+ items.push("item start");
28
+ expect(printTree(tree)).toBe(d`
29
+ item start
30
+ item 1
31
+ `);
32
+ });
@@ -2,6 +2,7 @@ import { Children, effect, memo, onCleanup } from "@alloy-js/core/jsx-runtime";
2
2
  import { ref } from "@vue/reactivity";
3
3
  import { describe, expect, it } from "vitest";
4
4
  import { renderTree } from "../../src/render.js";
5
+ import { flushJobs } from "../../src/scheduler.js";
5
6
 
6
7
  describe("memo cleanup", () => {
7
8
  it("cleans up when memo value is recomputed", () => {
@@ -19,6 +20,7 @@ describe("memo cleanup", () => {
19
20
  expect(callCount).toBe(0);
20
21
 
21
22
  r.value = 2;
23
+ flushJobs();
22
24
 
23
25
  expect(m()).toBe(2);
24
26
  expect(callCount).toBe(1);
@@ -40,6 +42,7 @@ describe("effect cleanup", () => {
40
42
  expect(cleanedUp).toBe(false);
41
43
 
42
44
  r.value = 2;
45
+ flushJobs();
43
46
 
44
47
  expect(cleanedUp).toBe(true);
45
48
  });
@@ -58,6 +61,7 @@ describe("element cleanup", () => {
58
61
  const template = <>{el}</>;
59
62
  renderTree(template);
60
63
  el.value = "";
64
+ flushJobs();
61
65
  expect(cleanedUp).toBe(true);
62
66
  });
63
67
 
@@ -85,6 +89,7 @@ describe("element cleanup", () => {
85
89
  const template = <>{el}</>;
86
90
  renderTree(template);
87
91
  el.value = "";
92
+ flushJobs();
88
93
  expect(cleanedUpC1).toBe(true);
89
94
  expect(cleanedUpC2).toBe(true);
90
95
  });
@@ -1,6 +1,7 @@
1
1
  import { ref } from "@vue/reactivity";
2
2
  import { expect, it } from "vitest";
3
3
  import { memo, untrack } from "../../src/jsx-runtime.js";
4
+ import { flushJobs } from "../../src/scheduler.js";
4
5
 
5
6
  it("ignores signals for dependency tracking", () => {
6
7
  const signal = ref(0);
@@ -12,6 +13,7 @@ it("ignores signals for dependency tracking", () => {
12
13
  expect(m()).toBe(0);
13
14
 
14
15
  signal.value = 1;
16
+ flushJobs();
15
17
 
16
18
  expect(m()).toBe(0);
17
19
  });
@@ -28,6 +30,7 @@ it("doesn't affect signal changes", () => {
28
30
  untrack(() => {
29
31
  signal.value = 1;
30
32
  });
33
+ flushJobs();
31
34
 
32
35
  expect(m()).toBe(1);
33
36
  });
@@ -2,6 +2,7 @@ import { memo } from "@alloy-js/core/jsx-runtime";
2
2
  import { ref } from "@vue/reactivity";
3
3
  import { expect, it } from "vitest";
4
4
  import { renderTree } from "../../src/render.js";
5
+ import { flushJobs } from "../../src/scheduler.js";
5
6
 
6
7
  it("memoizes child components", () => {
7
8
  let renderCount = 0;
@@ -26,5 +27,6 @@ it("memoizes child components", () => {
26
27
 
27
28
  renderTree(template);
28
29
  doThing.value = true;
30
+ flushJobs();
29
31
  expect(renderCount).toBe(2);
30
32
  });