@overmvp/kumi 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +14 -2
- package/animation/color-transition.ts +196 -0
- package/animation/cycle.ts +151 -0
- package/animation/ease.ts +29 -0
- package/animation/index.ts +13 -0
- package/animation/package.json +12 -0
- package/animation/presets.ts +376 -0
- package/animation/spec.ts +197 -0
- package/builder/Cargo.lock +192 -0
- package/builder/Cargo.toml +7 -0
- package/builder/css-v1.mjs +334 -0
- package/builder/css-v1.test.mjs +321 -0
- package/builder/generated-output.mjs +61 -0
- package/builder/generated-output.test.mjs +132 -0
- package/builder/jsx-form.mjs +93 -0
- package/builder/jsx-form.test.mjs +48 -0
- package/builder/jsx-lists.mjs +209 -0
- package/builder/link-addon-package.mjs +86 -0
- package/builder/lua-assets.mjs +74 -0
- package/builder/lua-builtin-values.mjs +57 -0
- package/builder/lua-builtin-values.test.mjs +155 -0
- package/builder/lua-loop-closures.mjs +183 -0
- package/builder/lua-loop-closures.test.mjs +89 -0
- package/builder/lua-truthiness.mjs +61 -0
- package/builder/lua-truthiness.test.mjs +113 -0
- package/builder/module-declarations.mjs +594 -0
- package/builder/module-declarations.test.mjs +444 -0
- package/builder/native-kernels.mjs +60 -0
- package/builder/native-lua.d.ts +57 -0
- package/builder/native-routes.mjs +51 -0
- package/builder/no-static-ref-read.mjs +93 -0
- package/builder/no-static-ref-read.test.mjs +181 -0
- package/builder/secure-snippet.mjs +317 -0
- package/builder/secure-snippet.test.mjs +101 -0
- package/builder/shared-lua-runtime.mjs +150 -0
- package/builder/src/addon-link.lua +29 -0
- package/builder/src/arrays.lua +260 -0
- package/builder/src/assets.rs +177 -0
- package/builder/src/classes.lua +75 -0
- package/builder/src/collections.lua +190 -0
- package/builder/src/compiler.rs +2164 -0
- package/builder/src/config.rs +42 -0
- package/builder/src/entries.rs +166 -0
- package/builder/src/errors.lua +15 -0
- package/builder/src/lua_data.rs +231 -0
- package/builder/src/main.rs +367 -0
- package/builder/src/native_ir.rs +65 -0
- package/builder/src/native_signal.rs +47 -0
- package/builder/src/numbers.lua +62 -0
- package/builder/src/project-front.mjs +3331 -0
- package/builder/src/records.lua +75 -0
- package/builder/src/routes.rs +127 -0
- package/builder/src/signal-kernel.fingerprint +1 -0
- package/builder/src/strings.lua +103 -0
- package/builder/src/ts-lower.mjs +68 -0
- package/builder/src/wow_client.rs +253 -0
- package/builder/src/wow_types.rs +564 -0
- package/builder/style-states.mjs +100 -0
- package/builder/style-states.test.mjs +103 -0
- package/builder/vite-assets.mjs +53 -0
- package/builder/vite-project.mjs +10 -0
- package/config.ts +17 -0
- package/form/index.ts +522 -0
- package/form/package.json +13 -0
- package/motion/index.ts +2 -0
- package/motion/package.json +10 -0
- package/package.json +22 -4
- package/router/index.ts +43 -0
- package/router/package.json +12 -0
- package/router/routes.ts +64 -0
- package/scripts/kumi-cli.mjs +70 -0
- package/scripts/link-addons.mjs +51 -0
- package/src/bindings.ts +35 -0
- package/src/context.ts +15 -0
- package/src/css-tokens.ts +234 -0
- package/src/css-v1.ts +58 -0
- package/src/cvars.ts +91 -0
- package/src/element-node.ts +32 -0
- package/src/elements.ts +479 -0
- package/src/feature-scope.ts +82 -0
- package/src/flex-layout.ts +807 -0
- package/src/frame-scheduler.ts +334 -0
- package/src/grid-tracks.ts +165 -0
- package/src/image-imports.d.ts +24 -0
- package/src/images.ts +79 -0
- package/src/index.ts +28 -0
- package/src/jsx-dev-runtime.ts +2 -0
- package/src/jsx-runtime.ts +22 -0
- package/src/jsx.d.ts +12 -0
- package/src/keyed-list.ts +363 -0
- package/src/media-query.ts +144 -0
- package/src/module.ts +277 -0
- package/src/node-selectors.ts +114 -0
- package/src/owned-collections.ts +44 -0
- package/src/providers.ts +235 -0
- package/src/reactive-scope.ts +34 -0
- package/src/reactivity.ts +254 -0
- package/src/result.ts +58 -0
- package/src/runtime.ts +52 -0
- package/src/scheduler.ts +185 -0
- package/src/signal-kernel.ts +30 -0
- package/src/style-classes.ts +51 -0
- package/src/style-states.ts +134 -0
- package/src/style-variables.ts +53 -0
- package/src/text-children.ts +42 -0
- package/src/theme.ts +50 -0
- package/src/transition.ts +18 -0
- package/state/index.ts +3 -0
- package/state/package.json +12 -0
- package/state/store-bindings.ts +25 -0
- package/state/store.ts +82 -0
- package/styles/_layers.scss +14 -0
- package/templates/addon/README.md +14 -0
- package/templates/addon/__ADDON_NAME__.toc +4 -0
- package/templates/addon/eslint.config.mjs +12 -0
- package/templates/addon/gitignore +8 -0
- package/templates/addon/package.json +21 -0
- package/templates/addon/scripts/build.mjs +25 -0
- package/templates/addon/src/app/main.tsx +13 -0
- package/templates/addon/tsconfig.json +24 -0
- package/translate/index.ts +14 -0
- package/translate/package.json +9 -0
- package/utils/colors.ts +76 -0
- package/utils/format.ts +37 -0
- package/utils/index.ts +4 -0
- package/utils/number.ts +17 -0
- package/utils/package.json +7 -0
- package/utils/text.ts +6 -0
- package/validate/index.ts +119 -0
- package/validate/package.json +9 -0
- package/wow/absolute-position.ts +142 -0
- package/wow/animation.ts +428 -0
- package/wow/aura-button.ts +76 -0
- package/wow/aura-container.ts +591 -0
- package/wow/aura-countdown.ts +201 -0
- package/wow/aura-slot.ts +153 -0
- package/wow/bar-shade.ts +93 -0
- package/wow/borrowed-slot.ts +216 -0
- package/wow/button-drag.ts +48 -0
- package/wow/cast-clock.ts +153 -0
- package/wow/cast-text.ts +83 -0
- package/wow/class-colors.ts +126 -0
- package/wow/clock.ts +7 -0
- package/wow/color-picker.ts +128 -0
- package/wow/commands.ts +17 -0
- package/wow/contained-frame.ts +128 -0
- package/wow/contract.ts +42 -0
- package/wow/css-box.ts +110 -0
- package/wow/css-color.ts +12 -0
- package/wow/css-style.ts +109 -0
- package/wow/css-surfaces.ts +398 -0
- package/wow/css-text.ts +399 -0
- package/wow/cvar-schema.ts +32 -0
- package/wow/cvars.ts +98 -0
- package/wow/dropdown-art.ts +39 -0
- package/wow/element-click.ts +20 -0
- package/wow/element-event-coordinator.ts +219 -0
- package/wow/element-events.ts +317 -0
- package/wow/event-dispatcher.ts +151 -0
- package/wow/events.ts +50 -0
- package/wow/font-cache.ts +92 -0
- package/wow/frame-access.ts +31 -0
- package/wow/frame-layer.ts +5 -0
- package/wow/frames.ts +236 -0
- package/wow/heal-prediction.ts +305 -0
- package/wow/image-layers.ts +55 -0
- package/wow/index.ts +97 -0
- package/wow/layout-view.ts +651 -0
- package/wow/lua-string.ts +18 -0
- package/wow/lua-table.ts +18 -0
- package/wow/macro-book.ts +84 -0
- package/wow/map-position.ts +27 -0
- package/wow/media-query.ts +65 -0
- package/wow/module-events.ts +11 -0
- package/wow/native-hooks.ts +168 -0
- package/wow/native-placement.ts +88 -0
- package/wow/native.ts +135 -0
- package/wow/number-format.ts +8 -0
- package/wow/number-input.ts +10 -0
- package/wow/protect.ts +24 -0
- package/wow/protected-layout.ts +133 -0
- package/wow/range-art.ts +74 -0
- package/wow/reparent-frame.ts +99 -0
- package/wow/rounded-texture.ts +156 -0
- package/wow/saved-variables.ts +60 -0
- package/wow/scroll-view.ts +64 -0
- package/wow/secret-ref.ts +14 -0
- package/wow/secure-bindings.ts +89 -0
- package/wow/secure-clicks.ts +432 -0
- package/wow/secure-snippet.ts +235 -0
- package/wow/secure-state.ts +130 -0
- package/wow/secure-target-visibility.ts +89 -0
- package/wow/secure-transaction.ts +331 -0
- package/wow/special-window.ts +269 -0
- package/wow/start-addon.ts +99 -0
- package/wow/state-paint.ts +256 -0
- package/wow/transition.ts +86 -0
- package/wow/unit-button.ts +39 -0
- package/wow/unit-cast.ts +92 -0
- package/wow/unit-health-text.ts +47 -0
- package/wow/view-nodes.ts +304 -0
- package/wow/view.ts +1746 -0
- package/wow/visible-frame-job.ts +34 -0
- package/wow/width-reserve.ts +108 -0
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import type { Element, ElementKey, UIElement } from "./elements";
|
|
2
|
+
import type { OwnedArray, OwnedMap, OwnedReadonlyArray, OwnedRecord, OwnedSet } from "./owned-collections";
|
|
3
|
+
import { withReactiveScope } from "./reactive-scope";
|
|
4
|
+
import { ref, type ReadonlyRef, type Ref } from "./reactivity";
|
|
5
|
+
import { fail, ok, type Failure, type Result } from "./result";
|
|
6
|
+
import type { RuntimeScope } from "./runtime";
|
|
7
|
+
import { TextBinding } from "./text-children";
|
|
8
|
+
|
|
9
|
+
interface ListHost<View> {
|
|
10
|
+
mount(this: void, tree: UIElement, scope: RuntimeScope): Result<View>;
|
|
11
|
+
order(this: void, rows: readonly View[]): void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type MountKeyedList = <View>(this: void, scope: RuntimeScope, host: ListHost<View>) => Failure | undefined;
|
|
15
|
+
|
|
16
|
+
type Props = Record<string, unknown>;
|
|
17
|
+
type ReadNode = (this: void) => UIElement;
|
|
18
|
+
|
|
19
|
+
interface Row<View> {
|
|
20
|
+
tree: Ref<UIElement>;
|
|
21
|
+
scope: RuntimeScope;
|
|
22
|
+
view: View;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const layoutProps = new Set(["anchor", "width", "height", "opacity", "shown"]);
|
|
26
|
+
const bindingProps: Partial<Record<UIElement["kind"], ReadonlySet<string>>> = {
|
|
27
|
+
frame: new Set(["nativeScale", "nativeImageLayers"]),
|
|
28
|
+
image: new Set(["nativeDesaturated", "verticalGradient"]),
|
|
29
|
+
text: new Set(["value", "color", "fontSize"]),
|
|
30
|
+
button: new Set(["value", "disabled", "checked"]),
|
|
31
|
+
editbox: new Set(["value"]),
|
|
32
|
+
checkbox: new Set(["checked", "disabled"]),
|
|
33
|
+
range: new Set(["value", "disabled"]),
|
|
34
|
+
listbox: new Set(["value", "disabled"]),
|
|
35
|
+
};
|
|
36
|
+
// `ref` is not proxied: a ref owns the handle it received, so a row with a different ref is mounted again.
|
|
37
|
+
const callbackProps = new Set(["onClick", "onChange", "onFocus", "onBlur", "onEnter", "onEscape", "onPointerEnter", "onPointerLeave",
|
|
38
|
+
"onDrag", "onDragEnd", "onDragCancel", "onShow", "onHide", "onClickOutside", "onDismiss"]);
|
|
39
|
+
const descriptorProps = new Set(["style", "src", "items", "data"]);
|
|
40
|
+
|
|
41
|
+
function propertyNames(props: Props) {
|
|
42
|
+
return Object.keys(props).filter(name => name !== "children" && name !== "key" && props[name] !== undefined) as OwnedArray<string>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function properties(node: UIElement): Props {
|
|
46
|
+
return node.props as unknown as Props;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function canBind(kind: UIElement["kind"], name: string) {
|
|
50
|
+
if (layoutProps.has(name)) return true;
|
|
51
|
+
const local = bindingProps[kind] as OwnedSet<string> | undefined;
|
|
52
|
+
return local !== undefined && local.has(name);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function equivalent(left: unknown, right: unknown, depth: number): boolean {
|
|
56
|
+
if (left === right) return true;
|
|
57
|
+
if (depth === 0 || typeof left !== "object" || typeof right !== "object" || left === null || right === null) return false;
|
|
58
|
+
// Compare plain layout, asset and option descriptors by value.
|
|
59
|
+
const props = left as Props;
|
|
60
|
+
const other = right as Props;
|
|
61
|
+
const keys = Object.keys(props);
|
|
62
|
+
return keys.length === Object.keys(other).length && keys.every(key => equivalent(props[key], other[key], depth - 1));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function compatible(current: UIElement, next: UIElement): boolean {
|
|
66
|
+
if (current === next) return true;
|
|
67
|
+
const currentChildren = current.children as OwnedArray<UIElement>;
|
|
68
|
+
const nextChildren = next.children as OwnedArray<UIElement>;
|
|
69
|
+
if (current.kind !== next.kind || current.key !== next.key || currentChildren.length !== nextChildren.length) return false;
|
|
70
|
+
const previous = properties(current);
|
|
71
|
+
const changed = properties(next);
|
|
72
|
+
const names = propertyNames(previous);
|
|
73
|
+
if (names.length !== propertyNames(changed).length) return false;
|
|
74
|
+
for (const name of names) {
|
|
75
|
+
if (changed[name] === undefined) return false;
|
|
76
|
+
if (canBind(current.kind, name) || callbackProps.has(name)) continue;
|
|
77
|
+
if (name === "cssVars") {
|
|
78
|
+
const before = propertyNames(previous[name] as Props);
|
|
79
|
+
const after = changed[name] as Props;
|
|
80
|
+
if (before.length !== propertyNames(after).length || before.some(variable => after[variable] === undefined)) return false;
|
|
81
|
+
} else if (!equivalent(previous[name], changed[name], descriptorProps.has(name) ? 2 : 0)) return false;
|
|
82
|
+
}
|
|
83
|
+
return currentChildren.every((child, index) => compatible(child, nextChildren[index]));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function validateOverflow(current: UIElement, next: UIElement): Failure | undefined {
|
|
87
|
+
if (current === next || current.kind !== next.kind || current.key !== next.key) return;
|
|
88
|
+
if (overflowOf(current) !== overflowOf(next)) return fail("CSS overflow is fixed at mount");
|
|
89
|
+
const currentChildren = current.children as OwnedArray<UIElement>;
|
|
90
|
+
const nextChildren = next.children as OwnedArray<UIElement>;
|
|
91
|
+
for (let index = 0; index < Math.min(currentChildren.length, nextChildren.length); index++) {
|
|
92
|
+
const invalid = validateOverflow(currentChildren[index], nextChildren[index]);
|
|
93
|
+
if (invalid !== undefined) return invalid;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function overflowOf(element: UIElement) {
|
|
98
|
+
const style = "style" in element.props ? element.props.style : undefined;
|
|
99
|
+
return style && "overflow" in style ? style.overflow : undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function boundProperty(value: unknown): Result<unknown> {
|
|
103
|
+
if (value instanceof TextBinding) return value.value;
|
|
104
|
+
if (typeof value === "object" && value !== null && "value" in value) {
|
|
105
|
+
const current = (value as ReadonlyRef<unknown>).value;
|
|
106
|
+
if (typeof current === "object" && current !== null && "ok" in current) return current as Result<unknown>;
|
|
107
|
+
return ok(current);
|
|
108
|
+
}
|
|
109
|
+
return ok(value);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function bindRow(read: ReadNode, initial: UIElement, scope: RuntimeScope): UIElement {
|
|
113
|
+
const props = { ...properties(initial) as OwnedRecord<Props> };
|
|
114
|
+
for (const name of propertyNames(props)) {
|
|
115
|
+
if (canBind(initial.kind, name)) {
|
|
116
|
+
props[name] = scope.computedResult(() => boundProperty(properties(read())[name]));
|
|
117
|
+
} else if (name === "cssVars") {
|
|
118
|
+
const variables: Props = {};
|
|
119
|
+
for (const variable of propertyNames(props[name] as Props)) {
|
|
120
|
+
variables[variable] = scope.computedResult(() => boundProperty((properties(read())[name] as Props)[variable]));
|
|
121
|
+
}
|
|
122
|
+
props[name] = variables;
|
|
123
|
+
} else if (callbackProps.has(name) && typeof props[name] === "function") {
|
|
124
|
+
props[name] = (...args: unknown[]) => {
|
|
125
|
+
const callback = properties(read())[name] as (this: void, ...args: unknown[]) => unknown;
|
|
126
|
+
return callback(...args);
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const children = (initial.children as OwnedArray<UIElement>).map((child, index) => bindRow(
|
|
131
|
+
() => (read().children as OwnedArray<UIElement>)[index], child, scope));
|
|
132
|
+
return { ...initial as OwnedRecord<UIElement>, props, children } as UIElement;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function validateKey(key: ElementKey | undefined, seen: { has(this: object, key: ElementKey): boolean }): Failure | undefined {
|
|
136
|
+
if (typeof key !== "string" && (typeof key !== "number" || !Number.isFinite(key))) {
|
|
137
|
+
return fail("Each reactive map row needs a string or finite numeric key");
|
|
138
|
+
}
|
|
139
|
+
if (seen.has(key)) return fail(`Duplicate reactive map key '${key}'`);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function mountRow<View>(node: UIElement, scope: RuntimeScope, host: ListHost<View>): Result<Row<View>> {
|
|
143
|
+
const child = scope.fork();
|
|
144
|
+
const tree = ref(node);
|
|
145
|
+
const attempt = scope.protect(() => host.mount(bindRow(() => tree.value, node, child), child));
|
|
146
|
+
const result = attempt.ok ? attempt.value : attempt;
|
|
147
|
+
if (result.ok) return ok({ tree, scope: child, view: result.value });
|
|
148
|
+
child.dispose();
|
|
149
|
+
return result;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function mountList<View>(read: (this: void) => readonly UIElement[], scope: RuntimeScope, host: ListHost<View>) {
|
|
153
|
+
let rows = new Map<ElementKey, Row<View>>() as OwnedMap<ElementKey, Row<View>>;
|
|
154
|
+
|
|
155
|
+
function reconcile(input: readonly UIElement[]): Failure | undefined {
|
|
156
|
+
const nodes = input as OwnedReadonlyArray<UIElement>;
|
|
157
|
+
const seen = new Set<ElementKey>();
|
|
158
|
+
for (const node of nodes) {
|
|
159
|
+
const { key } = node;
|
|
160
|
+
const invalid = validateKey(key, seen);
|
|
161
|
+
if (invalid) return invalid;
|
|
162
|
+
seen.add(key!);
|
|
163
|
+
}
|
|
164
|
+
for (const node of nodes) {
|
|
165
|
+
const previous = rows.get(node.key!);
|
|
166
|
+
if (previous === undefined) continue;
|
|
167
|
+
const invalid = validateOverflow(previous.tree.value, node);
|
|
168
|
+
if (invalid !== undefined) return invalid;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const next = new Map<ElementKey, Row<View>>();
|
|
172
|
+
const created: RuntimeScope[] = [];
|
|
173
|
+
const order: View[] = [];
|
|
174
|
+
for (const node of nodes) {
|
|
175
|
+
const key = node.key!;
|
|
176
|
+
let row = rows.get(key);
|
|
177
|
+
if (!row || !compatible(row.tree.value, node)) {
|
|
178
|
+
const mounted = mountRow(node, scope, host);
|
|
179
|
+
if (!mounted.ok) {
|
|
180
|
+
for (const staged of created) staged.dispose();
|
|
181
|
+
return mounted;
|
|
182
|
+
}
|
|
183
|
+
row = mounted.value;
|
|
184
|
+
created.push(row.scope);
|
|
185
|
+
}
|
|
186
|
+
next.set(key, row);
|
|
187
|
+
order.push(row.view!);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
let failure: Failure | undefined;
|
|
191
|
+
for (const [key, row] of rows) {
|
|
192
|
+
if (next.get(key) === row) continue;
|
|
193
|
+
const disposed = row.scope.dispose();
|
|
194
|
+
if (!disposed.ok) failure ??= disposed;
|
|
195
|
+
}
|
|
196
|
+
for (const node of nodes) next.get(node.key!)!.tree.value = node;
|
|
197
|
+
rows = next as OwnedMap<ElementKey, Row<View>>;
|
|
198
|
+
host.order(order);
|
|
199
|
+
return failure;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const subscription = scope.watch(read, reconcile, true);
|
|
203
|
+
scope.onDispose(() => rows.clear());
|
|
204
|
+
return subscription.ok ? undefined : subscription;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** @internal Reconciles a reactive list of already-built descriptors. */
|
|
208
|
+
export function jsxList(this: void, read: (this: void) => readonly UIElement[]): Element<"keyedList"> {
|
|
209
|
+
return { kind: "keyedList", props: { mount: (scope, host) => mountList(read, scope, host) }, children: [] };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
interface MapInput<Item> {
|
|
213
|
+
item: Item;
|
|
214
|
+
index: number;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
interface MapRow<Item, View> {
|
|
218
|
+
committedInput: MapInput<Item>;
|
|
219
|
+
input: Ref<MapInput<Item>>;
|
|
220
|
+
scope: RuntimeScope;
|
|
221
|
+
view(this: void): View;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function mountMapRow<Item, Key extends ElementKey, View>(key: Key, initial: MapInput<Item>,
|
|
225
|
+
render: (this: void, item: Item, index: number, key: Key) => UIElement,
|
|
226
|
+
parent: RuntimeScope, host: ListHost<View>, reorder: (this: void) => void): Result<MapRow<Item, View>> {
|
|
227
|
+
const scope = parent.fork();
|
|
228
|
+
const input = ref(initial);
|
|
229
|
+
let mounted: Row<View> | undefined;
|
|
230
|
+
let renderScope: RuntimeScope | undefined;
|
|
231
|
+
|
|
232
|
+
const subscription = scope.watch(() => {
|
|
233
|
+
const { item, index } = input.value;
|
|
234
|
+
const candidate = scope.fork();
|
|
235
|
+
const result = withReactiveScope(candidate, () => {
|
|
236
|
+
const node = render(item, index, key);
|
|
237
|
+
return node.key === key ? node : { ...node as OwnedRecord<UIElement>, key };
|
|
238
|
+
});
|
|
239
|
+
return { candidate, result };
|
|
240
|
+
}, ({ candidate, result }) => {
|
|
241
|
+
if (!result.ok) {
|
|
242
|
+
candidate.dispose();
|
|
243
|
+
return result;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const previous = mounted;
|
|
247
|
+
const node = result.value;
|
|
248
|
+
const invalid = previous === undefined ? undefined : validateOverflow(previous.tree.value, node);
|
|
249
|
+
if (invalid !== undefined) {
|
|
250
|
+
candidate.dispose();
|
|
251
|
+
return invalid;
|
|
252
|
+
}
|
|
253
|
+
let failure: Failure | undefined;
|
|
254
|
+
if (previous && compatible(previous.tree.value, node)) {
|
|
255
|
+
previous.tree.value = node;
|
|
256
|
+
} else {
|
|
257
|
+
const replacement = mountRow(node, scope, host);
|
|
258
|
+
if (!replacement.ok) {
|
|
259
|
+
candidate.dispose();
|
|
260
|
+
return replacement;
|
|
261
|
+
}
|
|
262
|
+
mounted = replacement.value;
|
|
263
|
+
if (previous) {
|
|
264
|
+
const retired = previous.scope.dispose();
|
|
265
|
+
if (!retired.ok) failure = retired;
|
|
266
|
+
reorder();
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const retired = renderScope?.dispose();
|
|
271
|
+
renderScope = candidate;
|
|
272
|
+
if (retired && !retired.ok) failure ??= retired;
|
|
273
|
+
return failure;
|
|
274
|
+
}, true);
|
|
275
|
+
|
|
276
|
+
if (!subscription.ok) {
|
|
277
|
+
scope.dispose();
|
|
278
|
+
return subscription;
|
|
279
|
+
}
|
|
280
|
+
return ok({ committedInput: initial, input, scope, view: () => mounted!.view });
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function mountMap<Item, Key extends ElementKey, View>(read: (this: void) => readonly Item[],
|
|
284
|
+
keyBy: (this: void, item: Item, index: number) => Key,
|
|
285
|
+
render: (this: void, item: Item, index: number, key: Key) => UIElement,
|
|
286
|
+
usesIndex: boolean, scope: RuntimeScope, host: ListHost<View>): Failure | undefined {
|
|
287
|
+
let rows = new Map<Key, MapRow<Item, View>>() as OwnedMap<Key, MapRow<Item, View>>;
|
|
288
|
+
let orderPending = false;
|
|
289
|
+
|
|
290
|
+
function orderRows() {
|
|
291
|
+
orderPending = false;
|
|
292
|
+
host.order([...rows.values()].map(row => row.view()));
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function reorder() {
|
|
296
|
+
if (orderPending) return;
|
|
297
|
+
orderPending = true;
|
|
298
|
+
scope.nextTick(() => {
|
|
299
|
+
if (orderPending) orderRows();
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function readInputs(): Result<OwnedMap<Key, MapInput<Item>>> {
|
|
304
|
+
const inputs = new Map<Key, MapInput<Item>>() as OwnedMap<Key, MapInput<Item>>;
|
|
305
|
+
const items = read() as OwnedReadonlyArray<Item>;
|
|
306
|
+
let invalid: Failure | undefined;
|
|
307
|
+
items.forEach((item, index) => {
|
|
308
|
+
if (invalid) return;
|
|
309
|
+
const key = keyBy(item, index);
|
|
310
|
+
invalid = validateKey(key, inputs);
|
|
311
|
+
if (invalid) return;
|
|
312
|
+
// Keep structural dependencies limited to the source and key selector.
|
|
313
|
+
const previous = rows.get(key)?.committedInput;
|
|
314
|
+
const unchanged = previous && previous.item === item && (!usesIndex || previous.index === index);
|
|
315
|
+
inputs.set(key, unchanged ? previous : { item, index });
|
|
316
|
+
});
|
|
317
|
+
return invalid ?? ok(inputs);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function reconcile(inputs: OwnedMap<Key, MapInput<Item>>): Failure | undefined {
|
|
321
|
+
const next = new Map<Key, MapRow<Item, View>>();
|
|
322
|
+
const created: RuntimeScope[] = [];
|
|
323
|
+
for (const [key, input] of inputs) {
|
|
324
|
+
let row = rows.get(key);
|
|
325
|
+
if (!row) {
|
|
326
|
+
const mounted = mountMapRow(key, input, render, scope, host, reorder);
|
|
327
|
+
if (!mounted.ok) {
|
|
328
|
+
for (const staged of created) staged.dispose();
|
|
329
|
+
return mounted;
|
|
330
|
+
}
|
|
331
|
+
row = mounted.value;
|
|
332
|
+
created.push(row.scope);
|
|
333
|
+
}
|
|
334
|
+
next.set(key, row);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
let failure: Failure | undefined;
|
|
338
|
+
for (const [key, row] of rows) {
|
|
339
|
+
if (next.has(key)) continue;
|
|
340
|
+
const disposed = row.scope.dispose();
|
|
341
|
+
if (!disposed.ok) failure ??= disposed;
|
|
342
|
+
}
|
|
343
|
+
for (const [key, input] of inputs) {
|
|
344
|
+
const row = next.get(key)!;
|
|
345
|
+
row.committedInput = input;
|
|
346
|
+
row.input.value = input;
|
|
347
|
+
}
|
|
348
|
+
rows = next as OwnedMap<Key, MapRow<Item, View>>;
|
|
349
|
+
orderRows();
|
|
350
|
+
return failure;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const subscription = scope.watch(readInputs, inputs => inputs.ok ? reconcile(inputs.value) : inputs, true);
|
|
354
|
+
scope.onDispose(() => rows.clear());
|
|
355
|
+
return subscription.ok ? undefined : subscription;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** @internal The TSX transform calls this for keyed maps over reactive sources. */
|
|
359
|
+
export function jsxMap<Item, Key extends ElementKey>(this: void, read: (this: void) => readonly Item[],
|
|
360
|
+
keyBy: (this: void, item: Item, index: number) => Key,
|
|
361
|
+
render: (this: void, item: Item, index: number, key: Key) => UIElement, usesIndex = false): Element<"keyedList"> {
|
|
362
|
+
return { kind: "keyedList", props: { mount: (scope, host) => mountMap(read, keyBy, render, usesIndex, scope, host) }, children: [] };
|
|
363
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { ref, type ReadonlyRef, type Ref } from "./reactivity";
|
|
2
|
+
import { currentScope } from "./reactive-scope";
|
|
3
|
+
import { ownedArray, type OwnedArray, type OwnedReadonlyArray, type OwnedRecord } from "./owned-collections";
|
|
4
|
+
import type { RuntimeScope } from "./runtime";
|
|
5
|
+
|
|
6
|
+
export type BreakpointTable = Readonly<Record<string, number>> & { readonly base: 0 };
|
|
7
|
+
|
|
8
|
+
export type MediaQuery<Name extends string> =
|
|
9
|
+
| { readonly minWidth: Name; readonly maxWidth?: never; readonly reducedMotion?: never }
|
|
10
|
+
| { readonly maxWidth: Name; readonly minWidth?: never; readonly reducedMotion?: never }
|
|
11
|
+
| { readonly reducedMotion: true; readonly minWidth?: never; readonly maxWidth?: never };
|
|
12
|
+
|
|
13
|
+
export type MediaRead = (this: void, observer: MediaObserver<string>, owner: RuntimeScope) => void;
|
|
14
|
+
|
|
15
|
+
interface MediaElement {
|
|
16
|
+
readonly mediaReads?: readonly MediaRead[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// JSX creates child elements before native frames exist. Keep only actual hook reads until mount.
|
|
20
|
+
const pending = new Map<RuntimeScope, OwnedArray<MediaRead>>();
|
|
21
|
+
const owned = new Set<RuntimeScope>();
|
|
22
|
+
|
|
23
|
+
export function markMediaReads(this: void) {
|
|
24
|
+
const scope = pending.size > 0 ? currentScope() : undefined;
|
|
25
|
+
const reads = scope === undefined ? undefined : pending.get(scope);
|
|
26
|
+
return reads === undefined ? 0 : reads.length;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function attachMediaReads<Element extends MediaElement>(this: void, element: Element, mark: number): Element {
|
|
30
|
+
const scope = pending.size > 0 ? currentScope() : undefined;
|
|
31
|
+
const reads = scope === undefined ? undefined : pending.get(scope);
|
|
32
|
+
if (reads === undefined || reads.length === mark) return element;
|
|
33
|
+
const attached = reads.splice(mark) as OwnedArray<MediaRead>;
|
|
34
|
+
if (reads.length === 0 && scope !== undefined) pending.delete(scope);
|
|
35
|
+
if (element.mediaReads !== undefined) {
|
|
36
|
+
for (const read of element.mediaReads as OwnedReadonlyArray<MediaRead>) attached.push(read);
|
|
37
|
+
}
|
|
38
|
+
return { ...(element as OwnedRecord<Element>), mediaReads: attached };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function captureRead<Value>(initial: Value, select: (this: void, observer: MediaObserver<string>) => ReadonlyRef<Value>): ReadonlyRef<Value> {
|
|
42
|
+
const value = ref(initial);
|
|
43
|
+
const scope = currentScope();
|
|
44
|
+
if (scope === undefined) return value;
|
|
45
|
+
let reads = pending.get(scope);
|
|
46
|
+
if (reads === undefined) {
|
|
47
|
+
reads = ownedArray<MediaRead>();
|
|
48
|
+
pending.set(scope, reads);
|
|
49
|
+
if (!owned.has(scope)) {
|
|
50
|
+
owned.add(scope);
|
|
51
|
+
scope.onDispose(() => { pending.delete(scope); owned.delete(scope); });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
reads.push((observer, owner) => {
|
|
55
|
+
const source = select(observer);
|
|
56
|
+
value.value = source.value;
|
|
57
|
+
owner.watch(() => source.value, next => { value.value = next; });
|
|
58
|
+
});
|
|
59
|
+
return value;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function createMediaHooks<Table extends BreakpointTable>(this: void, breakpoints: Table) {
|
|
63
|
+
type Name = Extract<keyof Table, string>;
|
|
64
|
+
return {
|
|
65
|
+
useBreakpoint: () => captureRead("base" as Name, observer => observer.breakpoint as ReadonlyRef<Name>),
|
|
66
|
+
useMediaQuery: (query: MediaQuery<Name>) => captureRead(
|
|
67
|
+
query.reducedMotion !== true && (query.minWidth === undefined || breakpoints[query.minWidth] === 0),
|
|
68
|
+
observer => observer.matches(query),
|
|
69
|
+
),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface MediaObserver<Name extends string> {
|
|
74
|
+
readonly breakpoint: ReadonlyRef<Name>;
|
|
75
|
+
matches(this: void, query: MediaQuery<Name>): ReadonlyRef<boolean>;
|
|
76
|
+
resize(this: void, width: number): void;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
interface Threshold<Name> {
|
|
80
|
+
readonly name: Name;
|
|
81
|
+
readonly width: number;
|
|
82
|
+
min?: Ref<boolean>;
|
|
83
|
+
max?: Ref<boolean>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function countReached<Name>(thresholds: OwnedReadonlyArray<Threshold<Name>>, width: number, inclusive: boolean) {
|
|
87
|
+
let from = 0;
|
|
88
|
+
let to = thresholds.length;
|
|
89
|
+
while (from < to) {
|
|
90
|
+
const candidate = Math.floor((from + to) / 2);
|
|
91
|
+
const next = thresholds[candidate].width;
|
|
92
|
+
if (next < width || inclusive && next === width) from = candidate + 1;
|
|
93
|
+
else to = candidate;
|
|
94
|
+
}
|
|
95
|
+
return from;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function createMediaObserver<Table extends BreakpointTable>(this: void, breakpoints: Table,
|
|
99
|
+
reducedMotion: ReadonlyRef<boolean>): MediaObserver<Extract<keyof Table, string>> {
|
|
100
|
+
type Name = Extract<keyof Table, string>;
|
|
101
|
+
const thresholds = ownedArray<Threshold<Name>>();
|
|
102
|
+
for (const name of Object.keys(breakpoints) as Name[]) thresholds.push({ name, width: breakpoints[name] });
|
|
103
|
+
thresholds.sort((left, right) => {
|
|
104
|
+
const order = left.width - right.width;
|
|
105
|
+
return order !== 0 ? order : left.name < right.name ? -1 : 1;
|
|
106
|
+
});
|
|
107
|
+
const byName = new Map<Name, Threshold<Name>>();
|
|
108
|
+
for (const threshold of thresholds) byName.set(threshold.name, threshold);
|
|
109
|
+
const breakpoint = ref<Name>("base" as Name);
|
|
110
|
+
let width = 0;
|
|
111
|
+
let minCount = countReached(thresholds, width, true);
|
|
112
|
+
let maxCount = countReached(thresholds, width, false);
|
|
113
|
+
|
|
114
|
+
function matches(this: void, query: MediaQuery<Name>): ReadonlyRef<boolean> {
|
|
115
|
+
const name = query.minWidth ?? query.maxWidth;
|
|
116
|
+
if (name === undefined) return reducedMotion;
|
|
117
|
+
const threshold = byName.get(name)!;
|
|
118
|
+
return query.minWidth !== undefined
|
|
119
|
+
? threshold.min ??= ref(width >= threshold.width)
|
|
120
|
+
: threshold.max ??= ref(width <= threshold.width);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function resize(this: void, nextWidth: number) {
|
|
124
|
+
if (!Number.isFinite(nextWidth) || nextWidth < 0 || width === nextWidth) return;
|
|
125
|
+
width = nextWidth;
|
|
126
|
+
const nextMin = countReached(thresholds, width, true);
|
|
127
|
+
const nextMax = countReached(thresholds, width, false);
|
|
128
|
+
const grew = nextMin > minCount;
|
|
129
|
+
for (let i = Math.min(minCount, nextMin); i < Math.max(minCount, nextMin); i += 1) {
|
|
130
|
+
const signal = thresholds[i].min;
|
|
131
|
+
if (signal !== undefined) signal.value = grew;
|
|
132
|
+
}
|
|
133
|
+
const shrank = nextMax < maxCount;
|
|
134
|
+
for (let i = Math.min(maxCount, nextMax); i < Math.max(maxCount, nextMax); i += 1) {
|
|
135
|
+
const signal = thresholds[i].max;
|
|
136
|
+
if (signal !== undefined) signal.value = shrank;
|
|
137
|
+
}
|
|
138
|
+
if (minCount !== nextMin) breakpoint.value = thresholds[nextMin - 1].name;
|
|
139
|
+
minCount = nextMin;
|
|
140
|
+
maxCount = nextMax;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return { breakpoint, matches, resize };
|
|
144
|
+
}
|