@tschk/moonshine-svelte 0.4.4 → 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/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
@@ -17,7 +17,13 @@ import { peekComponentContext } from "@tschk/moonshine-dom/component";
17
17
  import { flushComponentReferences } from "@tschk/moonshine-dom/component-instance";
18
18
 
19
19
  import { holdHistory, releaseHistory, withHistoryCleanup } from "./history";
20
+ import { notifyTrackableUnsubscribe } from "./store-demand";
20
21
  import { queueTickWork, removeTickWork } from "./tick-queue";
22
+ import {
23
+ createAbortContext,
24
+ withAbortContext,
25
+ withoutAbortContext,
26
+ } from "./abort-signal";
21
27
 
22
28
  const pending = new Set<() => void>();
23
29
  let scheduled = false,
@@ -145,15 +151,23 @@ export function createRuneEffect(fn: () => void | (() => void)): void {
145
151
  runWithOwner(owner, () =>
146
152
  createRoot((stop) => {
147
153
  const scope = getOwner();
148
- stopRun = () => runWithOwner(scope, () => withHistoryCleanup(stop));
154
+ const abortCtx = createAbortContext();
155
+ onCleanup(() => abortCtx.abort());
156
+ stopRun = () => {
157
+ abortCtx.abort();
158
+ runWithOwner(scope, () => withHistoryCleanup(stop));
159
+ };
149
160
  dependencies = collectDeps(
150
161
  () => {
151
162
  try {
152
- const cleanup = fn();
163
+ const cleanup = withAbortContext(abortCtx, fn);
153
164
  if (typeof cleanup === "function") {
154
165
  if (getOwner()?.disposed)
155
- withHistoryCleanup(() => untrack(cleanup));
156
- else onCleanup(cleanup);
166
+ withHistoryCleanup(() =>
167
+ untrack(() => withoutAbortContext(cleanup as () => void)),
168
+ );
169
+ else
170
+ onCleanup(() => withoutAbortContext(cleanup as () => void));
157
171
  }
158
172
  } catch (error) {
159
173
  failed = true;
@@ -168,6 +182,7 @@ export function createRuneEffect(fn: () => void | (() => void)): void {
168
182
  const cleanup = stopRun as (() => void) | undefined;
169
183
  stopRun = undefined;
170
184
  untrack(() => cleanup?.());
185
+ notifyTrackableUnsubscribe();
171
186
  if (failed) throw failure;
172
187
  return;
173
188
  }
@@ -184,12 +199,16 @@ export function createRuneEffect(fn: () => void | (() => void)): void {
184
199
  }),
185
200
  );
186
201
  for (const [dependency, unsubscribe] of subscriptions)
187
- if (!next.has(dependency)) unsubscribe();
202
+ if (!next.has(dependency)) {
203
+ unsubscribe();
204
+ notifyTrackableUnsubscribe();
205
+ }
188
206
  });
189
207
  subscriptions = next;
190
208
  } catch (error) {
191
209
  for (const [dependency, unsubscribe] of next)
192
210
  if (!subscriptions.has(dependency)) unsubscribe();
211
+ notifyTrackableUnsubscribe();
193
212
  if (!failed) failure = error;
194
213
  failed = true;
195
214
  }
@@ -234,6 +253,7 @@ export function createRuneEffect(fn: () => void | (() => void)): void {
234
253
  }
235
254
  subscriptions.clear();
236
255
  stopRun = undefined;
256
+ notifyTrackableUnsubscribe();
237
257
  if (failed) throw failure;
238
258
  });
239
259
  enqueue();
package/src/events.ts ADDED
@@ -0,0 +1,76 @@
1
+ /** Independent host-free Svelte 5 DOM event helper (`svelte/events`). */
2
+
3
+ export type EventListenerWithTarget<
4
+ Target extends EventTarget,
5
+ E extends Event,
6
+ > = (this: Target, event: E & { currentTarget: Target }) => any;
7
+
8
+ export function on<
9
+ K extends keyof WindowEventMap,
10
+ Target extends Window = Window,
11
+ >(
12
+ window: Target,
13
+ type: K,
14
+ handler: EventListenerWithTarget<Target, WindowEventMap[K]>,
15
+ options?: AddEventListenerOptions | boolean,
16
+ ): () => void;
17
+ export function on<
18
+ K extends keyof DocumentEventMap,
19
+ Target extends Document = Document,
20
+ >(
21
+ document: Target,
22
+ type: K,
23
+ handler: EventListenerWithTarget<Target, DocumentEventMap[K]>,
24
+ options?: AddEventListenerOptions | boolean,
25
+ ): () => void;
26
+ export function on<
27
+ K extends keyof HTMLElementEventMap,
28
+ Target extends HTMLElement = HTMLElement,
29
+ >(
30
+ element: Target,
31
+ type: K,
32
+ handler: EventListenerWithTarget<Target, HTMLElementEventMap[K]>,
33
+ options?: AddEventListenerOptions | boolean,
34
+ ): () => void;
35
+ export function on<
36
+ K extends keyof MediaQueryListEventMap,
37
+ Target extends MediaQueryList = MediaQueryList,
38
+ >(
39
+ target: Target,
40
+ type: K,
41
+ handler: EventListenerWithTarget<Target, MediaQueryListEventMap[K]>,
42
+ options?: AddEventListenerOptions | boolean,
43
+ ): () => void;
44
+ export function on<Target extends EventTarget, E extends Event = Event>(
45
+ element: Target,
46
+ type: string,
47
+ handler: EventListenerWithTarget<Target, E>,
48
+ options?: AddEventListenerOptions | boolean,
49
+ ): () => void;
50
+ export function on(
51
+ element: EventTarget,
52
+ type: string,
53
+ handler: EventListenerWithTarget<EventTarget, Event>,
54
+ options?: AddEventListenerOptions | boolean,
55
+ ): () => void {
56
+ if (
57
+ !element ||
58
+ typeof element.addEventListener !== "function" ||
59
+ typeof element.removeEventListener !== "function"
60
+ ) {
61
+ throw new TypeError("on() requires an EventTarget");
62
+ }
63
+ if (typeof handler !== "function") {
64
+ throw new TypeError("on() requires a callback function");
65
+ }
66
+ const listener: EventListener = (event) => {
67
+ return handler.call(
68
+ element,
69
+ event as Event & { currentTarget: EventTarget },
70
+ );
71
+ };
72
+ element.addEventListener(type, listener, options);
73
+ return () => {
74
+ element.removeEventListener(type, listener, options);
75
+ };
76
+ }