@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
package/src/history.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/** Svelte teardown reads use the first values preceding the current flush. */
|
|
2
|
+
import { getOwner, type Owner } from "@tschk/moonshine-dom/reactive";
|
|
3
|
+
export type HistoryRead = { target: object; key: PropertyKey };
|
|
4
|
+
let values = new Map<object, Map<PropertyKey, unknown>>();
|
|
5
|
+
let historical = new Map<object, unknown>();
|
|
6
|
+
let scheduledEpoch: number | undefined;
|
|
7
|
+
let epoch = 0,
|
|
8
|
+
holds = 0,
|
|
9
|
+
mutationDepth = 0;
|
|
10
|
+
let cleanupOwner: Owner | null | undefined;
|
|
11
|
+
const collectors: HistoryRead[][] = [];
|
|
12
|
+
function clear(): void {
|
|
13
|
+
values.clear();
|
|
14
|
+
historical.clear();
|
|
15
|
+
epoch++;
|
|
16
|
+
}
|
|
17
|
+
function schedule(): void {
|
|
18
|
+
if (holds || (!values.size && !historical.size) || scheduledEpoch === epoch)
|
|
19
|
+
return;
|
|
20
|
+
const expected = epoch;
|
|
21
|
+
scheduledEpoch = expected;
|
|
22
|
+
queueMicrotask(() => {
|
|
23
|
+
if (scheduledEpoch === expected) scheduledEpoch = undefined;
|
|
24
|
+
if (expected === epoch && !holds && !mutationDepth) clear();
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
export function holdHistory(): void {
|
|
28
|
+
holds++;
|
|
29
|
+
}
|
|
30
|
+
export function releaseHistory(): void {
|
|
31
|
+
if (!holds) throw new Error("Unbalanced history release");
|
|
32
|
+
if (--holds === 0 && !mutationDepth) clear();
|
|
33
|
+
}
|
|
34
|
+
export function withHistoryMutation<T>(fn: () => T): T {
|
|
35
|
+
mutationDepth++;
|
|
36
|
+
try {
|
|
37
|
+
return fn();
|
|
38
|
+
} finally {
|
|
39
|
+
if (--mutationDepth === 0) schedule();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export function isHistoryReading(): boolean {
|
|
43
|
+
return cleanupOwner !== undefined && cleanupOwner === getOwner();
|
|
44
|
+
}
|
|
45
|
+
export function withHistoryCleanup<T>(fn: () => T): T {
|
|
46
|
+
const previous = cleanupOwner;
|
|
47
|
+
cleanupOwner = getOwner();
|
|
48
|
+
try {
|
|
49
|
+
return fn();
|
|
50
|
+
} finally {
|
|
51
|
+
cleanupOwner = previous;
|
|
52
|
+
if (cleanupOwner === undefined && !mutationDepth) schedule();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export function recordHistory(
|
|
56
|
+
target: object,
|
|
57
|
+
key: PropertyKey,
|
|
58
|
+
before: unknown,
|
|
59
|
+
after: unknown,
|
|
60
|
+
): void {
|
|
61
|
+
let entries = values.get(target);
|
|
62
|
+
if (!entries) values.set(target, (entries = new Map()));
|
|
63
|
+
if (isHistoryReading()) entries.set(key, after);
|
|
64
|
+
else if (!entries.has(key)) entries.set(key, before);
|
|
65
|
+
}
|
|
66
|
+
export function trackHistoryRead(target: object, key: PropertyKey): void {
|
|
67
|
+
for (const collector of collectors)
|
|
68
|
+
if (!collector.some((read) => read.target === target && read.key === key))
|
|
69
|
+
collector.push({ target, key });
|
|
70
|
+
}
|
|
71
|
+
export function readHistory<T>(
|
|
72
|
+
target: object,
|
|
73
|
+
key: PropertyKey,
|
|
74
|
+
live: () => T,
|
|
75
|
+
): T {
|
|
76
|
+
trackHistoryRead(target, key);
|
|
77
|
+
const entries = values.get(target);
|
|
78
|
+
return isHistoryReading() && entries?.has(key)
|
|
79
|
+
? (entries.get(key) as T)
|
|
80
|
+
: live();
|
|
81
|
+
}
|
|
82
|
+
export function collectHistoryReads<T>(fn: () => T): {
|
|
83
|
+
value: T;
|
|
84
|
+
reads: HistoryRead[];
|
|
85
|
+
} {
|
|
86
|
+
const reads: HistoryRead[] = [];
|
|
87
|
+
collectors.push(reads);
|
|
88
|
+
try {
|
|
89
|
+
return { value: fn(), reads };
|
|
90
|
+
} finally {
|
|
91
|
+
collectors.pop();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
export function hasHistory(reads: readonly HistoryRead[]): boolean {
|
|
95
|
+
return reads.some(
|
|
96
|
+
({ target, key }) => values.get(target)?.has(key) || historical.has(target),
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
export function historicalValue<T>(token: object, compute: () => T): T {
|
|
100
|
+
if (historical.has(token)) return historical.get(token) as T;
|
|
101
|
+
const value = compute();
|
|
102
|
+
historical.set(token, value);
|
|
103
|
+
return value;
|
|
104
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** Independent store bridge; component-language parsing belongs in Crepus. */
|
|
2
|
+
import {
|
|
3
|
+
createSignal as msCreateSignal,
|
|
4
|
+
type Signal as MsSignal,
|
|
5
|
+
} from "@tschk/moonshine";
|
|
6
|
+
import { writable, type Writable } from "./store";
|
|
7
|
+
|
|
8
|
+
export type SvelteWritable<T> = Writable<T>;
|
|
9
|
+
|
|
10
|
+
/** Store subscribers share one kernel subscription while they are active. */
|
|
11
|
+
export function fromMoonshineSignal<T>(signal: MsSignal<T>): SvelteWritable<T> {
|
|
12
|
+
let mirrored = signal.peek();
|
|
13
|
+
const store = writable(mirrored, (set) => {
|
|
14
|
+
mirrored = signal.peek();
|
|
15
|
+
set(mirrored);
|
|
16
|
+
return signal.subscribe(() => {
|
|
17
|
+
const current = signal.peek();
|
|
18
|
+
if (!Object.is(mirrored, current)) {
|
|
19
|
+
mirrored = current;
|
|
20
|
+
set(current);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
function set(next: T): void {
|
|
25
|
+
const previous = signal.peek();
|
|
26
|
+
signal.set(() => next);
|
|
27
|
+
const current = signal.peek();
|
|
28
|
+
// Preserve store object notifications without duplicating deferred kernel writes.
|
|
29
|
+
if (Object.is(previous, current) || !Object.is(mirrored, current)) {
|
|
30
|
+
mirrored = current;
|
|
31
|
+
store.set(current);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
subscribe: store.subscribe,
|
|
36
|
+
set,
|
|
37
|
+
update: (fn) => set(fn(signal.peek())),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function createBridgedWritable<T>(initial: T): SvelteWritable<T> {
|
|
42
|
+
return fromMoonshineSignal(msCreateSignal(initial));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { msCreateSignal as createMoonshineSignal };
|
|
46
|
+
export type { MsSignal as MoonshineSignal };
|
|
47
|
+
export {
|
|
48
|
+
createResource,
|
|
49
|
+
createSignal as createMsSignal,
|
|
50
|
+
createMemo,
|
|
51
|
+
batch,
|
|
52
|
+
untrack,
|
|
53
|
+
} from "@tschk/moonshine";
|
|
54
|
+
export type { Resource, ResourceStatus } from "@tschk/moonshine";
|
|
55
|
+
export { crepusRenderer } from "./framework";
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/** Erased return metadata supplied only by the native SFC compiler. */
|
|
2
|
+
declare const componentMetadata: unique symbol;
|
|
3
|
+
export type ComponentView<
|
|
4
|
+
View,
|
|
5
|
+
Exports extends object,
|
|
6
|
+
Props extends object,
|
|
7
|
+
> = View & {
|
|
8
|
+
readonly [componentMetadata]?: { props: Props; exports: Exports };
|
|
9
|
+
};
|
|
10
|
+
type Metadata<C extends (...args: never[]) => unknown> = [
|
|
11
|
+
ReturnType<C>,
|
|
12
|
+
] extends [never]
|
|
13
|
+
? never
|
|
14
|
+
: typeof componentMetadata extends keyof NonNullable<ReturnType<C>>
|
|
15
|
+
? NonNullable<NonNullable<ReturnType<C>>[typeof componentMetadata]>
|
|
16
|
+
: never;
|
|
17
|
+
export type ComponentProps<C extends (...args: never[]) => unknown> = [
|
|
18
|
+
Metadata<C>,
|
|
19
|
+
] extends [never]
|
|
20
|
+
? never
|
|
21
|
+
: Metadata<C> extends { props: infer P extends object }
|
|
22
|
+
? P
|
|
23
|
+
: never;
|
|
24
|
+
export type ComponentExports<C extends (...args: never[]) => unknown> = [
|
|
25
|
+
Metadata<C>,
|
|
26
|
+
] extends [never]
|
|
27
|
+
? never
|
|
28
|
+
: Metadata<C> extends { exports: infer E extends object }
|
|
29
|
+
? E
|
|
30
|
+
: never;
|
|
31
|
+
/** Restrict the actual public callable while retaining its native view return type. */
|
|
32
|
+
export type ComponentFactory<C extends (...args: never[]) => unknown> = [
|
|
33
|
+
Metadata<C>,
|
|
34
|
+
] extends [never]
|
|
35
|
+
? never
|
|
36
|
+
: {} extends ComponentProps<C>
|
|
37
|
+
? (props?: ComponentProps<C>) => ReturnType<C>
|
|
38
|
+
: (props: ComponentProps<C>) => ReturnType<C>;
|
|
39
|
+
export type MountOptions<Props> = {
|
|
40
|
+
target: Element | ShadowRoot;
|
|
41
|
+
anchor?: Node;
|
|
42
|
+
context?: ReadonlyMap<unknown, unknown>;
|
|
43
|
+
/** Source transitions remain unsupported; both boolean intro values are accepted. */
|
|
44
|
+
intro?: boolean;
|
|
45
|
+
} & ({} extends Props ? { props?: Props } : { props: Props });
|
|
46
|
+
export type UnmountOptions = { outro?: false };
|
package/src/mount.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { createComponent } from "@tschk/moonshine-dom/component";
|
|
2
|
+
import {
|
|
3
|
+
createInsertionRange,
|
|
4
|
+
moveInsertionRange,
|
|
5
|
+
render,
|
|
6
|
+
type DOMChild,
|
|
7
|
+
} from "@tschk/moonshine-dom/dom";
|
|
8
|
+
import { withRuneScheduler } from "./pre";
|
|
9
|
+
const mounted = new WeakMap<object, () => void>();
|
|
10
|
+
export type {
|
|
11
|
+
ComponentView,
|
|
12
|
+
ComponentFactory,
|
|
13
|
+
ComponentProps,
|
|
14
|
+
ComponentExports,
|
|
15
|
+
MountOptions,
|
|
16
|
+
UnmountOptions,
|
|
17
|
+
} from "./mount-types";
|
|
18
|
+
import type {
|
|
19
|
+
ComponentExports,
|
|
20
|
+
ComponentProps,
|
|
21
|
+
MountOptions,
|
|
22
|
+
UnmountOptions,
|
|
23
|
+
} from "./mount-types";
|
|
24
|
+
/** Fresh mount for compiler-marked native SFC factories; hydration is not supported. */
|
|
25
|
+
export function mount<C extends (props: never) => DOMChild>(
|
|
26
|
+
component: C & ([ComponentExports<C>] extends [never] ? never : unknown),
|
|
27
|
+
options: MountOptions<ComponentProps<C>>,
|
|
28
|
+
): ComponentExports<C> {
|
|
29
|
+
const { target, anchor } = options;
|
|
30
|
+
if (
|
|
31
|
+
typeof Element === "undefined" ||
|
|
32
|
+
(!(target instanceof Element) && !(target instanceof ShadowRoot))
|
|
33
|
+
)
|
|
34
|
+
throw new TypeError(
|
|
35
|
+
"Native mount requires a current-realm Element or ShadowRoot target",
|
|
36
|
+
);
|
|
37
|
+
for (const key of Reflect.ownKeys(options)) {
|
|
38
|
+
if (
|
|
39
|
+
key !== "target" &&
|
|
40
|
+
key !== "anchor" &&
|
|
41
|
+
key !== "props" &&
|
|
42
|
+
key !== "context" &&
|
|
43
|
+
key !== "intro"
|
|
44
|
+
)
|
|
45
|
+
throw new TypeError(`Unsupported native mount option: ${String(key)}`);
|
|
46
|
+
}
|
|
47
|
+
if (options.intro !== undefined && typeof options.intro !== "boolean")
|
|
48
|
+
throw new TypeError("Native mount intro must be boolean");
|
|
49
|
+
if (anchor && anchor.parentNode !== target)
|
|
50
|
+
throw new Error("Mount anchor must belong to target");
|
|
51
|
+
let instance: object | undefined,
|
|
52
|
+
range: Node | undefined,
|
|
53
|
+
stop: (() => void) | undefined;
|
|
54
|
+
try {
|
|
55
|
+
stop = render(
|
|
56
|
+
() =>
|
|
57
|
+
withRuneScheduler(() => {
|
|
58
|
+
range = createInsertionRange(() =>
|
|
59
|
+
createComponent(component, (options.props ?? {}) as never, "dom", {
|
|
60
|
+
contexts: options.context,
|
|
61
|
+
capture: (value) => {
|
|
62
|
+
instance = value;
|
|
63
|
+
},
|
|
64
|
+
}),
|
|
65
|
+
);
|
|
66
|
+
return range;
|
|
67
|
+
}),
|
|
68
|
+
target,
|
|
69
|
+
);
|
|
70
|
+
if (anchor) {
|
|
71
|
+
if (anchor.parentNode !== target)
|
|
72
|
+
throw new Error("Mount anchor must still belong to target after setup");
|
|
73
|
+
moveInsertionRange(range!, anchor);
|
|
74
|
+
}
|
|
75
|
+
if (!instance) throw new Error("Component setup was cancelled");
|
|
76
|
+
mounted.set(instance, stop);
|
|
77
|
+
return instance as ComponentExports<C>;
|
|
78
|
+
} catch (error) {
|
|
79
|
+
try {
|
|
80
|
+
stop?.();
|
|
81
|
+
} catch {}
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export async function unmount(
|
|
86
|
+
instance: object,
|
|
87
|
+
options?: UnmountOptions,
|
|
88
|
+
): Promise<void> {
|
|
89
|
+
for (const key of Reflect.ownKeys(options ?? {})) {
|
|
90
|
+
if (key !== "outro")
|
|
91
|
+
throw new TypeError(`Unsupported native unmount option: ${String(key)}`);
|
|
92
|
+
}
|
|
93
|
+
if (options?.outro !== undefined && options.outro !== false)
|
|
94
|
+
throw new TypeError("Native unmount transitions are unsupported");
|
|
95
|
+
const stop = mounted.get(instance);
|
|
96
|
+
if (stop) {
|
|
97
|
+
mounted.delete(instance);
|
|
98
|
+
stop();
|
|
99
|
+
}
|
|
100
|
+
}
|