@tschk/moonshine-vue 0.4.4 → 0.4.49
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 +271 -48
- package/package.json +9 -4
- package/src/app.ts +62 -9
- package/src/context.ts +13 -1
- package/src/error-context.ts +53 -3
- package/src/events.ts +30 -2
- package/src/expose.ts +123 -0
- package/src/lifecycle.ts +67 -13
- package/src/model-equality.ts +43 -0
- package/src/model-ssr.ts +39 -0
- package/src/model.ts +222 -0
- package/src/reactive.ts +1429 -115
- package/src/select-ssr.ts +24 -0
- package/src/select.ts +159 -0
- package/src/set.ts +145 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Compiler-facing option selection; source SSR differs from mounted select policy. */
|
|
2
|
+
import { h, type SSRChild } from "@tschk/moonshine-dom/ssr";
|
|
3
|
+
import { equalModelValue } from "./model-equality";
|
|
4
|
+
|
|
5
|
+
export function createServerModelOption(
|
|
6
|
+
props: Record<string, unknown>,
|
|
7
|
+
children: SSRChild,
|
|
8
|
+
read: () => unknown,
|
|
9
|
+
): SSRChild {
|
|
10
|
+
const attributes: Record<string, unknown> = Object.create(null);
|
|
11
|
+
for (const key of Object.keys(props))
|
|
12
|
+
if (key !== "value") attributes[key] = () => props[key];
|
|
13
|
+
const value = "value" in props ? props.value : null;
|
|
14
|
+
if (value != null && ["string", "number", "boolean"].includes(typeof value))
|
|
15
|
+
attributes.value = value;
|
|
16
|
+
const model = read();
|
|
17
|
+
if (
|
|
18
|
+
Array.isArray(model)
|
|
19
|
+
? model.some((entry) => equalModelValue(entry, value))
|
|
20
|
+
: equalModelValue(model, value)
|
|
21
|
+
)
|
|
22
|
+
attributes.selected = true;
|
|
23
|
+
return h("option", attributes, children);
|
|
24
|
+
}
|
package/src/select.ts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/** Opt-in Vue source select policy; independent of Svelte default/reset adoption. */
|
|
2
|
+
import { h, type DOMChild } from "@tschk/moonshine-dom/dom";
|
|
3
|
+
import {
|
|
4
|
+
createRenderEffect,
|
|
5
|
+
getOwner,
|
|
6
|
+
getRenderScheduler,
|
|
7
|
+
onCleanup,
|
|
8
|
+
untrack,
|
|
9
|
+
} from "@tschk/moonshine-dom/reactive";
|
|
10
|
+
import { equalModelValue } from "./model-equality";
|
|
11
|
+
|
|
12
|
+
const values = new WeakMap<HTMLOptionElement, unknown>();
|
|
13
|
+
const refresh = new WeakMap<HTMLSelectElement, () => void>();
|
|
14
|
+
const valueOf = (option: HTMLOptionElement): unknown =>
|
|
15
|
+
values.has(option) ? values.get(option) : option.value;
|
|
16
|
+
|
|
17
|
+
/** Bind after options; the returned refresh also supports factories building children. */
|
|
18
|
+
export function bindSelectModel(
|
|
19
|
+
select: HTMLSelectElement,
|
|
20
|
+
read: () => unknown,
|
|
21
|
+
write: (value: unknown) => void,
|
|
22
|
+
number = false,
|
|
23
|
+
): () => void {
|
|
24
|
+
const owner = getOwner();
|
|
25
|
+
if (!owner || owner.disposed)
|
|
26
|
+
throw new Error("Select models require a live native owner");
|
|
27
|
+
if (!(select instanceof HTMLSelectElement))
|
|
28
|
+
throw new TypeError("Select models require a select element");
|
|
29
|
+
if (typeof read !== "function" || typeof write !== "function")
|
|
30
|
+
throw new TypeError("Select models require getter/setter functions");
|
|
31
|
+
if (refresh.has(select))
|
|
32
|
+
throw new Error("Select already has a model binding");
|
|
33
|
+
let active = true;
|
|
34
|
+
let assigning = false;
|
|
35
|
+
const apply = () => {
|
|
36
|
+
const model = read();
|
|
37
|
+
if (!active) return;
|
|
38
|
+
if (select.multiple) {
|
|
39
|
+
if (!Array.isArray(model) && !(model instanceof Set)) return;
|
|
40
|
+
for (const option of select.options) {
|
|
41
|
+
const value = valueOf(option);
|
|
42
|
+
const selected =
|
|
43
|
+
model instanceof Set
|
|
44
|
+
? model.has(value)
|
|
45
|
+
: model.some((entry) => equalModelValue(entry, value));
|
|
46
|
+
if (!assigning) option.selected = selected;
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
let found = false;
|
|
50
|
+
for (const option of select.options) {
|
|
51
|
+
const matches = !found && equalModelValue(model, valueOf(option));
|
|
52
|
+
if (!assigning) option.selected = matches;
|
|
53
|
+
if (matches) found = true;
|
|
54
|
+
}
|
|
55
|
+
if (!found && !assigning) select.selectedIndex = -1;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const change = () =>
|
|
59
|
+
untrack(() => {
|
|
60
|
+
const selected = Array.from(select.options)
|
|
61
|
+
.filter((option) => option.selected)
|
|
62
|
+
.map((option) => {
|
|
63
|
+
const value = valueOf(option);
|
|
64
|
+
if (!number || typeof value !== "string") return value;
|
|
65
|
+
const parsed = Number.parseFloat(value);
|
|
66
|
+
return Number.isNaN(parsed) ? value : parsed;
|
|
67
|
+
});
|
|
68
|
+
const model = read();
|
|
69
|
+
assigning = true;
|
|
70
|
+
try {
|
|
71
|
+
write(
|
|
72
|
+
select.multiple
|
|
73
|
+
? model instanceof Set
|
|
74
|
+
? new Set(selected)
|
|
75
|
+
: selected
|
|
76
|
+
: selected[0],
|
|
77
|
+
);
|
|
78
|
+
} finally {
|
|
79
|
+
queueMicrotask(() => {
|
|
80
|
+
assigning = false;
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
select.addEventListener("change", change);
|
|
85
|
+
const observer = new MutationObserver(() => {
|
|
86
|
+
if (active) untrack(apply);
|
|
87
|
+
});
|
|
88
|
+
refresh.set(select, apply);
|
|
89
|
+
onCleanup(() => {
|
|
90
|
+
active = false;
|
|
91
|
+
observer.disconnect();
|
|
92
|
+
refresh.delete(select);
|
|
93
|
+
select.removeEventListener("change", change);
|
|
94
|
+
});
|
|
95
|
+
createRenderEffect(apply);
|
|
96
|
+
observer.observe(select, {
|
|
97
|
+
childList: true,
|
|
98
|
+
subtree: true,
|
|
99
|
+
characterData: true,
|
|
100
|
+
attributes: true,
|
|
101
|
+
attributeFilter: ["value", "selected", "multiple"],
|
|
102
|
+
});
|
|
103
|
+
return () => untrack(apply);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Compiler-facing typed option; omitted values retain the DOM text fallback. */
|
|
107
|
+
export function createModelOption(
|
|
108
|
+
props: Record<string, unknown>,
|
|
109
|
+
children: DOMChild,
|
|
110
|
+
): HTMLOptionElement {
|
|
111
|
+
const attributes: Record<string, unknown> = Object.create(null);
|
|
112
|
+
for (const key of Object.keys(props))
|
|
113
|
+
if (key !== "ref")
|
|
114
|
+
attributes[key] = key.startsWith("on") ? props[key] : () => props[key];
|
|
115
|
+
if ("ref" in props)
|
|
116
|
+
attributes.ref = (option: HTMLOptionElement) => {
|
|
117
|
+
if (typeof props.ref === "function")
|
|
118
|
+
(props.ref as (node: HTMLOptionElement) => void)(option);
|
|
119
|
+
};
|
|
120
|
+
const option = h("option", attributes, children);
|
|
121
|
+
if ("value" in props)
|
|
122
|
+
createRenderEffect(() => {
|
|
123
|
+
values.set(option, props.value);
|
|
124
|
+
const select = option.closest("select");
|
|
125
|
+
if (select) untrack(() => refresh.get(select)?.());
|
|
126
|
+
});
|
|
127
|
+
return option;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Register the model before user handlers, then synchronously select built options. */
|
|
131
|
+
export function createSelectModel(
|
|
132
|
+
props: Record<string, unknown>,
|
|
133
|
+
read: () => unknown,
|
|
134
|
+
write: (value: unknown) => void,
|
|
135
|
+
children: DOMChild,
|
|
136
|
+
number = false,
|
|
137
|
+
): HTMLSelectElement {
|
|
138
|
+
const attributes: Record<string, unknown> = Object.create(null);
|
|
139
|
+
for (const key of Object.keys(props))
|
|
140
|
+
if (!key.startsWith("on") && key !== "ref")
|
|
141
|
+
attributes[key] = () => props[key];
|
|
142
|
+
let apply!: () => void;
|
|
143
|
+
attributes.ref = (select: HTMLSelectElement) => {
|
|
144
|
+
apply = bindSelectModel(select, read, write, number);
|
|
145
|
+
};
|
|
146
|
+
for (const key of Object.keys(props))
|
|
147
|
+
if (key.startsWith("on")) attributes[key] = props[key];
|
|
148
|
+
const select = h("select", attributes, children);
|
|
149
|
+
apply();
|
|
150
|
+
if (typeof props.ref === "function")
|
|
151
|
+
(props.ref as (node: HTMLSelectElement) => void)(select);
|
|
152
|
+
return select;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Compiler helper elements share the authored component's update group. */
|
|
156
|
+
export function captureSelectScope(): <T>(fn: () => T) => T {
|
|
157
|
+
const scheduler = getRenderScheduler();
|
|
158
|
+
return (fn) => (scheduler ? scheduler.branch(fn) : fn());
|
|
159
|
+
}
|
package/src/set.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/** Independent Set instrumentation on the native graph. */
|
|
2
|
+
import { isTracking } from "@tschk/moonshine/tracking";
|
|
3
|
+
import { batch, createSignal } from "@tschk/moonshine-dom/reactive";
|
|
4
|
+
|
|
5
|
+
type Cell = ReturnType<typeof createSignal<number>>;
|
|
6
|
+
type State = {
|
|
7
|
+
target: Set<unknown>;
|
|
8
|
+
wrap: (value: unknown) => unknown;
|
|
9
|
+
raw: (value: unknown) => unknown;
|
|
10
|
+
keys: Map<unknown, Cell>;
|
|
11
|
+
objects: WeakMap<object, Cell>;
|
|
12
|
+
iteration?: Cell;
|
|
13
|
+
cleared?: Cell;
|
|
14
|
+
};
|
|
15
|
+
const states = new WeakMap<object, State>();
|
|
16
|
+
function state(value: object): State {
|
|
17
|
+
const found = states.get(value);
|
|
18
|
+
if (!found) throw new TypeError("Set method requires a native reactive Set");
|
|
19
|
+
return found;
|
|
20
|
+
}
|
|
21
|
+
const objectKey = (key: unknown): key is object =>
|
|
22
|
+
(typeof key === "object" && key !== null) || typeof key === "function";
|
|
23
|
+
function cell(s: State, key: unknown, create = false): Cell | undefined {
|
|
24
|
+
let found = objectKey(key) ? s.objects.get(key) : s.keys.get(key);
|
|
25
|
+
if (!found && create) {
|
|
26
|
+
found = createSignal(0);
|
|
27
|
+
if (objectKey(key)) s.objects.set(key, found);
|
|
28
|
+
else s.keys.set(key, found);
|
|
29
|
+
}
|
|
30
|
+
return found;
|
|
31
|
+
}
|
|
32
|
+
function trackKey(s: State, key: unknown): void {
|
|
33
|
+
if (!isTracking()) return;
|
|
34
|
+
(s.cleared ??= createSignal(0))[0]();
|
|
35
|
+
cell(s, key, true)![0]();
|
|
36
|
+
}
|
|
37
|
+
function trackIteration(s: State): void {
|
|
38
|
+
if (isTracking()) (s.iteration ??= createSignal(0))[0]();
|
|
39
|
+
}
|
|
40
|
+
function bump(signal: Cell | undefined): void {
|
|
41
|
+
signal?.[1]((version) => version + 1);
|
|
42
|
+
}
|
|
43
|
+
function changed(s: State, key: unknown): void {
|
|
44
|
+
batch(() => {
|
|
45
|
+
bump(cell(s, key));
|
|
46
|
+
bump(s.iteration);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function iterator(receiver: Set<unknown>, entries: boolean): SetIterator<any> {
|
|
50
|
+
const s = state(receiver);
|
|
51
|
+
trackIteration(s);
|
|
52
|
+
return (function* () {
|
|
53
|
+
for (const value of s.target) {
|
|
54
|
+
const wrapped = s.wrap(value);
|
|
55
|
+
yield entries ? [wrapped, wrapped] : wrapped;
|
|
56
|
+
}
|
|
57
|
+
return undefined;
|
|
58
|
+
})();
|
|
59
|
+
}
|
|
60
|
+
const methods = {
|
|
61
|
+
get size() {
|
|
62
|
+
const s = state(this);
|
|
63
|
+
trackIteration(s);
|
|
64
|
+
return s.target.size;
|
|
65
|
+
},
|
|
66
|
+
has(this: Set<unknown>, key: unknown) {
|
|
67
|
+
const s = state(this),
|
|
68
|
+
raw = s.raw(key);
|
|
69
|
+
trackKey(s, key);
|
|
70
|
+
if (!Object.is(key, raw)) trackKey(s, raw);
|
|
71
|
+
return s.target.has(key) || s.target.has(raw);
|
|
72
|
+
},
|
|
73
|
+
add(this: Set<unknown>, value: unknown) {
|
|
74
|
+
const s = state(this),
|
|
75
|
+
key = s.raw(value);
|
|
76
|
+
if (!s.target.has(key)) {
|
|
77
|
+
s.target.add(key);
|
|
78
|
+
changed(s, key);
|
|
79
|
+
}
|
|
80
|
+
return this;
|
|
81
|
+
},
|
|
82
|
+
delete(this: Set<unknown>, value: unknown) {
|
|
83
|
+
const s = state(this),
|
|
84
|
+
key = s.target.has(value) ? value : s.raw(value);
|
|
85
|
+
const deleted = s.target.delete(key);
|
|
86
|
+
if (deleted) changed(s, key);
|
|
87
|
+
return deleted;
|
|
88
|
+
},
|
|
89
|
+
clear(this: Set<unknown>) {
|
|
90
|
+
const s = state(this);
|
|
91
|
+
if (!s.target.size) return;
|
|
92
|
+
s.target.clear();
|
|
93
|
+
batch(() => {
|
|
94
|
+
bump(s.cleared);
|
|
95
|
+
bump(s.iteration);
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
keys(this: Set<unknown>) {
|
|
99
|
+
return iterator(this, false);
|
|
100
|
+
},
|
|
101
|
+
values(this: Set<unknown>) {
|
|
102
|
+
return iterator(this, false);
|
|
103
|
+
},
|
|
104
|
+
entries(this: Set<unknown>) {
|
|
105
|
+
return iterator(this, true);
|
|
106
|
+
},
|
|
107
|
+
[Symbol.iterator](this: Set<unknown>) {
|
|
108
|
+
return iterator(this, false);
|
|
109
|
+
},
|
|
110
|
+
forEach(
|
|
111
|
+
this: Set<unknown>,
|
|
112
|
+
callback: (value: unknown, key: unknown, set: Set<unknown>) => void,
|
|
113
|
+
thisArg?: unknown,
|
|
114
|
+
) {
|
|
115
|
+
if (typeof callback !== "function")
|
|
116
|
+
throw new TypeError("Set callback must be a function");
|
|
117
|
+
const s = state(this);
|
|
118
|
+
trackIteration(s);
|
|
119
|
+
s.target.forEach((value) => {
|
|
120
|
+
const wrapped = s.wrap(value);
|
|
121
|
+
callback.call(thisArg, wrapped, wrapped, this);
|
|
122
|
+
});
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
export function createTrackedSet(
|
|
126
|
+
target: Set<unknown>,
|
|
127
|
+
wrap: State["wrap"],
|
|
128
|
+
raw: State["raw"],
|
|
129
|
+
): Set<unknown> {
|
|
130
|
+
const proxy = new Proxy(target, {
|
|
131
|
+
get(source, key, receiver) {
|
|
132
|
+
return Object.hasOwn(methods, key) && key in source
|
|
133
|
+
? Reflect.get(methods, key, receiver)
|
|
134
|
+
: Reflect.get(source, key, receiver);
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
states.set(proxy, {
|
|
138
|
+
target,
|
|
139
|
+
wrap,
|
|
140
|
+
raw,
|
|
141
|
+
keys: new Map(),
|
|
142
|
+
objects: new WeakMap(),
|
|
143
|
+
});
|
|
144
|
+
return proxy;
|
|
145
|
+
}
|