@tschk/moonshine-svelte 0.4.49 → 0.4.80
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 +46 -8
- package/package.json +17 -5
- package/src/abort-signal.ts +60 -0
- package/src/animate.ts +71 -0
- package/src/attachments.ts +93 -0
- package/src/derived.ts +35 -5
- package/src/easing.ts +176 -0
- package/src/effect.ts +17 -4
- package/src/events.ts +76 -0
- package/src/kit.ts +1973 -0
- package/src/legacy.ts +361 -0
- package/src/motion.ts +504 -0
- package/src/navigation.ts +1134 -0
- package/src/pre.ts +18 -4
- package/src/prop-id.ts +43 -0
- package/src/reactivity-window.ts +173 -0
- package/src/reactivity.ts +983 -0
- package/src/runes.ts +1 -0
- package/src/server.ts +56 -0
- package/src/ssr.ts +34 -3
- package/src/svelte.ts +30 -1
- package/src/tick-queue.ts +3 -0
- package/src/transition.ts +348 -0
package/README.md
CHANGED
|
@@ -10,12 +10,21 @@ exports `crepusRenderer` with the framework's `Renderer` contract. The framework
|
|
|
10
10
|
remains optional for runtime imports and bridge-only browser/Bun/Node bundles.
|
|
11
11
|
This requirement does not apply to the signal-only `@tschk/moonshine` core entry.
|
|
12
12
|
|
|
13
|
-
| Subpath
|
|
14
|
-
|
|
|
15
|
-
| `.`
|
|
16
|
-
| `./svelte`
|
|
17
|
-
| `./
|
|
18
|
-
| `./
|
|
13
|
+
| Subpath | Provides |
|
|
14
|
+
| --------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
15
|
+
| `.` | signal bridges and lazy native SSR compatibility renderer |
|
|
16
|
+
| `./svelte` | lifecycle, `tick`, `settled`, `flushSync`, `untrack`, `getAbortSignal`, context, typed `createContext`, events |
|
|
17
|
+
| `./reactivity` | independent `SvelteMap`, `SvelteSet`, `SvelteDate`, `SvelteURL`, `SvelteURLSearchParams`, `MediaQuery`, `ReactiveValue`, `createSubscriber` |
|
|
18
|
+
| `./reactivity/window` | independent reactive `innerWidth`, `innerHeight`, `outerWidth`, `outerHeight`, `screenLeft`, `screenTop`, `scrollX`, `scrollY`, `online`, `devicePixelRatio` |
|
|
19
|
+
| `./attachments` | independent `createAttachmentKey`, `isAttachmentKey`, `fromAction`, and `Attachment` |
|
|
20
|
+
| `./transition` | independent `fade`, `blur`, `fly`, `slide`, `scale`, `draw`, and `crossfade` |
|
|
21
|
+
| `./animate` | independent `flip` FLIP animation helper |
|
|
22
|
+
| `./server` | independent `render(component, options)` (`{ body, head, html }`) for `svelte/server` |
|
|
23
|
+
| `./legacy` | independent `run`, event modifiers (`handlers`, `once`, `preventDefault`, `stopPropagation`, `self`, `trusted`, `passive`, `nonpassive`), `createBubbler`, `createClassComponent`, `asClassComponent` |
|
|
24
|
+
| `./navigation` | independent SvelteKit `$app/state`, `$app/stores`, `$app/navigation`, `$app/paths`, `$app/environment`, and `$app/forms` runtime (`page`, `navigating`, `updated`, `goto`, `invalidate`, `enhance`) |
|
|
25
|
+
| `./kit` | independent `@sveltejs/kit`, `@sveltejs/kit/hooks`, `$app/server`, `$env/*`, and `$service-worker` helpers (`error`, `redirect`, `fail`, `json`, `text`, `sequence`, `createKitCookies`, `getRequestEvent`, `read`, `query`, `prerender`, `command`, `form`, `executeKitServerLoad`, `executeKitUniversalLoad`, `executeKitAction`) |
|
|
26
|
+
| `./class` | opt-in `classValue` and `classDirectives` for source classes |
|
|
27
|
+
| `./store` | independent `writable`, `readable`, `readonly`, `derived`, `get`, `toStore`, and `fromStore` |
|
|
19
28
|
|
|
20
29
|
`classValue` flattens nested class arrays and includes truthy object keys. Empty
|
|
21
30
|
results return `undefined`, omitting the DOM/SSR attribute. Top-level primitive
|
|
@@ -29,8 +38,8 @@ uses it for authored intrinsic `class:` directives, including dynamic elements.
|
|
|
29
38
|
Source parsing and independent input memoization belong to Crepus; this helper
|
|
30
39
|
does not execute Svelte's compiled component instructions or implement style directives.
|
|
31
40
|
|
|
32
|
-
The optional `/runes` entry exports compiler-facing `createState`, `createDerived
|
|
33
|
-
and `createRuneEffect`.
|
|
41
|
+
The optional `/runes` entry exports compiler-facing `createState`, `createDerived`,
|
|
42
|
+
`createProp`, `propsId`, and `createRuneEffect`.
|
|
34
43
|
|
|
35
44
|
Compiler-facing `/runes` also exports `spreadProps(...layers)` for live component
|
|
36
45
|
props. Layers are objects, nullish values, or object getters; later layers win.
|
|
@@ -593,3 +602,32 @@ Svelte 5.57.1 reference retains them. This is bounded authored-source support,
|
|
|
593
602
|
without hydration or Svelte's compiled component ABI. Set
|
|
594
603
|
`MOONSHINE_CREPUS_COMPILER` to the local compiler to enable the isolated source
|
|
595
604
|
regression in `test/standalone.test.ts`.
|
|
605
|
+
|
|
606
|
+
### `$props.id()`, `settled`, and `flushSync`
|
|
607
|
+
|
|
608
|
+
`/runes` and `/ssr` export `propsId()` for Svelte 5's `$props.id()` rune, and the
|
|
609
|
+
local unpublished Crepus compiler lowers top-level component-instance
|
|
610
|
+
`const id = $props.id()` / `let id = $props.id()` declarations to `propsId()`.
|
|
611
|
+
`propsId()` generates deterministic sequential `"s-0"`, `"s-1"`, ... IDs scoped to
|
|
612
|
+
the root `Owner` via `WeakMap<Owner, { next: number }>` so DOM and SSR component
|
|
613
|
+
trees produce identical IDs without cross-request or cross-root counter drift,
|
|
614
|
+
and throws when called outside active component/root initialization or during
|
|
615
|
+
cleanup computations.
|
|
616
|
+
|
|
617
|
+
`/svelte` and `/ssr` also export `settled()`, `flushSync(fn?)`, `untrack(fn)`, and
|
|
618
|
+
`getAbortSignal()` (with `settled` and `getAbortSignal` routed alongside `tick`
|
|
619
|
+
from `'svelte'` in the local Crepus compiler). `flushSync(fn?)` executes an
|
|
620
|
+
optional callback and synchronously drains all queued native rune work
|
|
621
|
+
(`drainTickWork()`), while `settled()` awaits queued rune flushes in a loop until
|
|
622
|
+
all chained rune effects settle (`hasPendingTickWork()` is false).
|
|
623
|
+
`getAbortSignal()` returns an `AbortSignal` that aborts when the enclosing
|
|
624
|
+
`$effect`, `$effect.pre`, `onMount`, `{@attach}`, or `$derived` reaction re-runs
|
|
625
|
+
or is disposed, and throws when called outside an active effect or derived
|
|
626
|
+
computation.
|
|
627
|
+
|
|
628
|
+
`/reactivity` exports `ReactiveValue<T>` alongside `createSubscriber`,
|
|
629
|
+
`MediaQuery`, `SvelteDate`, `SvelteMap`, `SvelteSet`, `SvelteURL`, and
|
|
630
|
+
`SvelteURLSearchParams`, while `/reactivity/window` exports reactive browser
|
|
631
|
+
metrics (`innerWidth`, `innerHeight`, `outerWidth`, `outerHeight`, `screenLeft`,
|
|
632
|
+
`screenTop`, `scrollX`, `scrollY`, `online`, and `devicePixelRatio`) backed by
|
|
633
|
+
`ReactiveValue` and returning `undefined` on SSR.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tschk/moonshine-svelte",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.80",
|
|
4
4
|
"description": "Independent Svelte store and native lifecycle APIs with Moonshine signal bridges",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"type": "module",
|
|
@@ -24,17 +24,29 @@
|
|
|
24
24
|
"./each": "./src/each.ts",
|
|
25
25
|
"./mount": "./src/mount.ts",
|
|
26
26
|
"./server-mount": "./src/server-mount.ts",
|
|
27
|
-
"./const": "./src/const-derived.ts"
|
|
27
|
+
"./const": "./src/const-derived.ts",
|
|
28
|
+
"./animate": "./src/animate.ts",
|
|
29
|
+
"./attachments": "./src/attachments.ts",
|
|
30
|
+
"./easing": "./src/easing.ts",
|
|
31
|
+
"./events": "./src/events.ts",
|
|
32
|
+
"./motion": "./src/motion.ts",
|
|
33
|
+
"./reactivity": "./src/reactivity.ts",
|
|
34
|
+
"./reactivity/window": "./src/reactivity-window.ts",
|
|
35
|
+
"./transition": "./src/transition.ts",
|
|
36
|
+
"./server": "./src/server.ts",
|
|
37
|
+
"./legacy": "./src/legacy.ts",
|
|
38
|
+
"./navigation": "./src/navigation.ts",
|
|
39
|
+
"./kit": "./src/kit.ts"
|
|
28
40
|
},
|
|
29
41
|
"dependencies": {
|
|
30
|
-
"@tschk/moonshine": "^0.4.
|
|
31
|
-
"@tschk/moonshine-dom": "^0.4.
|
|
42
|
+
"@tschk/moonshine": "^0.4.80",
|
|
43
|
+
"@tschk/moonshine-dom": "^0.4.80"
|
|
32
44
|
},
|
|
33
45
|
"devDependencies": {
|
|
34
46
|
"typescript": "7.0.2"
|
|
35
47
|
},
|
|
36
48
|
"peerDependencies": {
|
|
37
|
-
"@tschk/moonshine-framework": "^0.4.
|
|
49
|
+
"@tschk/moonshine-framework": "^0.4.80"
|
|
38
50
|
},
|
|
39
51
|
"peerDependenciesMeta": {
|
|
40
52
|
"@tschk/moonshine-framework": {
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/** Scoped AbortSignal tracking for native Svelte effects and derived computations. */
|
|
2
|
+
|
|
3
|
+
export type AbortContext = {
|
|
4
|
+
controller?: AbortController;
|
|
5
|
+
aborted: boolean;
|
|
6
|
+
abort(): void;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
let currentAbortContext: AbortContext | undefined;
|
|
10
|
+
|
|
11
|
+
export function createAbortContext(): AbortContext {
|
|
12
|
+
const ctx: AbortContext = {
|
|
13
|
+
aborted: false,
|
|
14
|
+
abort() {
|
|
15
|
+
if (ctx.aborted) return;
|
|
16
|
+
ctx.aborted = true;
|
|
17
|
+
ctx.controller?.abort();
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
return ctx;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function withAbortContext<T>(ctx: AbortContext, fn: () => T): T {
|
|
24
|
+
const prev = currentAbortContext;
|
|
25
|
+
currentAbortContext = ctx;
|
|
26
|
+
try {
|
|
27
|
+
return fn();
|
|
28
|
+
} finally {
|
|
29
|
+
currentAbortContext = prev;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function withoutAbortContext<T>(fn: () => T): T {
|
|
34
|
+
const prev = currentAbortContext;
|
|
35
|
+
currentAbortContext = undefined;
|
|
36
|
+
try {
|
|
37
|
+
return fn();
|
|
38
|
+
} finally {
|
|
39
|
+
currentAbortContext = prev;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Return an `AbortSignal` that aborts when the current native effect or derived
|
|
45
|
+
* computation re-runs or is disposed.
|
|
46
|
+
*/
|
|
47
|
+
export function getAbortSignal(): AbortSignal {
|
|
48
|
+
if (!currentAbortContext) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
"getAbortSignal() can only be called inside an effect or derived",
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
if (!currentAbortContext.controller) {
|
|
54
|
+
currentAbortContext.controller = new AbortController();
|
|
55
|
+
if (currentAbortContext.aborted) {
|
|
56
|
+
currentAbortContext.controller.abort();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return currentAbortContext.controller.signal;
|
|
60
|
+
}
|
package/src/animate.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/** Independent Svelte 5 `svelte/animate` FLIP animation helper (`flip`). */
|
|
2
|
+
import { cubicOut } from "./easing";
|
|
3
|
+
|
|
4
|
+
export interface AnimationConfig {
|
|
5
|
+
delay?: number;
|
|
6
|
+
duration?: number;
|
|
7
|
+
easing?: (t: number) => number;
|
|
8
|
+
css?: (t: number, u: number) => string;
|
|
9
|
+
tick?: (t: number, u: number) => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface FlipParams {
|
|
13
|
+
delay?: number;
|
|
14
|
+
duration?: number | ((len: number) => number);
|
|
15
|
+
easing?: (t: number) => number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function getElementStyle(
|
|
19
|
+
node: Element,
|
|
20
|
+
): CSSStyleDeclaration | Record<string, string> {
|
|
21
|
+
if (typeof getComputedStyle === "function") {
|
|
22
|
+
try {
|
|
23
|
+
return getComputedStyle(node);
|
|
24
|
+
} catch {
|
|
25
|
+
// Fallback for synthetic/detached elements in test environments
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return (node as HTMLElement).style ?? {};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function flip(
|
|
32
|
+
node: Element,
|
|
33
|
+
{
|
|
34
|
+
from,
|
|
35
|
+
to,
|
|
36
|
+
}: {
|
|
37
|
+
from:
|
|
38
|
+
DOMRect | { left: number; top: number; width: number; height: number };
|
|
39
|
+
to: DOMRect | { left: number; top: number; width: number; height: number };
|
|
40
|
+
},
|
|
41
|
+
params: FlipParams = {},
|
|
42
|
+
): AnimationConfig {
|
|
43
|
+
const style = getElementStyle(node);
|
|
44
|
+
const transform =
|
|
45
|
+
style.transform === "none" || !style.transform ? "" : `${style.transform} `;
|
|
46
|
+
|
|
47
|
+
const {
|
|
48
|
+
delay = 0,
|
|
49
|
+
duration = (d: number) => Math.sqrt(d) * 120,
|
|
50
|
+
easing = cubicOut,
|
|
51
|
+
} = params;
|
|
52
|
+
|
|
53
|
+
const dx = from.left - to.left;
|
|
54
|
+
const dy = from.top - to.top;
|
|
55
|
+
const dsx = to.width ? from.width / to.width : 1;
|
|
56
|
+
const dsy = to.height ? from.height / to.height : 1;
|
|
57
|
+
const d = Math.hypot(dx, dy);
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
delay,
|
|
61
|
+
duration: typeof duration === "function" ? duration(d) : duration,
|
|
62
|
+
easing,
|
|
63
|
+
css: (_t, u) => {
|
|
64
|
+
const x = u * dx;
|
|
65
|
+
const y = u * dy;
|
|
66
|
+
const sx = dsx + (1 - dsx) * (1 - u);
|
|
67
|
+
const sy = dsy + (1 - dsy) * (1 - u);
|
|
68
|
+
return `transform: ${transform}translate(${x}px, ${y}px) scale(${sx}, ${sy});`;
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/** Independent Svelte 5 `svelte/attachments` implementation on the Moonshine reactive graph. */
|
|
2
|
+
import {
|
|
3
|
+
createRenderEffect,
|
|
4
|
+
getOwner,
|
|
5
|
+
untrack,
|
|
6
|
+
} from "@tschk/moonshine-dom/reactive";
|
|
7
|
+
import type { Action, ActionPayload, Attachment } from "./directives";
|
|
8
|
+
import { trackActionState } from "./state";
|
|
9
|
+
|
|
10
|
+
export type { Attachment };
|
|
11
|
+
|
|
12
|
+
const ATTACHMENT_SYMBOL_PREFIX = "@attach";
|
|
13
|
+
const attachmentKeys = new Set<symbol>();
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Creates a unique symbol key recognized as an element attachment property
|
|
17
|
+
* when spread onto an element (`{ [createAttachmentKey()]: myAttachment }`).
|
|
18
|
+
*/
|
|
19
|
+
export function createAttachmentKey(): symbol {
|
|
20
|
+
const key = Symbol(ATTACHMENT_SYMBOL_PREFIX);
|
|
21
|
+
attachmentKeys.add(key);
|
|
22
|
+
return key;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Check whether a property key was created by `createAttachmentKey()`. */
|
|
26
|
+
export function isAttachmentKey(key: PropertyKey): key is symbol {
|
|
27
|
+
return (
|
|
28
|
+
typeof key === "symbol" &&
|
|
29
|
+
(attachmentKeys.has(key) || key.description === ATTACHMENT_SYMBOL_PREFIX)
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function different(previous: unknown, next: unknown): boolean {
|
|
34
|
+
return previous !== previous
|
|
35
|
+
? next === next
|
|
36
|
+
: previous !== next ||
|
|
37
|
+
(previous !== null && typeof previous === "object") ||
|
|
38
|
+
typeof previous === "function";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Converts a classic Svelte `use:action` function into a Svelte 5 `Attachment`.
|
|
43
|
+
*/
|
|
44
|
+
export function fromAction<
|
|
45
|
+
E extends Element,
|
|
46
|
+
P = undefined,
|
|
47
|
+
A extends Record<string, any> = Record<never, never>,
|
|
48
|
+
>(
|
|
49
|
+
action: Action<E, P, A> | ((node: E, param: P) => void | ActionPayload<P>),
|
|
50
|
+
fn: () => P,
|
|
51
|
+
): Attachment<E>;
|
|
52
|
+
export function fromAction<
|
|
53
|
+
E extends Element,
|
|
54
|
+
A extends Record<string, any> = Record<never, never>,
|
|
55
|
+
>(
|
|
56
|
+
action:
|
|
57
|
+
Action<E, undefined, A> | ((node: E) => void | ActionPayload<undefined>),
|
|
58
|
+
): Attachment<E>;
|
|
59
|
+
export function fromAction<E extends Element, P = undefined>(
|
|
60
|
+
action: (node: E, param?: P) => void | ActionPayload<P>,
|
|
61
|
+
fn?: () => P,
|
|
62
|
+
): Attachment<E> {
|
|
63
|
+
return (element: E) => {
|
|
64
|
+
const payload =
|
|
65
|
+
untrack(() => (fn ? action(element, fn()) : action(element))) || {};
|
|
66
|
+
|
|
67
|
+
if (fn && typeof payload.update === "function") {
|
|
68
|
+
const owner = getOwner();
|
|
69
|
+
if (owner && !owner.disposed) {
|
|
70
|
+
let initialized = false;
|
|
71
|
+
let previous: unknown = {};
|
|
72
|
+
createRenderEffect(() => {
|
|
73
|
+
const next = fn();
|
|
74
|
+
trackActionState(next);
|
|
75
|
+
if (initialized && different(previous, next)) {
|
|
76
|
+
previous = next;
|
|
77
|
+
payload.update!(next);
|
|
78
|
+
} else if (!initialized) {
|
|
79
|
+
previous = next;
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
initialized = true;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (typeof payload.destroy === "function") {
|
|
87
|
+
return () => {
|
|
88
|
+
payload.destroy!();
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
return undefined;
|
|
92
|
+
};
|
|
93
|
+
}
|
package/src/derived.ts
CHANGED
|
@@ -25,6 +25,11 @@ import {
|
|
|
25
25
|
withHistoryMutation,
|
|
26
26
|
type HistoryRead,
|
|
27
27
|
} from "./history";
|
|
28
|
+
import {
|
|
29
|
+
createAbortContext,
|
|
30
|
+
withAbortContext,
|
|
31
|
+
type AbortContext,
|
|
32
|
+
} from "./abort-signal";
|
|
28
33
|
|
|
29
34
|
export function createDerived<T>(fn: () => T): {
|
|
30
35
|
get value(): T;
|
|
@@ -55,8 +60,10 @@ export function createDerived<T>(fn: () => T): {
|
|
|
55
60
|
let overrideVersions = new Map<Trackable, number>();
|
|
56
61
|
let override!: T, last!: T;
|
|
57
62
|
let subscriptions = new Map<Trackable, () => void>();
|
|
63
|
+
let abortCtx: AbortContext | undefined;
|
|
58
64
|
function invalidate(): void {
|
|
59
65
|
if (disposed) return;
|
|
66
|
+
abortCtx?.abort();
|
|
60
67
|
// Invalidating a committed override restores the previous computed inputs.
|
|
61
68
|
// A same-flush assignment already recorded its earlier value, which wins.
|
|
62
69
|
if (overridden) recordHistory(token, "override", undefined, undefined);
|
|
@@ -70,11 +77,16 @@ export function createDerived<T>(fn: () => T): {
|
|
|
70
77
|
let next!: T,
|
|
71
78
|
failed = false,
|
|
72
79
|
failure: unknown;
|
|
80
|
+
abortCtx?.abort();
|
|
81
|
+
const runAbortCtx = createAbortContext();
|
|
82
|
+
abortCtx = runAbortCtx;
|
|
73
83
|
const versions = new Map<Trackable, number>();
|
|
74
84
|
const dependencies = collectDeps(
|
|
75
85
|
() => {
|
|
76
86
|
try {
|
|
77
|
-
const collected = collectHistoryReads(() =>
|
|
87
|
+
const collected = collectHistoryReads(() =>
|
|
88
|
+
runWithOwner(owner, () => withAbortContext(runAbortCtx, fn)),
|
|
89
|
+
);
|
|
78
90
|
next = collected.value;
|
|
79
91
|
reads = collected.reads;
|
|
80
92
|
} catch (error) {
|
|
@@ -110,13 +122,18 @@ export function createDerived<T>(fn: () => T): {
|
|
|
110
122
|
failed = true;
|
|
111
123
|
}
|
|
112
124
|
subscriptions = nextSubscriptions;
|
|
113
|
-
if (failed)
|
|
125
|
+
if (failed) {
|
|
126
|
+
runAbortCtx.abort();
|
|
127
|
+
throw failure;
|
|
128
|
+
}
|
|
114
129
|
if (
|
|
115
130
|
dependencies.some(
|
|
116
131
|
(dependency) => dependency.version !== versions.get(dependency),
|
|
117
132
|
)
|
|
118
|
-
)
|
|
133
|
+
) {
|
|
134
|
+
runAbortCtx.abort();
|
|
119
135
|
throw new Error("Derived computations must not write their dependencies");
|
|
136
|
+
}
|
|
120
137
|
// Pull validation can run before this dependency's queued notification.
|
|
121
138
|
// Reset here too so optimistic siblings cannot expose a stale override.
|
|
122
139
|
if (initialized) {
|
|
@@ -159,6 +176,7 @@ export function createDerived<T>(fn: () => T): {
|
|
|
159
176
|
if (owner)
|
|
160
177
|
onCleanup(() => {
|
|
161
178
|
disposed = true;
|
|
179
|
+
abortCtx?.abort();
|
|
162
180
|
for (const unsubscribe of subscriptions.values()) untrack(unsubscribe);
|
|
163
181
|
subscriptions.clear();
|
|
164
182
|
});
|
|
@@ -179,7 +197,14 @@ export function createDerived<T>(fn: () => T): {
|
|
|
179
197
|
if (previous) return previous.value;
|
|
180
198
|
if (initialized && !hasHistory(reads))
|
|
181
199
|
return !recorded && overridden ? override : baseValue;
|
|
182
|
-
|
|
200
|
+
const tempAbort = createAbortContext();
|
|
201
|
+
try {
|
|
202
|
+
return runWithOwner(owner, () =>
|
|
203
|
+
withHistoryCleanup(() => withAbortContext(tempAbort, fn)),
|
|
204
|
+
);
|
|
205
|
+
} finally {
|
|
206
|
+
tempAbort.abort();
|
|
207
|
+
}
|
|
183
208
|
}),
|
|
184
209
|
);
|
|
185
210
|
if (disposed) {
|
|
@@ -194,11 +219,14 @@ export function createDerived<T>(fn: () => T): {
|
|
|
194
219
|
);
|
|
195
220
|
const versions = new Map<Trackable, number>();
|
|
196
221
|
let next!: T;
|
|
222
|
+
const tempAbort = createAbortContext();
|
|
197
223
|
try {
|
|
198
224
|
const dependencies = untrack(() =>
|
|
199
225
|
collectDeps(
|
|
200
226
|
() => {
|
|
201
|
-
next = runWithOwner(owner,
|
|
227
|
+
next = runWithOwner(owner, () =>
|
|
228
|
+
withAbortContext(tempAbort, fn),
|
|
229
|
+
);
|
|
202
230
|
},
|
|
203
231
|
(dependency) => versions.set(dependency, dependency.version),
|
|
204
232
|
),
|
|
@@ -219,6 +247,8 @@ export function createDerived<T>(fn: () => T): {
|
|
|
219
247
|
frozenFailure = true;
|
|
220
248
|
frozenError = error;
|
|
221
249
|
throw error;
|
|
250
|
+
} finally {
|
|
251
|
+
tempAbort.abort();
|
|
222
252
|
}
|
|
223
253
|
}
|
|
224
254
|
if (
|
package/src/easing.ts
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/** Independent host-free Svelte easing functions (`svelte/easing`). */
|
|
2
|
+
|
|
3
|
+
export type EasingFunction = (t: number) => number;
|
|
4
|
+
|
|
5
|
+
export function linear(t: number): number {
|
|
6
|
+
return t;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function backInOut(t: number): number {
|
|
10
|
+
const s = 1.70158 * 1.525;
|
|
11
|
+
if ((t *= 2) < 1) {
|
|
12
|
+
return 0.5 * (t * t * ((s + 1) * t - s));
|
|
13
|
+
}
|
|
14
|
+
return 0.5 * ((t -= 2) * t * ((s + 1) * t + s) + 2);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function backIn(t: number): number {
|
|
18
|
+
const s = 1.70158;
|
|
19
|
+
return t * t * ((s + 1) * t - s);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function backOut(t: number): number {
|
|
23
|
+
const s = 1.70158;
|
|
24
|
+
return --t * t * ((s + 1) * t + s) + 1;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function bounceOut(t: number): number {
|
|
28
|
+
const a = 4.0 / 11.0;
|
|
29
|
+
const b = 8.0 / 11.0;
|
|
30
|
+
const c = 9.0 / 10.0;
|
|
31
|
+
|
|
32
|
+
const ca = 4356.0 / 361.0;
|
|
33
|
+
const cb = 35442.0 / 1805.0;
|
|
34
|
+
const cc = 16061.0 / 1805.0;
|
|
35
|
+
|
|
36
|
+
const t2 = t * t;
|
|
37
|
+
|
|
38
|
+
return t < a
|
|
39
|
+
? 7.5625 * t2
|
|
40
|
+
: t < b
|
|
41
|
+
? 9.075 * t2 - 9.9 * t + 3.4
|
|
42
|
+
: t < c
|
|
43
|
+
? ca * t2 - cb * t + cc
|
|
44
|
+
: 10.8 * t * t - 20.52 * t + 10.72;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function bounceInOut(t: number): number {
|
|
48
|
+
return t < 0.5
|
|
49
|
+
? 0.5 * (1.0 - bounceOut(1.0 - t * 2.0))
|
|
50
|
+
: 0.5 * bounceOut(t * 2.0 - 1.0) + 0.5;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function bounceIn(t: number): number {
|
|
54
|
+
return 1.0 - bounceOut(1.0 - t);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function circInOut(t: number): number {
|
|
58
|
+
if ((t *= 2) < 1) {
|
|
59
|
+
return -0.5 * (Math.sqrt(1 - t * t) - 1);
|
|
60
|
+
}
|
|
61
|
+
return 0.5 * (Math.sqrt(1 - (t -= 2) * t) + 1);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function circIn(t: number): number {
|
|
65
|
+
return 1.0 - Math.sqrt(1.0 - t * t);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function circOut(t: number): number {
|
|
69
|
+
return Math.sqrt(1 - --t * t);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function cubicInOut(t: number): number {
|
|
73
|
+
return t < 0.5 ? 4.0 * t * t * t : 0.5 * Math.pow(2.0 * t - 2.0, 3.0) + 1.0;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function cubicIn(t: number): number {
|
|
77
|
+
return t * t * t;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function cubicOut(t: number): number {
|
|
81
|
+
const f = t - 1.0;
|
|
82
|
+
return f * f * f + 1.0;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function elasticInOut(t: number): number {
|
|
86
|
+
return t < 0.5
|
|
87
|
+
? 0.5 *
|
|
88
|
+
Math.sin(((+13.0 * Math.PI) / 2) * 2.0 * t) *
|
|
89
|
+
Math.pow(2.0, 10.0 * (2.0 * t - 1.0))
|
|
90
|
+
: 0.5 *
|
|
91
|
+
(Math.sin(((-13.0 * Math.PI) / 2) * (2.0 * t - 1.0 + 1.0)) *
|
|
92
|
+
Math.pow(2.0, -10.0 * (2.0 * t - 1.0)) +
|
|
93
|
+
2.0);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function elasticIn(t: number): number {
|
|
97
|
+
return Math.sin((13.0 * t * Math.PI) / 2) * Math.pow(2.0, 10.0 * (t - 1.0));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function elasticOut(t: number): number {
|
|
101
|
+
return (
|
|
102
|
+
Math.sin((-13.0 * (t + 1.0) * Math.PI) / 2) * Math.pow(2.0, -10.0 * t) + 1.0
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function expoInOut(t: number): number {
|
|
107
|
+
return t === 0.0 || t === 1.0
|
|
108
|
+
? t
|
|
109
|
+
: t < 0.5
|
|
110
|
+
? +0.5 * Math.pow(2.0, 20.0 * t - 10.0)
|
|
111
|
+
: -0.5 * Math.pow(2.0, 10.0 - t * 20.0) + 1.0;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function expoIn(t: number): number {
|
|
115
|
+
return t === 0.0 ? t : Math.pow(2.0, 10.0 * (t - 1.0));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function expoOut(t: number): number {
|
|
119
|
+
return t === 1.0 ? t : 1.0 - Math.pow(2.0, -10.0 * t);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function quadInOut(t: number): number {
|
|
123
|
+
t /= 0.5;
|
|
124
|
+
if (t < 1) return 0.5 * t * t;
|
|
125
|
+
t--;
|
|
126
|
+
return -0.5 * (t * (t - 2) - 1);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function quadIn(t: number): number {
|
|
130
|
+
return t * t;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function quadOut(t: number): number {
|
|
134
|
+
return -t * (t - 2.0);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function quartInOut(t: number): number {
|
|
138
|
+
return t < 0.5
|
|
139
|
+
? +8.0 * Math.pow(t, 4.0)
|
|
140
|
+
: -8.0 * Math.pow(t - 1.0, 4.0) + 1.0;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function quartIn(t: number): number {
|
|
144
|
+
return Math.pow(t, 4.0);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function quartOut(t: number): number {
|
|
148
|
+
return Math.pow(t - 1.0, 3.0) * (1.0 - t) + 1.0;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function quintInOut(t: number): number {
|
|
152
|
+
if ((t *= 2) < 1) return 0.5 * t * t * t * t * t;
|
|
153
|
+
return 0.5 * ((t -= 2) * t * t * t * t + 2);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function quintIn(t: number): number {
|
|
157
|
+
return t * t * t * t * t;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function quintOut(t: number): number {
|
|
161
|
+
return --t * t * t * t * t + 1;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function sineInOut(t: number): number {
|
|
165
|
+
return -0.5 * (Math.cos(Math.PI * t) - 1);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function sineIn(t: number): number {
|
|
169
|
+
const v = Math.cos(t * Math.PI * 0.5);
|
|
170
|
+
if (Math.abs(v) < 1e-14) return 1;
|
|
171
|
+
return 1 - v;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function sineOut(t: number): number {
|
|
175
|
+
return Math.sin((t * Math.PI) / 2);
|
|
176
|
+
}
|
package/src/effect.ts
CHANGED
|
@@ -19,6 +19,11 @@ import { flushComponentReferences } from "@tschk/moonshine-dom/component-instanc
|
|
|
19
19
|
import { holdHistory, releaseHistory, withHistoryCleanup } from "./history";
|
|
20
20
|
import { notifyTrackableUnsubscribe } from "./store-demand";
|
|
21
21
|
import { queueTickWork, removeTickWork } from "./tick-queue";
|
|
22
|
+
import {
|
|
23
|
+
createAbortContext,
|
|
24
|
+
withAbortContext,
|
|
25
|
+
withoutAbortContext,
|
|
26
|
+
} from "./abort-signal";
|
|
22
27
|
|
|
23
28
|
const pending = new Set<() => void>();
|
|
24
29
|
let scheduled = false,
|
|
@@ -146,15 +151,23 @@ export function createRuneEffect(fn: () => void | (() => void)): void {
|
|
|
146
151
|
runWithOwner(owner, () =>
|
|
147
152
|
createRoot((stop) => {
|
|
148
153
|
const scope = getOwner();
|
|
149
|
-
|
|
154
|
+
const abortCtx = createAbortContext();
|
|
155
|
+
onCleanup(() => abortCtx.abort());
|
|
156
|
+
stopRun = () => {
|
|
157
|
+
abortCtx.abort();
|
|
158
|
+
runWithOwner(scope, () => withHistoryCleanup(stop));
|
|
159
|
+
};
|
|
150
160
|
dependencies = collectDeps(
|
|
151
161
|
() => {
|
|
152
162
|
try {
|
|
153
|
-
const cleanup = fn
|
|
163
|
+
const cleanup = withAbortContext(abortCtx, fn);
|
|
154
164
|
if (typeof cleanup === "function") {
|
|
155
165
|
if (getOwner()?.disposed)
|
|
156
|
-
withHistoryCleanup(() =>
|
|
157
|
-
|
|
166
|
+
withHistoryCleanup(() =>
|
|
167
|
+
untrack(() => withoutAbortContext(cleanup as () => void)),
|
|
168
|
+
);
|
|
169
|
+
else
|
|
170
|
+
onCleanup(() => withoutAbortContext(cleanup as () => void));
|
|
158
171
|
}
|
|
159
172
|
} catch (error) {
|
|
160
173
|
failed = true;
|