@alloy-js/core 0.5.0 → 0.7.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 (212) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/babel.config.cjs +4 -1
  3. package/dist/src/binder.d.ts +8 -2
  4. package/dist/src/binder.d.ts.map +1 -1
  5. package/dist/src/binder.js +41 -15
  6. package/dist/src/binder.js.map +1 -1
  7. package/dist/src/code.d.ts +2 -2
  8. package/dist/src/code.d.ts.map +1 -1
  9. package/dist/src/code.js +4 -4
  10. package/dist/src/code.js.map +1 -1
  11. package/dist/src/components/Block.d.ts +25 -0
  12. package/dist/src/components/Block.d.ts.map +1 -0
  13. package/dist/src/components/Block.js +25 -0
  14. package/dist/src/components/Block.js.map +1 -0
  15. package/dist/src/components/Declaration.d.ts.map +1 -1
  16. package/dist/src/components/Declaration.js +4 -0
  17. package/dist/src/components/Declaration.js.map +1 -1
  18. package/dist/src/components/For.d.ts +44 -0
  19. package/dist/src/components/For.d.ts.map +1 -0
  20. package/dist/src/components/For.js +41 -0
  21. package/dist/src/components/For.js.map +1 -0
  22. package/dist/src/components/Indent.d.ts +5 -9
  23. package/dist/src/components/Indent.d.ts.map +1 -1
  24. package/dist/src/components/Indent.js +7 -18
  25. package/dist/src/components/Indent.js.map +1 -1
  26. package/dist/src/components/List.d.ts +38 -0
  27. package/dist/src/components/List.d.ts.map +1 -0
  28. package/dist/src/components/List.js +40 -0
  29. package/dist/src/components/List.js.map +1 -0
  30. package/dist/src/components/MemberDeclaration.d.ts.map +1 -1
  31. package/dist/src/components/MemberDeclaration.js.map +1 -1
  32. package/dist/src/components/MemberName.js +1 -1
  33. package/dist/src/components/MemberName.js.map +1 -1
  34. package/dist/src/components/MemberScope.d.ts.map +1 -1
  35. package/dist/src/components/MemberScope.js.map +1 -1
  36. package/dist/src/components/Name.js +1 -1
  37. package/dist/src/components/Name.js.map +1 -1
  38. package/dist/src/components/Output.d.ts +2 -1
  39. package/dist/src/components/Output.d.ts.map +1 -1
  40. package/dist/src/components/Output.js +9 -1
  41. package/dist/src/components/Output.js.map +1 -1
  42. package/dist/src/components/Scope.d.ts.map +1 -1
  43. package/dist/src/components/Scope.js.map +1 -1
  44. package/dist/src/components/Show.d.ts +8 -0
  45. package/dist/src/components/Show.d.ts.map +1 -0
  46. package/dist/src/components/Show.js +4 -0
  47. package/dist/src/components/Show.js.map +1 -0
  48. package/dist/src/components/SourceDirectory.d.ts.map +1 -1
  49. package/dist/src/components/SourceDirectory.js +1 -0
  50. package/dist/src/components/SourceDirectory.js.map +1 -1
  51. package/dist/src/components/SourceFile.d.ts +2 -6
  52. package/dist/src/components/SourceFile.d.ts.map +1 -1
  53. package/dist/src/components/SourceFile.js +6 -13
  54. package/dist/src/components/SourceFile.js.map +1 -1
  55. package/dist/src/components/StatementList.d.ts +9 -0
  56. package/dist/src/components/StatementList.d.ts.map +1 -0
  57. package/dist/src/components/StatementList.js +17 -0
  58. package/dist/src/components/StatementList.js.map +1 -0
  59. package/dist/src/components/Switch.d.ts +41 -0
  60. package/dist/src/components/Switch.d.ts.map +1 -0
  61. package/dist/src/components/Switch.js +41 -0
  62. package/dist/src/components/Switch.js.map +1 -0
  63. package/dist/src/components/Wrap.d.ts +20 -0
  64. package/dist/src/components/Wrap.d.ts.map +1 -0
  65. package/dist/src/components/Wrap.js +15 -0
  66. package/dist/src/components/Wrap.js.map +1 -0
  67. package/dist/src/components/index.d.ts +8 -1
  68. package/dist/src/components/index.d.ts.map +1 -1
  69. package/dist/src/components/index.js +7 -0
  70. package/dist/src/components/index.js.map +1 -1
  71. package/dist/src/components/stc/index.d.ts +77 -6
  72. package/dist/src/components/stc/index.d.ts.map +1 -1
  73. package/dist/src/components/stc/index.js +17 -1
  74. package/dist/src/components/stc/index.js.map +1 -1
  75. package/dist/src/context/index.d.ts +0 -1
  76. package/dist/src/context/index.d.ts.map +1 -1
  77. package/dist/src/context/index.js +0 -1
  78. package/dist/src/context/index.js.map +1 -1
  79. package/dist/src/context.d.ts.map +1 -1
  80. package/dist/src/context.js +3 -3
  81. package/dist/src/context.js.map +1 -1
  82. package/dist/src/index.browser.d.ts +3 -0
  83. package/dist/src/index.browser.d.ts.map +1 -0
  84. package/dist/src/index.browser.js +3 -0
  85. package/dist/src/index.browser.js.map +1 -0
  86. package/dist/src/index.d.ts +1 -0
  87. package/dist/src/index.d.ts.map +1 -1
  88. package/dist/src/index.js +1 -0
  89. package/dist/src/index.js.map +1 -1
  90. package/dist/src/jsx-runtime.d.ts +139 -8
  91. package/dist/src/jsx-runtime.d.ts.map +1 -1
  92. package/dist/src/jsx-runtime.js +102 -12
  93. package/dist/src/jsx-runtime.js.map +1 -1
  94. package/dist/src/render.d.ts +107 -132
  95. package/dist/src/render.d.ts.map +1 -1
  96. package/dist/src/render.js +281 -177
  97. package/dist/src/render.js.map +1 -1
  98. package/dist/src/stc.d.ts +14 -0
  99. package/dist/src/stc.d.ts.map +1 -0
  100. package/dist/src/stc.js +52 -0
  101. package/dist/src/stc.js.map +1 -0
  102. package/dist/src/utils.d.ts +22 -15
  103. package/dist/src/utils.d.ts.map +1 -1
  104. package/dist/src/utils.js +95 -59
  105. package/dist/src/utils.js.map +1 -1
  106. package/dist/src/write-output.js +3 -3
  107. package/dist/src/write-output.js.map +1 -1
  108. package/dist/test/browser-build.test.d.ts +2 -0
  109. package/dist/test/browser-build.test.d.ts.map +1 -0
  110. package/dist/test/components/block.test.d.ts +2 -0
  111. package/dist/test/components/block.test.d.ts.map +1 -0
  112. package/dist/test/components/declaration.test.d.ts +2 -0
  113. package/dist/test/components/declaration.test.d.ts.map +1 -0
  114. package/dist/test/components/list.test.d.ts +2 -0
  115. package/dist/test/components/list.test.d.ts.map +1 -0
  116. package/dist/test/components/wrap.test.d.ts +2 -0
  117. package/dist/test/components/wrap.test.d.ts.map +1 -0
  118. package/dist/test/control-flow/for.test.d.ts +2 -0
  119. package/dist/test/control-flow/for.test.d.ts.map +1 -0
  120. package/dist/test/control-flow/match.test.d.ts +2 -0
  121. package/dist/test/control-flow/match.test.d.ts.map +1 -0
  122. package/dist/test/control-flow/show.test.d.ts +2 -0
  123. package/dist/test/control-flow/show.test.d.ts.map +1 -0
  124. package/dist/test/reactivity/cleanup.test.d.ts +2 -0
  125. package/dist/test/reactivity/cleanup.test.d.ts.map +1 -0
  126. package/dist/test/reactivity/memo.test.d.ts +2 -0
  127. package/dist/test/reactivity/memo.test.d.ts.map +1 -0
  128. package/dist/test/reactivity/untrack.test.d.ts +2 -0
  129. package/dist/test/reactivity/untrack.test.d.ts.map +1 -0
  130. package/dist/test/rendering/formatting.test.d.ts +2 -0
  131. package/dist/test/rendering/formatting.test.d.ts.map +1 -0
  132. package/dist/test/rendering/memoization.test.d.ts +2 -0
  133. package/dist/test/rendering/memoization.test.d.ts.map +1 -0
  134. package/dist/test/split-props.test.d.ts +2 -0
  135. package/dist/test/split-props.test.d.ts.map +1 -0
  136. package/dist/test/stc.test.d.ts.map +1 -1
  137. package/dist/test/utils.test.d.ts.map +1 -1
  138. package/dist/testing/extend-expect.js +4 -4
  139. package/dist/testing/extend-expect.js.map +1 -1
  140. package/dist/testing/render.d.ts +2 -3
  141. package/dist/testing/render.d.ts.map +1 -1
  142. package/dist/testing/render.js +2 -4
  143. package/dist/testing/render.js.map +1 -1
  144. package/dist/tsconfig.tsbuildinfo +1 -1
  145. package/package.json +6 -8
  146. package/src/binder.ts +54 -18
  147. package/src/code.ts +17 -12
  148. package/src/components/Block.tsx +44 -0
  149. package/src/components/Declaration.tsx +10 -4
  150. package/src/components/For.tsx +81 -0
  151. package/src/components/Indent.tsx +20 -27
  152. package/src/components/List.tsx +94 -0
  153. package/src/components/MemberDeclaration.tsx +9 -6
  154. package/src/components/MemberScope.tsx +4 -2
  155. package/src/components/Output.tsx +25 -13
  156. package/src/components/Scope.tsx +4 -2
  157. package/src/components/Show.tsx +11 -0
  158. package/src/components/SourceDirectory.tsx +5 -1
  159. package/src/components/SourceFile.tsx +12 -16
  160. package/src/components/StatementList.tsx +16 -0
  161. package/src/components/Switch.tsx +62 -0
  162. package/src/components/Wrap.tsx +29 -0
  163. package/src/components/index.tsx +8 -1
  164. package/src/components/stc/index.ts +18 -1
  165. package/src/context/index.ts +0 -1
  166. package/src/context.ts +2 -3
  167. package/src/index.browser.ts +2 -0
  168. package/src/index.ts +1 -0
  169. package/src/jsx-runtime.ts +245 -23
  170. package/src/render.ts +392 -198
  171. package/src/stc.ts +95 -0
  172. package/src/utils.ts +162 -95
  173. package/src/write-output.ts +3 -3
  174. package/temp/api.json +8407 -3301
  175. package/test/browser-build.test.ts +91 -0
  176. package/test/children.test.tsx +8 -10
  177. package/test/components/block.test.tsx +48 -0
  178. package/test/components/declaration.test.tsx +37 -0
  179. package/test/components/list.test.tsx +91 -0
  180. package/test/components/slot.test.tsx +31 -25
  181. package/test/components/source-file.test.tsx +11 -31
  182. package/test/components/wrap.test.tsx +42 -0
  183. package/test/control-flow/for.test.tsx +194 -0
  184. package/test/control-flow/match.test.tsx +49 -0
  185. package/test/control-flow/show.test.tsx +25 -0
  186. package/test/name-policy.test.tsx +5 -5
  187. package/test/reactivity/cleanup.test.tsx +91 -0
  188. package/test/reactivity/memo.test.tsx +17 -0
  189. package/test/reactivity/ref-rendering.test.tsx +3 -8
  190. package/test/reactivity/test.test.tsx +7 -6
  191. package/test/reactivity/untrack.test.ts +33 -0
  192. package/test/rendering/basic.test.tsx +25 -47
  193. package/test/rendering/code.test.tsx +3 -3
  194. package/test/rendering/formatting.test.tsx +487 -0
  195. package/test/rendering/indent.test.tsx +42 -529
  196. package/test/rendering/memoization.test.tsx +30 -0
  197. package/test/split-props.test.ts +87 -0
  198. package/test/stc.test.tsx +29 -8
  199. package/test/symbols.test.ts +87 -8
  200. package/test/utils.test.tsx +129 -20
  201. package/testing/extend-expect.ts +14 -4
  202. package/testing/render.ts +2 -4
  203. package/testing/vitest.d.ts +6 -1
  204. package/vitest.config.ts +1 -1
  205. package/dist/src/context/indent.d.ts +0 -5
  206. package/dist/src/context/indent.d.ts.map +0 -1
  207. package/dist/src/context/indent.js +0 -8
  208. package/dist/src/context/indent.js.map +0 -1
  209. package/dist/test/rendering/linebreaks.test.d.ts +0 -2
  210. package/dist/test/rendering/linebreaks.test.d.ts.map +0 -1
  211. package/src/context/indent.ts +0 -17
  212. package/test/rendering/linebreaks.test.tsx +0 -72
@@ -21,7 +21,9 @@ export function Scope(props: ScopeProps) {
21
21
  scope = binder.createScope({ kind, name: props.name! });
22
22
  }
23
23
 
24
- return <ScopeContext.Provider value={scope}>
24
+ return (
25
+ <ScopeContext.Provider value={scope}>
25
26
  {props.children}
26
- </ScopeContext.Provider>;
27
+ </ScopeContext.Provider>
28
+ );
27
29
  }
@@ -0,0 +1,11 @@
1
+ import { Children } from "@alloy-js/core/jsx-runtime";
2
+
3
+ export interface ShowProps {
4
+ children: Children;
5
+ fallback?: Children;
6
+ when: boolean | undefined | null;
7
+ }
8
+
9
+ export function Show(props: ShowProps) {
10
+ return () => (props.when ? props.children : props.fallback);
11
+ }
@@ -20,7 +20,11 @@ export function SourceDirectory(props: SourceDirectoryProps) {
20
20
  nodeContext.meta.directory = {
21
21
  path: sdPath,
22
22
  };
23
- return <SourceDirectoryContext.Provider value={context}>{props.children}</SourceDirectoryContext.Provider>;
23
+ return (
24
+ <SourceDirectoryContext.Provider value={context}>
25
+ {props.children}
26
+ </SourceDirectoryContext.Provider>
27
+ );
24
28
  }
25
29
 
26
30
  function createSourceDirectoryContext(
@@ -1,12 +1,12 @@
1
1
  import { join } from "pathe";
2
2
  import { useContext } from "../context.js";
3
- import { IndentContext } from "../context/indent.js";
4
3
  import { SourceDirectoryContext } from "../context/source-directory.js";
5
4
  import { SourceFileContext } from "../context/source-file.js";
6
5
  import { Children, ComponentDefinition, getContext } from "../jsx-runtime.js";
7
6
  import { Refkey } from "../refkey.js";
7
+ import { PrintTreeOptions } from "../render.js";
8
8
 
9
- export interface SourceFileProps {
9
+ export interface SourceFileProps extends PrintTreeOptions {
10
10
  /**
11
11
  * The path of this file relative to its parent directory
12
12
  */
@@ -24,12 +24,6 @@ export interface SourceFileProps {
24
24
  * contents.
25
25
  */
26
26
  reference?: ComponentDefinition<{ refkey: Refkey }>;
27
-
28
- /**
29
- * A string representing one indent level, used when reindenting contents of
30
- * this file.
31
- */
32
- indent?: string;
33
27
  }
34
28
 
35
29
  export function SourceFile(props: SourceFileProps) {
@@ -43,13 +37,15 @@ export function SourceFile(props: SourceFileProps) {
43
37
  const nodeContext = getContext()!;
44
38
  nodeContext.meta ??= {};
45
39
  nodeContext.meta.sourceFile = context;
40
+ nodeContext.meta.printOptions = {
41
+ printWidth: props.printWidth,
42
+ tabWidth: props.tabWidth,
43
+ useTabs: props.useTabs,
44
+ };
46
45
 
47
- return <SourceFileContext.Provider value={context}>
48
- { props.indent
49
- ? <IndentContext.Provider value={{ level: 0, indent: props.indent, indentString: "" }}>
50
- {props.children}
51
- </IndentContext.Provider>
52
- : props.children
53
- }
54
- </SourceFileContext.Provider>;
46
+ return (
47
+ <SourceFileContext.Provider value={context}>
48
+ {props.children}
49
+ </SourceFileContext.Provider>
50
+ );
55
51
  }
@@ -0,0 +1,16 @@
1
+ import { Children, List } from "@alloy-js/core";
2
+
3
+ export interface StatementListProps {
4
+ children: Children;
5
+ }
6
+
7
+ /**
8
+ * Join child elements with semicolons and hardlines.
9
+ */
10
+ export function StatementList(props: StatementListProps) {
11
+ return (
12
+ <List semicolon hardline enderPunctuation>
13
+ {props.children}
14
+ </List>
15
+ );
16
+ }
@@ -0,0 +1,62 @@
1
+ import { Children, memo, taggedComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { childrenArray, findKeyedChildren } from "../utils.js";
3
+
4
+ export interface SwitchProps {
5
+ children: Children;
6
+ }
7
+
8
+ /**
9
+ * Conditionally render blocks of content based on the `when` prop of nested
10
+ * {@link Match} components.
11
+ *
12
+ * @example
13
+ *
14
+ * ```tsx
15
+ * <Switch>
16
+ * <Match when={someCondition}>
17
+ * <div>Condition met!</div>
18
+ * </Match>
19
+ * <Match else>
20
+ * <div>Condition not met!</div>
21
+ * </Match>
22
+ * </Switch>
23
+ * ```
24
+ */
25
+ export function Switch(props: SwitchProps) {
26
+ const children = childrenArray(() => props.children);
27
+ const matches = findKeyedChildren(children, matchTag);
28
+
29
+ return memo(() => {
30
+ for (const match of matches) {
31
+ if (match.props.when || match.props.else) {
32
+ return match.props.children;
33
+ }
34
+ }
35
+
36
+ return undefined;
37
+ });
38
+ }
39
+
40
+ export interface MatchProps {
41
+ /**
42
+ * Condition under which the children of this element will be rendered.
43
+ */
44
+ when?: boolean;
45
+
46
+ /**
47
+ * If no `when` is matched, the children of this element will be rendered.
48
+ * The `else` match must be placed last.
49
+ */
50
+ else?: boolean;
51
+ children: Children;
52
+ }
53
+
54
+ export const matchTag = Symbol();
55
+
56
+ /**
57
+ * The Match component is used inside of a {@link Switch} component to
58
+ * define conditionally rendered blocks of content.
59
+ */
60
+ export const Match = taggedComponent(matchTag, (props: MatchProps) => {
61
+ return () => (props.when ? props.children : undefined);
62
+ });
@@ -0,0 +1,29 @@
1
+ import { Children, ComponentDefinition } from "@alloy-js/core/jsx-runtime";
2
+
3
+ export interface WrapProps<TProps> {
4
+ /**
5
+ * When true, the children will be wrapped with the provided component.
6
+ * Otherwise, the children will be rendered as is.
7
+ */
8
+ when: boolean;
9
+
10
+ /** Children to be wrapped. */
11
+ children: Children;
12
+
13
+ /** Component to be used for wrapping. */
14
+ with: ComponentDefinition<TProps>;
15
+
16
+ /** Props to pass to the wrapper component. */
17
+ props?: Omit<TProps, "children">;
18
+ }
19
+
20
+ /**
21
+ * Conditionally wrap the children of this component with the component given to
22
+ * `with` and passing `props` to it.
23
+ */
24
+ export function Wrap<TProps>(props: WrapProps<TProps>) {
25
+ const Wrapper = props.with as any;
26
+ return props.when ?
27
+ <Wrapper {...(props.props ?? {})}>{props.children}</Wrapper>
28
+ : props.children;
29
+ }
@@ -1,10 +1,17 @@
1
+ export * from "./Block.js";
1
2
  export * from "./Declaration.js";
2
- export * from "./Indent.js";
3
+ export * from "./For.js";
4
+ export * from "./Indent.jsx";
5
+ export * from "./List.jsx";
3
6
  export * from "./MemberDeclaration.jsx";
4
7
  export * from "./MemberName.jsx";
5
8
  export * from "./MemberScope.jsx";
6
9
  export * from "./Name.jsx";
7
10
  export * from "./Output.js";
8
11
  export * from "./Scope.js";
12
+ export * from "./Show.jsx";
9
13
  export * from "./SourceDirectory.js";
10
14
  export * from "./SourceFile.js";
15
+ export * from "./StatementList.jsx";
16
+ export * from "./Switch.jsx";
17
+ export * from "./Wrap.jsx";
@@ -1,9 +1,26 @@
1
- import { stc } from "../../utils.js";
1
+ import { stc, sti } from "../../stc.js";
2
2
  import * as base from "../index.js";
3
3
 
4
+ export const Block = stc(base.Block);
4
5
  export const Declaration = stc(base.Declaration);
6
+ export const For = stc(base.For);
5
7
  export const Indent = stc(base.Indent);
8
+ export const List = stc(base.List);
9
+ export const MemberDeclaration = stc(base.MemberDeclaration);
10
+ export const MemberName = stc(base.MemberName);
11
+ export const MemberScope = stc(base.MemberScope);
12
+ export const Name = stc(base.Name);
6
13
  export const Output = stc(base.Output);
7
14
  export const Scope = stc(base.Scope);
15
+ export const Show = stc(base.Show);
16
+ export const StatementList = stc(base.StatementList);
8
17
  export const SourceDirectory = stc(base.SourceDirectory);
9
18
  export const SourceFile = stc(base.SourceFile);
19
+ export const Switch = stc(base.Switch);
20
+ export const Wrap = stc(base.Wrap);
21
+
22
+ export const indent = sti("indent");
23
+ export const hbr = sti("hbr");
24
+ export const sbr = sti("sbr");
25
+ export const lbr = sti("lbr");
26
+ export const br = sti("br");
@@ -1,7 +1,6 @@
1
1
  export * from "./assignment.js";
2
2
  export * from "./binder.js";
3
3
  export * from "./declaration.js";
4
- export * from "./indent.js";
5
4
  export * from "./member-declaration.js";
6
5
  export * from "./member-scope.js";
7
6
  export * from "./name-policy.js";
package/src/context.ts CHANGED
@@ -4,7 +4,6 @@ import {
4
4
  ComponentDefinition,
5
5
  effect,
6
6
  getContext,
7
- untrack,
8
7
  } from "./jsx-runtime.js";
9
8
 
10
9
  export interface ComponentContext<T> {
@@ -49,8 +48,8 @@ export function createContext<T = unknown>(
49
48
  const rendered = shallowRef();
50
49
  effect(() => {
51
50
  context!.context![id] = props.value;
52
- rendered.value = untrack(() => props.children);
53
- });
51
+ rendered.value = () => props.children;
52
+ }, undefined);
54
53
 
55
54
  return rendered.value;
56
55
  },
@@ -0,0 +1,2 @@
1
+ export * from "./index.js"; // Re-export everything
2
+ export { writeOutput } from "./write-output.browser.js"; // Override writeOutput for browsers
package/src/index.ts CHANGED
@@ -17,6 +17,7 @@ export * from "./jsx-runtime.js";
17
17
  export * from "./name-policy.js";
18
18
  export * from "./refkey.js";
19
19
  export * from "./render.js";
20
+ export * from "./stc.js";
20
21
  export * from "./tap.js";
21
22
  export * from "./utils.js";
22
23
  export * from "./write-output.js";
@@ -1,14 +1,18 @@
1
1
  // Much of the implementations in this file are inspired by vuerx-js
2
2
  // See: https://github.com/ryansolid/vuerx-jsx.
3
3
  import {
4
+ isReactive,
4
5
  pauseTracking,
6
+ proxyRefs,
5
7
  Ref,
6
8
  resetTracking,
7
9
  shallowRef,
8
10
  stop,
11
+ toRefs,
9
12
  effect as vueEffect,
10
13
  } from "@vue/reactivity";
11
14
  import { Refkey } from "./refkey.js";
15
+ import { RenderedTextTree } from "./render.js";
12
16
 
13
17
  if ((globalThis as any).ALLOY) {
14
18
  throw new Error(
@@ -31,6 +35,11 @@ export interface Context {
31
35
  // store random info about the node
32
36
  meta?: Record<string, any>;
33
37
 
38
+ /**
39
+ * A cache of RenderTextTree nodes created within this context,
40
+ * indexed by the component or function which created them.
41
+ */
42
+ elementCache: ElementCache;
34
43
  /**
35
44
  * When this context was created by a component, this will
36
45
  * be the component that created it.
@@ -43,21 +52,35 @@ export function getContext() {
43
52
  return globalContext;
44
53
  }
45
54
 
46
- export function root<T>(
47
- fn: (d: Disposable) => T,
48
- componentOwner?: ComponentCreator<any>,
49
- ): T {
50
- globalContext = {
51
- componentOwner,
55
+ export function getElementCache() {
56
+ return getContext()!.elementCache;
57
+ }
58
+
59
+ export type ElementCacheKey =
60
+ | ComponentCreator
61
+ | (() => unknown)
62
+ | CustomContext;
63
+ export type ElementCache = Map<ElementCacheKey, RenderedTextTree>;
64
+
65
+ export interface RootOptions {
66
+ componentOwner?: ComponentCreator<any>;
67
+ }
68
+
69
+ export function root<T>(fn: (d: Disposable) => T, options?: RootOptions): T {
70
+ const context: Context = {
71
+ componentOwner: options?.componentOwner,
52
72
  disposables: [],
53
73
  owner: globalContext,
54
74
  context: {},
75
+ elementCache: new Map(),
55
76
  };
77
+
78
+ globalContext = context;
56
79
  let ret;
57
80
  try {
58
81
  ret = untrack(() =>
59
82
  fn(() => {
60
- for (const d of globalContext!.disposables) {
83
+ for (const d of context!.disposables) {
61
84
  d();
62
85
  }
63
86
  }),
@@ -83,28 +106,31 @@ export function memo<T>(fn: () => T, equal?: boolean): () => T {
83
106
  // eslint-disable-next-line @typescript-eslint/no-unused-expressions
84
107
  (!equal || prev !== res) && (o.value = res);
85
108
  return res;
86
- });
109
+ }, undefined as T);
87
110
  return () => o.value;
88
111
  }
89
112
 
90
113
  export function effect<T>(fn: (prev?: T) => T, current?: T) {
91
- const context = {
92
- src: "effect",
114
+ const context: Context = {
93
115
  context: {},
94
116
  disposables: [] as (() => void)[],
95
117
  owner: globalContext,
96
- } as any;
118
+ elementCache: new Map(),
119
+ };
97
120
 
98
121
  const cleanupFn = (final: boolean) => {
99
122
  const d = context.disposables;
100
123
  context.disposables = [];
101
124
  for (let k = 0, len = d.length; k < len; k++) d[k]();
125
+
102
126
  // eslint-disable-next-line @typescript-eslint/no-unused-expressions
103
127
  final && stop(c);
104
128
  };
105
129
 
130
+ onCleanup(() => cleanupFn(true));
106
131
  const c = vueEffect(() => {
107
132
  cleanupFn(false);
133
+
108
134
  const oldContext = globalContext;
109
135
  globalContext = context;
110
136
  try {
@@ -112,17 +138,64 @@ export function effect<T>(fn: (prev?: T) => T, current?: T) {
112
138
  } finally {
113
139
  globalContext = oldContext;
114
140
  }
115
- });
116
-
117
- cleanup(() => cleanupFn(true));
141
+ }, {});
118
142
  }
119
143
 
120
- export function cleanup(fn: Disposable) {
144
+ /**
145
+ * Register a cleanup function which is called when the current reactive scope
146
+ * is recalculated or disposed. This is useful to clean up any side effects
147
+ * created in the reactive scope.
148
+ *
149
+ * @remarks
150
+ *
151
+ * When onCleanup is called inside a component definition, the provided function
152
+ * is called when the component is removed from the tree. This can be useful to
153
+ * clean up any side effects created as a result of rendering the component. For
154
+ * example, if rendering a component creates a symbol, `onCleanup` can be used
155
+ * to remove the symbol when the component is removed from the tree.
156
+ *
157
+ * When onCleanup is called inside a memo or effect, the function is called when
158
+ * the effect is refreshed (e.g. when a memo or computed recalculates) or
159
+ * disposed (e.g. it is no longer needed because it is attached to a component
160
+ * which was removed).
161
+ */
162
+ export function onCleanup(fn: Disposable) {
121
163
  if (globalContext != null) {
122
164
  globalContext.disposables.push(fn);
123
165
  }
124
166
  }
125
167
 
168
+ /**
169
+ * Create a custom reactive context for the children returned by
170
+ * the provided context.
171
+ */
172
+ export interface CustomContext {
173
+ [CUSTOM_CONTEXT_SYM]: true;
174
+ useCustomContext: (useCb: CustomContextChildrenCallback) => void;
175
+ }
176
+
177
+ export type CustomContextChildrenCallback = (child: Children) => void;
178
+ const CUSTOM_CONTEXT_SYM = Symbol();
179
+
180
+ export function createCustomContext(
181
+ useCallback: (useChildren: CustomContextChildrenCallback) => void,
182
+ ): CustomContext {
183
+ return {
184
+ [CUSTOM_CONTEXT_SYM]: true,
185
+ useCustomContext(useCb: (children: Children) => void): void {
186
+ useCallback(useCb);
187
+ },
188
+ };
189
+ }
190
+
191
+ export function isCustomContext(child: Children): child is CustomContext {
192
+ return (
193
+ typeof child === "object" &&
194
+ child !== null &&
195
+ Object.hasOwn(child, CUSTOM_CONTEXT_SYM)
196
+ );
197
+ }
198
+
126
199
  export type Child =
127
200
  | string
128
201
  | boolean
@@ -131,23 +204,24 @@ export type Child =
131
204
  | null
132
205
  | void
133
206
  | (() => Children)
134
- | Child[]
135
207
  | Ref
136
- | Refkey;
208
+ | Refkey
209
+ | CustomContext
210
+ | IntrinsicElement;
137
211
 
138
- export type Children = Child | Child[];
212
+ export type Children = Child | Children[];
139
213
  export type Props = Record<string, any>;
140
214
 
141
215
  export interface ComponentDefinition<TProps = Props> {
142
- (props: TProps): Child | Children;
216
+ (props: TProps): Children;
143
217
  }
144
218
  export interface Component<TProps = Props> {
145
- (props: TProps): Child | Children;
219
+ (props: TProps): Children;
146
220
  tag?: symbol;
147
221
  }
148
222
  export interface ComponentCreator<TProps = Props> {
149
223
  component: Component<TProps>;
150
- (): Child | Children;
224
+ (): Children;
151
225
  props: Props;
152
226
  tag?: symbol;
153
227
  }
@@ -219,8 +293,31 @@ export function isComponentCreator(item: unknown): item is ComponentCreator {
219
293
  */
220
294
  // eslint-disable-next-line @typescript-eslint/no-namespace
221
295
  export namespace JSX {
222
- export interface IntrinsicElements {}
223
- export type ElementType = ComponentDefinition<any>;
296
+ export interface IntrinsicElements {
297
+ group: { shouldBreak?: boolean; id?: symbol; children: Children };
298
+ line: {};
299
+ br: {};
300
+ hardline: {};
301
+ hbr: {};
302
+ softline: {};
303
+ sbr: {};
304
+ literalline: {};
305
+ lbr: {};
306
+ indent: { children: Children };
307
+ indentIfBreak: { children: Children; groupId: symbol; negate?: boolean };
308
+ fill: { children: Children };
309
+ breakParent: {};
310
+ ifBreak: { children: Children; flatContents?: Children; groupId?: symbol };
311
+ lineSuffix: { children: Children };
312
+ lineSuffixBoundary: {};
313
+ dedent: { children: Children };
314
+ align:
315
+ | { children: Children; width: number }
316
+ | { children: Children; string: string };
317
+ markAsRoot: { children: Children };
318
+ dedentToRoot: { children: Children };
319
+ }
320
+ export type ElementType = string | ComponentDefinition<any>;
224
321
  export type Element = Children;
225
322
  export interface ElementChildrenAttribute {
226
323
  children: {};
@@ -258,6 +355,76 @@ export function taggedComponent<TProps = Props>(
258
355
  return component;
259
356
  }
260
357
 
358
+ export const intrinsicElementKey = Symbol();
359
+
360
+ export type IndentIntrinsicElement = IntrinsicElementBase<"indent">;
361
+ export type IndentIfBreakIntrinsicElement =
362
+ IntrinsicElementBase<"indentIfBreak">;
363
+ export type BrIntrinsicElement = IntrinsicElementBase<"br">;
364
+ export type LineIntrinsicElement = IntrinsicElementBase<"line">;
365
+ export type HbrIntrinsicElement = IntrinsicElementBase<"hbr">;
366
+ export type HardlineIntrinsicElement = IntrinsicElementBase<"hardline">;
367
+ export type SbrIntrinsicElement = IntrinsicElementBase<"sbr">;
368
+ export type SoftlineIntrinsicElement = IntrinsicElementBase<"softline">;
369
+ export type GroupIntrinsicElement = IntrinsicElementBase<"group">;
370
+ export type AlignIntrinsicElement = IntrinsicElementBase<"align">;
371
+ export type FillIntrinsicElement = IntrinsicElementBase<"fill">;
372
+ export type BreakParentIntrinsicElement = IntrinsicElementBase<"breakParent">;
373
+ export type LineSuffixIntrinsicElement = IntrinsicElementBase<"lineSuffix">;
374
+ export type LineSuffixBoundaryIntrinsicElement =
375
+ IntrinsicElementBase<"lineSuffixBoundary">;
376
+ export type DedentIntrinsicElement = IntrinsicElementBase<"dedent">;
377
+ export type DedentToRootIntrinsicElement = IntrinsicElementBase<"dedentToRoot">;
378
+ export type MarkAsRootIntrinsicElement = IntrinsicElementBase<"markAsRoot">;
379
+ export type LiterallineIntrinsicElement = IntrinsicElementBase<"literalline">;
380
+ export type LbrIntrinsicElement = IntrinsicElementBase<"lbr">;
381
+ export type IfBreakIntrinsicElement = IntrinsicElementBase<"ifBreak">;
382
+
383
+ export type IntrinsicElement =
384
+ | IndentIntrinsicElement
385
+ | IndentIfBreakIntrinsicElement
386
+ | BrIntrinsicElement
387
+ | LineIntrinsicElement
388
+ | HbrIntrinsicElement
389
+ | HardlineIntrinsicElement
390
+ | SbrIntrinsicElement
391
+ | SoftlineIntrinsicElement
392
+ | GroupIntrinsicElement
393
+ | AlignIntrinsicElement
394
+ | FillIntrinsicElement
395
+ | BreakParentIntrinsicElement
396
+ | LineSuffixIntrinsicElement
397
+ | LineSuffixBoundaryIntrinsicElement
398
+ | DedentIntrinsicElement
399
+ | LiterallineIntrinsicElement
400
+ | LbrIntrinsicElement
401
+ | DedentToRootIntrinsicElement
402
+ | MarkAsRootIntrinsicElement
403
+ | IfBreakIntrinsicElement;
404
+
405
+ export interface IntrinsicElementBase<
406
+ TKey extends keyof JSX.IntrinsicElements = keyof JSX.IntrinsicElements,
407
+ > {
408
+ [intrinsicElementKey]: true;
409
+ name: TKey;
410
+ props: JSX.IntrinsicElements[TKey];
411
+ }
412
+ export function createIntrinsic<TKey extends keyof JSX.IntrinsicElements>(
413
+ name: TKey,
414
+ props: JSX.IntrinsicElements[TKey],
415
+ ): IntrinsicElementBase<TKey> {
416
+ return {
417
+ [intrinsicElementKey]: true,
418
+ name,
419
+ props,
420
+ };
421
+ }
422
+
423
+ export function isIntrinsicElement(type: unknown): type is IntrinsicElement {
424
+ return (
425
+ typeof type === "object" && type !== null && intrinsicElementKey in type
426
+ );
427
+ }
261
428
  export function mergeProps<T, U>(source: T, source1: U): T & U;
262
429
  export function mergeProps<T, U, V>(
263
430
  source: T,
@@ -296,6 +463,61 @@ export function mergeProps(...sources: any): any {
296
463
  return target;
297
464
  }
298
465
 
466
+ export type SplitProps<T, K extends (readonly (keyof T)[])[]> = [
467
+ ...{
468
+ [P in keyof K]: P extends `${number}` ?
469
+ Pick<T, Extract<K[P], readonly (keyof T)[]>[number]>
470
+ : never;
471
+ },
472
+ { [P in keyof T as Exclude<P, K[number][number]>]: T[P] },
473
+ ];
474
+
475
+ export function splitProps<
476
+ T extends Record<any, any>,
477
+ K extends [readonly (keyof T)[], ...(readonly (keyof T)[])[]],
478
+ >(props: T, ...keys: K): SplitProps<T, K> {
479
+ if (isReactive(props)) {
480
+ const refs = untrack(() => toRefs(props));
481
+ const remainingKeys = new Set(Object.keys(refs));
482
+
483
+ const result: any = keys.map((keySet) => {
484
+ const resultSet: any = {};
485
+ for (const key of keySet) {
486
+ resultSet[key] = refs[key];
487
+ remainingKeys.delete(key as string);
488
+ }
489
+ return proxyRefs(resultSet);
490
+ });
491
+
492
+ const remaining: any = {};
493
+ for (const key of remainingKeys) {
494
+ remaining[key] = refs[key];
495
+ }
496
+
497
+ return [...result, proxyRefs(remaining)] as any;
498
+ }
499
+
500
+ const descriptors = Object.getOwnPropertyDescriptors(props);
501
+ const remainingKeys = new Set(Object.keys(descriptors));
502
+ const result: any = keys.map((keySet) => {
503
+ const resultSet: any = {};
504
+ for (const key of keySet) {
505
+ if (key in descriptors) {
506
+ Object.defineProperty(resultSet, key, descriptors[key]);
507
+ remainingKeys.delete(key as string);
508
+ }
509
+ }
510
+ return resultSet;
511
+ });
512
+
513
+ const remaining: any = {};
514
+ for (const key of remainingKeys) {
515
+ Object.defineProperty(remaining, key, descriptors[key]);
516
+ }
517
+
518
+ return [...result, remaining] as any;
519
+ }
520
+
299
521
  function shouldDebug() {
300
522
  return typeof process !== "undefined" && !!process.env?.ALLOY_DEBUG;
301
523
  }