@tschk/moonshine-svelte 0.0.0-stage → 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 +594 -2
- package/package.json +57 -4
- package/src/await.ts +123 -0
- package/src/binding.ts +116 -0
- package/src/class.ts +78 -0
- package/src/const-derived.ts +47 -0
- package/src/derived.ts +276 -0
- package/src/directives.ts +129 -0
- package/src/each.ts +306 -0
- package/src/effect.ts +250 -0
- package/src/framework.ts +1 -0
- package/src/history.ts +104 -0
- package/src/index.ts +55 -0
- package/src/mount-types.ts +46 -0
- package/src/mount.ts +100 -0
- package/src/pre.ts +749 -0
- package/src/private-class-state.ts +54 -0
- package/src/prop.ts +64 -0
- package/src/raw-snippet-dom.ts +214 -0
- package/src/raw-snippet-ssr.ts +75 -0
- package/src/raw-snippet.ts +36 -0
- package/src/raw-ssr-lifecycle.ts +8 -0
- package/src/raw-state.ts +19 -0
- package/src/runes.ts +15 -0
- package/src/server-mount.ts +31 -0
- package/src/server-prop.ts +29 -0
- package/src/server-store.ts +113 -0
- package/src/spread-element-dom.ts +343 -0
- package/src/spread-element-server.ts +106 -0
- package/src/spread-props.ts +153 -0
- package/src/spread-select.ts +86 -0
- package/src/ssr.ts +54 -0
- package/src/state.ts +492 -0
- package/src/store-binding.ts +125 -0
- package/src/store-demand.ts +18 -0
- package/src/store-internal.ts +34 -0
- package/src/store-interop.ts +145 -0
- package/src/store-write.ts +38 -0
- package/src/store.ts +215 -0
- package/src/svelte.ts +152 -0
- package/src/tick-queue.ts +31 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Subscriber, Unsubscriber } from "./store";
|
|
2
|
+
/** Observable subscriptions may return an object whose method needs its receiver. */
|
|
3
|
+
export function subscribeTo<T>(
|
|
4
|
+
store: { subscribe(run: Subscriber<T>, invalidate?: () => void): unknown },
|
|
5
|
+
run: Subscriber<T>,
|
|
6
|
+
invalidate?: () => void,
|
|
7
|
+
): Unsubscriber {
|
|
8
|
+
const subscription: unknown = store.subscribe(run, invalidate);
|
|
9
|
+
let active = true;
|
|
10
|
+
return () => {
|
|
11
|
+
if (!active) return;
|
|
12
|
+
active = false;
|
|
13
|
+
if (typeof subscription === "function") subscription();
|
|
14
|
+
else if (
|
|
15
|
+
subscription &&
|
|
16
|
+
typeof subscription === "object" &&
|
|
17
|
+
"unsubscribe" in subscription &&
|
|
18
|
+
typeof subscription.unsubscribe === "function"
|
|
19
|
+
)
|
|
20
|
+
subscription.unsubscribe();
|
|
21
|
+
else if (subscription !== undefined)
|
|
22
|
+
throw new TypeError(
|
|
23
|
+
"Store subscription must return a function or unsubscribe object",
|
|
24
|
+
);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function changed<T>(previous: T, next: T): boolean {
|
|
29
|
+
return previous !== previous
|
|
30
|
+
? next === next
|
|
31
|
+
: previous !== next ||
|
|
32
|
+
(previous !== null && typeof previous === "object") ||
|
|
33
|
+
typeof previous === "function";
|
|
34
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { createSignal, untrack } from "@tschk/moonshine";
|
|
2
|
+
import {
|
|
3
|
+
collectDeps,
|
|
4
|
+
hasValueDemand,
|
|
5
|
+
isTracking,
|
|
6
|
+
} from "@tschk/moonshine/tracking";
|
|
7
|
+
import { peekComponentContext } from "@tschk/moonshine-dom/component";
|
|
8
|
+
import { getOwner, onCleanup, type Owner } from "@tschk/moonshine-dom/reactive";
|
|
9
|
+
import { isSSRRendering } from "@tschk/moonshine-dom/ssr-owner";
|
|
10
|
+
import {
|
|
11
|
+
recordHistory,
|
|
12
|
+
trackHistoryRead,
|
|
13
|
+
withHistoryMutation,
|
|
14
|
+
} from "./history";
|
|
15
|
+
import { captureRuneToStoreScope, createRuneToStoreEffect } from "./pre";
|
|
16
|
+
import {
|
|
17
|
+
get as getStoreValue,
|
|
18
|
+
writable,
|
|
19
|
+
type Readable,
|
|
20
|
+
type Unsubscriber,
|
|
21
|
+
type Writable,
|
|
22
|
+
} from "./store";
|
|
23
|
+
import {
|
|
24
|
+
notifyTrackableUnsubscribe,
|
|
25
|
+
registerDemandCheck,
|
|
26
|
+
} from "./store-demand";
|
|
27
|
+
import { subscribeTo } from "./store-internal";
|
|
28
|
+
|
|
29
|
+
const trackedOwners = /* @__PURE__ */ new WeakSet<Owner>();
|
|
30
|
+
|
|
31
|
+
export function toStore<V>(get: () => V, set: (v: V) => void): Writable<V>;
|
|
32
|
+
export function toStore<V>(get: () => V): Readable<V>;
|
|
33
|
+
export function toStore<V>(
|
|
34
|
+
get: () => V,
|
|
35
|
+
set?: (v: V) => void,
|
|
36
|
+
): Writable<V> | Readable<V> {
|
|
37
|
+
const captured = captureRuneToStoreScope();
|
|
38
|
+
const server = isSSRRendering() || peekComponentContext()?.mode === "ssr";
|
|
39
|
+
const initValue = get();
|
|
40
|
+
const store = server
|
|
41
|
+
? writable(initValue)
|
|
42
|
+
: writable(initValue, (setStore) => {
|
|
43
|
+
if (isSSRRendering() || peekComponentContext()?.mode === "ssr") return;
|
|
44
|
+
let ran = initValue !== get();
|
|
45
|
+
const teardown = createRuneToStoreEffect(captured, () => {
|
|
46
|
+
const value = get();
|
|
47
|
+
if (ran) setStore(value);
|
|
48
|
+
});
|
|
49
|
+
ran = true;
|
|
50
|
+
return teardown;
|
|
51
|
+
});
|
|
52
|
+
if (set) {
|
|
53
|
+
return {
|
|
54
|
+
set,
|
|
55
|
+
update: (fn) => set(fn(get())),
|
|
56
|
+
subscribe: store.subscribe,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
subscribe: store.subscribe,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function fromStore<V>(store: Writable<V>): { current: V };
|
|
65
|
+
export function fromStore<V>(store: Readable<V>): { readonly current: V };
|
|
66
|
+
export function fromStore<V>(
|
|
67
|
+
store: Writable<V> | Readable<V>,
|
|
68
|
+
): { current: V } | { readonly current: V } {
|
|
69
|
+
let value = undefined as V;
|
|
70
|
+
let stop: Unsubscriber | undefined;
|
|
71
|
+
let unregisterDemand: (() => void) | undefined;
|
|
72
|
+
const version = createSignal(0);
|
|
73
|
+
const versionNode = collectDeps(() => version())[0]!;
|
|
74
|
+
const originalSubscribe = versionNode.subscribe;
|
|
75
|
+
versionNode.subscribe = (...args) => {
|
|
76
|
+
const unsubscribe = originalSubscribe(...args);
|
|
77
|
+
return () => {
|
|
78
|
+
unsubscribe();
|
|
79
|
+
notifyTrackableUnsubscribe();
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
function current(): V {
|
|
84
|
+
if (
|
|
85
|
+
!isSSRRendering() &&
|
|
86
|
+
peekComponentContext()?.mode !== "ssr" &&
|
|
87
|
+
isTracking() &&
|
|
88
|
+
!getOwner()?.disposed
|
|
89
|
+
) {
|
|
90
|
+
const owner = getOwner();
|
|
91
|
+
if (owner && !trackedOwners.has(owner)) {
|
|
92
|
+
trackedOwners.add(owner);
|
|
93
|
+
onCleanup(notifyTrackableUnsubscribe);
|
|
94
|
+
}
|
|
95
|
+
version();
|
|
96
|
+
trackHistoryRead(version, "value");
|
|
97
|
+
if (!stop) {
|
|
98
|
+
let ran = false;
|
|
99
|
+
stop = untrack(() =>
|
|
100
|
+
subscribeTo(store, (next) => {
|
|
101
|
+
if (ran) {
|
|
102
|
+
withHistoryMutation(() => {
|
|
103
|
+
recordHistory(version, "value", value, next);
|
|
104
|
+
value = next;
|
|
105
|
+
version.set((n) => n + 1);
|
|
106
|
+
});
|
|
107
|
+
} else {
|
|
108
|
+
value = next;
|
|
109
|
+
}
|
|
110
|
+
}),
|
|
111
|
+
);
|
|
112
|
+
ran = true;
|
|
113
|
+
unregisterDemand = registerDemandCheck(() => {
|
|
114
|
+
if (stop && !hasValueDemand(versionNode)) {
|
|
115
|
+
const cleanup = stop;
|
|
116
|
+
stop = undefined;
|
|
117
|
+
unregisterDemand?.();
|
|
118
|
+
unregisterDemand = undefined;
|
|
119
|
+
version.set((n) => n + 1);
|
|
120
|
+
untrack(() => cleanup());
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
return value;
|
|
125
|
+
}
|
|
126
|
+
return getStoreValue(store);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if ("set" in store) {
|
|
130
|
+
return {
|
|
131
|
+
get current() {
|
|
132
|
+
return current();
|
|
133
|
+
},
|
|
134
|
+
set current(v: V) {
|
|
135
|
+
store.set(v);
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
get current() {
|
|
142
|
+
return current();
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { StoreSource } from "./store-binding.js";
|
|
2
|
+
export interface WritableStoreSource<T> extends StoreSource<T> {
|
|
3
|
+
set(value: T): void;
|
|
4
|
+
}
|
|
5
|
+
export function setStoreValue<T, V extends T>(
|
|
6
|
+
store: WritableStoreSource<T>,
|
|
7
|
+
value: V,
|
|
8
|
+
): V {
|
|
9
|
+
store.set(value);
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
export function mutateStoreValue<T, R>(
|
|
13
|
+
store: WritableStoreSource<T>,
|
|
14
|
+
expression: R,
|
|
15
|
+
payload: NoInfer<T>,
|
|
16
|
+
): R {
|
|
17
|
+
store.set(payload);
|
|
18
|
+
return expression;
|
|
19
|
+
}
|
|
20
|
+
export function updateStoreValue<T>(
|
|
21
|
+
store: WritableStoreSource<T>,
|
|
22
|
+
old: NoInfer<T>,
|
|
23
|
+
delta: 1 | -1,
|
|
24
|
+
prefix: boolean,
|
|
25
|
+
): T {
|
|
26
|
+
// Svelte's store update helpers use addition, including its string/BigInt behavior.
|
|
27
|
+
const value = ((old as unknown as number) + delta) as T;
|
|
28
|
+
store.set(value);
|
|
29
|
+
return prefix ? value : old;
|
|
30
|
+
}
|
|
31
|
+
export function mutateServerStore<T, R>(
|
|
32
|
+
reference: { get(store: StoreSource<T>): T | undefined },
|
|
33
|
+
store: WritableStoreSource<T>,
|
|
34
|
+
expression: R,
|
|
35
|
+
): R {
|
|
36
|
+
store.set(reference.get(store)!);
|
|
37
|
+
return expression;
|
|
38
|
+
}
|
package/src/store.ts
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/** Independent synchronous Svelte store protocol; no component runtime needed. */
|
|
2
|
+
import { subscribeTo, changed } from "./store-internal";
|
|
3
|
+
export type Subscriber<T> = (value: T) => void;
|
|
4
|
+
export type Invalidator<T> = (value?: T) => void;
|
|
5
|
+
export type Unsubscriber = () => void;
|
|
6
|
+
export type Updater<T> = (value: T) => T;
|
|
7
|
+
export type StartStopNotifier<T> = (
|
|
8
|
+
set: (value: T) => void,
|
|
9
|
+
update: (fn: Updater<T>) => void,
|
|
10
|
+
) => void | Unsubscriber;
|
|
11
|
+
export interface Readable<T> {
|
|
12
|
+
subscribe(run: Subscriber<T>, invalidate?: Invalidator<T>): Unsubscriber;
|
|
13
|
+
}
|
|
14
|
+
export interface Writable<T> extends Readable<T> {
|
|
15
|
+
set(value: T): void;
|
|
16
|
+
update(fn: Updater<T>): void;
|
|
17
|
+
}
|
|
18
|
+
type Subscription<T> = { run: Subscriber<T>; invalidate: Invalidator<T> };
|
|
19
|
+
const notifications: Array<() => void> = [];
|
|
20
|
+
const noop = () => {};
|
|
21
|
+
|
|
22
|
+
export function writable<T>(
|
|
23
|
+
initial?: T,
|
|
24
|
+
start: StartStopNotifier<T> = noop,
|
|
25
|
+
): Writable<T> {
|
|
26
|
+
let value = initial as T;
|
|
27
|
+
let stop: Unsubscriber | undefined;
|
|
28
|
+
const subscribers = new Set<Subscription<T>>();
|
|
29
|
+
function set(next: T): void {
|
|
30
|
+
if (!changed(value, next)) return;
|
|
31
|
+
value = next;
|
|
32
|
+
if (!stop) return;
|
|
33
|
+
const flush = notifications.length === 0;
|
|
34
|
+
let failure: unknown;
|
|
35
|
+
let failed = false;
|
|
36
|
+
for (const subscriber of subscribers) {
|
|
37
|
+
try {
|
|
38
|
+
subscriber.invalidate();
|
|
39
|
+
} catch (error) {
|
|
40
|
+
if (!failed) failure = error;
|
|
41
|
+
failed = true;
|
|
42
|
+
}
|
|
43
|
+
const current = value;
|
|
44
|
+
notifications.push(() => subscriber.run(current));
|
|
45
|
+
}
|
|
46
|
+
if (flush) {
|
|
47
|
+
try {
|
|
48
|
+
for (let index = 0; index < notifications.length; index++) {
|
|
49
|
+
try {
|
|
50
|
+
notifications[index]!();
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (!failed) failure = error;
|
|
53
|
+
failed = true;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
} finally {
|
|
57
|
+
notifications.length = 0;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (failed) throw failure;
|
|
61
|
+
}
|
|
62
|
+
const update = (fn: Updater<T>) => set(fn(value));
|
|
63
|
+
function subscribe(
|
|
64
|
+
run: Subscriber<T>,
|
|
65
|
+
invalidate: Invalidator<T> = noop,
|
|
66
|
+
): Unsubscriber {
|
|
67
|
+
const subscriber = { run, invalidate };
|
|
68
|
+
subscribers.add(subscriber);
|
|
69
|
+
try {
|
|
70
|
+
if (subscribers.size === 1) stop = start(set, update) || noop;
|
|
71
|
+
run(value);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
subscribers.delete(subscriber);
|
|
74
|
+
if (!subscribers.size) {
|
|
75
|
+
const cleanup = stop;
|
|
76
|
+
stop = undefined;
|
|
77
|
+
try {
|
|
78
|
+
cleanup?.();
|
|
79
|
+
} catch {
|
|
80
|
+
/* Preserve subscription failure. */
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
return () => {
|
|
86
|
+
if (!subscribers.delete(subscriber)) return;
|
|
87
|
+
if (!subscribers.size) {
|
|
88
|
+
const cleanup = stop;
|
|
89
|
+
stop = undefined;
|
|
90
|
+
cleanup?.();
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
return { subscribe, set, update };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function readable<T>(
|
|
98
|
+
initial?: T,
|
|
99
|
+
start?: StartStopNotifier<T>,
|
|
100
|
+
): Readable<T> {
|
|
101
|
+
return { subscribe: writable(initial, start).subscribe };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Expose subscriptions without forwarding a store's mutation methods. */
|
|
105
|
+
export function readonly<T>(store: Readable<T>): Readable<T> {
|
|
106
|
+
return { subscribe: store.subscribe.bind(store) };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
type Stores = Readable<any> | readonly Readable<any>[];
|
|
110
|
+
type Values<S> =
|
|
111
|
+
S extends Readable<infer T>
|
|
112
|
+
? T
|
|
113
|
+
: { [K in keyof S]: S[K] extends Readable<infer T> ? T : never };
|
|
114
|
+
export function derived<S extends Stores, T>(
|
|
115
|
+
stores: S,
|
|
116
|
+
fn: (values: Values<S>) => T,
|
|
117
|
+
initial?: T,
|
|
118
|
+
): Readable<T>;
|
|
119
|
+
export function derived<S extends Stores, T>(
|
|
120
|
+
stores: S,
|
|
121
|
+
fn: (
|
|
122
|
+
values: Values<S>,
|
|
123
|
+
set: (value: T) => void,
|
|
124
|
+
update: (fn: Updater<T>) => void,
|
|
125
|
+
) => void | Unsubscriber,
|
|
126
|
+
initial?: T,
|
|
127
|
+
): Readable<T>;
|
|
128
|
+
export function derived<S extends Stores, T>(
|
|
129
|
+
stores: S,
|
|
130
|
+
fn: (
|
|
131
|
+
values: Values<S>,
|
|
132
|
+
set: (value: T) => void,
|
|
133
|
+
update: (fn: Updater<T>) => void,
|
|
134
|
+
) => T | void | Unsubscriber,
|
|
135
|
+
initial?: T,
|
|
136
|
+
): Readable<T> {
|
|
137
|
+
const multiple = Array.isArray(stores);
|
|
138
|
+
const inputs = (multiple ? stores : [stores]) as readonly Readable<unknown>[];
|
|
139
|
+
if (!inputs.every((store) => store && typeof store.subscribe === "function"))
|
|
140
|
+
throw new TypeError("derived expects readable stores");
|
|
141
|
+
const automatic = fn.length < 2;
|
|
142
|
+
return readable(initial, (set, update) => {
|
|
143
|
+
const values: unknown[] = [];
|
|
144
|
+
const pending = new Set<number>();
|
|
145
|
+
const subscriptions: Unsubscriber[] = [];
|
|
146
|
+
let active = false;
|
|
147
|
+
let cleanup: Unsubscriber = noop;
|
|
148
|
+
function sync(): void {
|
|
149
|
+
if (pending.size) return;
|
|
150
|
+
const previous = cleanup;
|
|
151
|
+
cleanup = noop;
|
|
152
|
+
previous();
|
|
153
|
+
const result = fn(
|
|
154
|
+
(multiple ? values : values[0]) as Values<S>,
|
|
155
|
+
set,
|
|
156
|
+
update,
|
|
157
|
+
);
|
|
158
|
+
if (automatic) set(result as T);
|
|
159
|
+
else if (typeof result === "function") cleanup = result as Unsubscriber;
|
|
160
|
+
}
|
|
161
|
+
function release(): void {
|
|
162
|
+
active = false;
|
|
163
|
+
let failed = false;
|
|
164
|
+
let failure: unknown;
|
|
165
|
+
for (const unsubscribe of [...subscriptions, cleanup]) {
|
|
166
|
+
try {
|
|
167
|
+
unsubscribe();
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (!failed) failure = error;
|
|
170
|
+
failed = true;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
subscriptions.length = 0;
|
|
174
|
+
cleanup = noop;
|
|
175
|
+
if (failed) throw failure;
|
|
176
|
+
}
|
|
177
|
+
try {
|
|
178
|
+
inputs.forEach((store, index) =>
|
|
179
|
+
subscriptions.push(
|
|
180
|
+
subscribeTo(
|
|
181
|
+
store,
|
|
182
|
+
(value) => {
|
|
183
|
+
values[index] = value;
|
|
184
|
+
pending.delete(index);
|
|
185
|
+
if (active) sync();
|
|
186
|
+
},
|
|
187
|
+
() => {
|
|
188
|
+
pending.add(index);
|
|
189
|
+
},
|
|
190
|
+
),
|
|
191
|
+
),
|
|
192
|
+
);
|
|
193
|
+
active = true;
|
|
194
|
+
sync();
|
|
195
|
+
} catch (error) {
|
|
196
|
+
try {
|
|
197
|
+
release();
|
|
198
|
+
} catch {
|
|
199
|
+
/* Preserve derivation failure. */
|
|
200
|
+
}
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
return release;
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export function get<T>(store: Readable<T>): T {
|
|
208
|
+
let value!: T;
|
|
209
|
+
subscribeTo(store, (current) => {
|
|
210
|
+
value = current;
|
|
211
|
+
})();
|
|
212
|
+
return value;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export { fromStore, toStore } from "./store-interop";
|
package/src/svelte.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { rawSSRRegistrar, type Registrar } from "./raw-ssr-lifecycle";
|
|
2
|
+
import { isSSRRendering } from "@tschk/moonshine-dom/ssr-owner";
|
|
3
|
+
import {
|
|
4
|
+
captureSSRRenderAttempt,
|
|
5
|
+
isSSRRenderAccepted,
|
|
6
|
+
} from "@tschk/moonshine-dom/ssr-attempt";
|
|
7
|
+
import {
|
|
8
|
+
componentEvents,
|
|
9
|
+
getComponentContext,
|
|
10
|
+
peekComponentContext,
|
|
11
|
+
getContext as nativeGetContext,
|
|
12
|
+
setContext as nativeSetContext,
|
|
13
|
+
hasContext as nativeHasContext,
|
|
14
|
+
} from "@tschk/moonshine-dom/component";
|
|
15
|
+
import { createRuneEffect } from "./effect";
|
|
16
|
+
import { drainTickWork } from "./tick-queue";
|
|
17
|
+
import { withHistoryCleanup } from "./history";
|
|
18
|
+
|
|
19
|
+
/** Source snippet parameters; compiled render cells are private to Crepus. */
|
|
20
|
+
export type Snippet<Parameters extends unknown[] = []> = (
|
|
21
|
+
...args: number extends Parameters["length"] ? never : Parameters
|
|
22
|
+
) => unknown;
|
|
23
|
+
|
|
24
|
+
/** Native owner lifecycle helpers; Svelte compiler output is not supported. */
|
|
25
|
+
import {
|
|
26
|
+
assertNotCleanupComputation,
|
|
27
|
+
getOwner,
|
|
28
|
+
onCleanup,
|
|
29
|
+
runWithOwner,
|
|
30
|
+
untrack,
|
|
31
|
+
} from "@tschk/moonshine-dom/reactive";
|
|
32
|
+
|
|
33
|
+
function componentOwner(hook: string) {
|
|
34
|
+
assertNotCleanupComputation();
|
|
35
|
+
const owner = getOwner();
|
|
36
|
+
const frame = peekComponentContext();
|
|
37
|
+
if (!owner || owner.disposed || (frame && !frame.active))
|
|
38
|
+
throw new Error(
|
|
39
|
+
`${hook} must be called during native component initialization`,
|
|
40
|
+
);
|
|
41
|
+
return owner;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function onMount(fn: () => unknown): void {
|
|
45
|
+
componentOwner("onMount");
|
|
46
|
+
if (isSSRRendering() || peekComponentContext()?.mode === "ssr") return;
|
|
47
|
+
createRuneEffect(() => {
|
|
48
|
+
const cleanup = untrack(fn);
|
|
49
|
+
if (typeof cleanup === "function") return cleanup as () => void;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function onDestroy(fn: () => void): void {
|
|
54
|
+
const owner = componentOwner("onDestroy");
|
|
55
|
+
const server = isSSRRendering() || peekComponentContext()?.mode === "ssr";
|
|
56
|
+
const registrar = getOwner()?.context?.get(rawSSRRegistrar) as
|
|
57
|
+
Registrar | undefined;
|
|
58
|
+
if (server && registrar && registrar.consumer === peekComponentContext()) {
|
|
59
|
+
registrar.register(fn);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const attempt = server ? captureSSRRenderAttempt() : undefined;
|
|
63
|
+
const cleanup = server
|
|
64
|
+
? () => {
|
|
65
|
+
if (isSSRRenderAccepted(attempt)) fn();
|
|
66
|
+
}
|
|
67
|
+
: () => runWithOwner(owner, () => withHistoryCleanup(fn));
|
|
68
|
+
// Server hooks belong to the authored component, not compiler memo scopes.
|
|
69
|
+
const consumer = server ? peekComponentContext()?.owner : undefined;
|
|
70
|
+
runWithOwner(consumer ?? owner, () => onCleanup(cleanup));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Commit pending native rune work; future promise/timer updates are separate. */
|
|
74
|
+
export async function tick(): Promise<void> {
|
|
75
|
+
await Promise.resolve();
|
|
76
|
+
drainTickWork();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export type EventDispatcher<EventMap extends Record<string, any>> = <
|
|
80
|
+
Type extends keyof EventMap,
|
|
81
|
+
>(
|
|
82
|
+
type: Type,
|
|
83
|
+
...args: undefined extends EventMap[Type]
|
|
84
|
+
? [detail?: EventMap[Type], options?: { cancelable?: boolean }]
|
|
85
|
+
: null extends EventMap[Type]
|
|
86
|
+
? [detail?: EventMap[Type], options?: { cancelable?: boolean }]
|
|
87
|
+
: [detail: EventMap[Type], options?: { cancelable?: boolean }]
|
|
88
|
+
) => boolean;
|
|
89
|
+
|
|
90
|
+
export function createEventDispatcher<
|
|
91
|
+
EventMap extends Record<string, any> = any,
|
|
92
|
+
>(): EventDispatcher<EventMap> {
|
|
93
|
+
const frame = getComponentContext();
|
|
94
|
+
return ((
|
|
95
|
+
type: string,
|
|
96
|
+
detail?: unknown,
|
|
97
|
+
options?: { cancelable?: boolean },
|
|
98
|
+
) => {
|
|
99
|
+
if (frame.owner.disposed) return true;
|
|
100
|
+
return untrack(() => {
|
|
101
|
+
const props = frame.props;
|
|
102
|
+
if (!props || (typeof props !== "object" && typeof props !== "function"))
|
|
103
|
+
return true;
|
|
104
|
+
const events = Reflect.get(props, componentEvents);
|
|
105
|
+
if (!events || !Object.hasOwn(events, type)) return true;
|
|
106
|
+
const listeners = events[type];
|
|
107
|
+
if (listeners == null) return true;
|
|
108
|
+
const callbacks = Array.isArray(listeners)
|
|
109
|
+
? listeners.slice()
|
|
110
|
+
: [listeners];
|
|
111
|
+
if (!callbacks.every((listener) => typeof listener === "function"))
|
|
112
|
+
throw new TypeError("Component event listeners must be functions");
|
|
113
|
+
const event = new CustomEvent(type, {
|
|
114
|
+
detail,
|
|
115
|
+
cancelable: options?.cancelable ?? false,
|
|
116
|
+
bubbles: false,
|
|
117
|
+
});
|
|
118
|
+
// Native function components have no legacy exported-instance `this`.
|
|
119
|
+
for (const callback of callbacks)
|
|
120
|
+
Reflect.apply(callback, undefined, [event]);
|
|
121
|
+
return !event.defaultPrevented;
|
|
122
|
+
});
|
|
123
|
+
}) as EventDispatcher<EventMap>;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function getContext<T>(key: unknown): T {
|
|
127
|
+
return nativeGetContext<T>(key) as T;
|
|
128
|
+
}
|
|
129
|
+
export function setContext<T>(key: unknown, value: T): T {
|
|
130
|
+
return nativeSetContext(key, value);
|
|
131
|
+
}
|
|
132
|
+
export function hasContext(key: unknown): boolean {
|
|
133
|
+
return nativeHasContext(key);
|
|
134
|
+
}
|
|
135
|
+
/** The live per-instance map, including inherited entries. */
|
|
136
|
+
export function getAllContexts<T extends Map<any, any> = Map<any, any>>(): T {
|
|
137
|
+
return getComponentContext().contexts as T;
|
|
138
|
+
}
|
|
139
|
+
export function createContext<T>(): [() => T, (value: T) => T, () => boolean] {
|
|
140
|
+
const key = Symbol("svelte-context");
|
|
141
|
+
return [
|
|
142
|
+
() => {
|
|
143
|
+
if (!hasContext(key))
|
|
144
|
+
throw new Error(
|
|
145
|
+
"Context has not been set in this component or its ancestors",
|
|
146
|
+
);
|
|
147
|
+
return getContext<T>(key);
|
|
148
|
+
},
|
|
149
|
+
(value) => setContext(key, value),
|
|
150
|
+
() => hasContext(key),
|
|
151
|
+
];
|
|
152
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** Private coordination of the optional rune queues; no kernel scheduling policy. */
|
|
2
|
+
const pending = /* @__PURE__ */ new Map<() => void, () => void>();
|
|
3
|
+
|
|
4
|
+
export function queueTickWork(run: () => void, cancel: () => void): void {
|
|
5
|
+
pending.set(run, cancel);
|
|
6
|
+
}
|
|
7
|
+
export function removeTickWork(run: () => void): void {
|
|
8
|
+
pending.delete(run);
|
|
9
|
+
}
|
|
10
|
+
export function drainTickWork(): void {
|
|
11
|
+
const counts = new Map<() => void, number>();
|
|
12
|
+
let failed = false,
|
|
13
|
+
failure: unknown;
|
|
14
|
+
while (pending.size) {
|
|
15
|
+
const [run, cancel] = pending.entries().next().value!;
|
|
16
|
+
pending.delete(run);
|
|
17
|
+
const count = (counts.get(run) ?? 0) + 1;
|
|
18
|
+
counts.set(run, count);
|
|
19
|
+
try {
|
|
20
|
+
if (count > 1000) {
|
|
21
|
+
if (!failed) failure = Error("Native tick update limit exceeded");
|
|
22
|
+
failed = true;
|
|
23
|
+
cancel();
|
|
24
|
+
} else run();
|
|
25
|
+
} catch (error) {
|
|
26
|
+
if (!failed) failure = error;
|
|
27
|
+
failed = true;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (failed) throw failure;
|
|
31
|
+
}
|