@thyn/core 0.0.322 → 0.0.323
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/dist/element.d.ts +18 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/router.d.ts +16 -0
- package/dist/router.js +66 -0
- package/dist/signals.d.ts +10 -0
- package/dist/signals.js +101 -0
- package/package.json +1 -1
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare function mount(app: any, parent: any): void;
|
|
2
|
+
export declare function collectEffect(effectFn: any): void;
|
|
3
|
+
export declare function createReactiveTextNode(v: any): any;
|
|
4
|
+
export declare function component(name: any, props?: any): any;
|
|
5
|
+
export declare function fixedComponent(name: any, props?: any): any;
|
|
6
|
+
export declare function setAttribute(el: any, key: any, val: any): any;
|
|
7
|
+
export declare function setProperty(el: any, key: any, val: any): any;
|
|
8
|
+
export declare function setReactiveAttribute(el: any, key: any, val: any): any;
|
|
9
|
+
export declare function setReactiveProperty(el: any, key: any, val: any): any;
|
|
10
|
+
export declare function addChildren(e: any, children: any): any;
|
|
11
|
+
export declare function markAsReactive(el: any): any;
|
|
12
|
+
export declare function addEffect(el: any, ef: any): any;
|
|
13
|
+
export declare function show(props: any): (Element | Comment) & {
|
|
14
|
+
$fx?: any;
|
|
15
|
+
};
|
|
16
|
+
export declare function terminalList(props: any): DocumentFragment;
|
|
17
|
+
export declare function list(props: any, terminal?: boolean): DocumentFragment;
|
|
18
|
+
export declare function isolatedTerminalList(props: any): DocumentFragment;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { addChildren, addEffect, component, createReactiveTextNode, fixedComponent, isolatedTerminalList, list, markAsReactive, mount, setAttribute, setProperty, setReactiveAttribute, setReactiveProperty, show, terminalList } from "./element.js";
|
|
2
|
+
export { $effect, $signal, staticEffect } from "./signals.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { addChildren, addEffect, component, createReactiveTextNode, fixedComponent, isolatedTerminalList, list, markAsReactive, mount, setAttribute, setProperty, setReactiveAttribute, setReactiveProperty, show, terminalList } from "./element.js";
|
|
2
|
+
export { $effect, $signal, staticEffect } from "./signals.js";
|
package/dist/router.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const router: {
|
|
2
|
+
path: import("./signals.js").Signal<string>;
|
|
3
|
+
param: (name: string) => string | undefined;
|
|
4
|
+
};
|
|
5
|
+
interface Route {
|
|
6
|
+
path: string;
|
|
7
|
+
component: () => Node;
|
|
8
|
+
}
|
|
9
|
+
export declare function Router({ routes }: {
|
|
10
|
+
routes: Route[];
|
|
11
|
+
}): any;
|
|
12
|
+
export declare function Link({ slot, to }: {
|
|
13
|
+
slot: any;
|
|
14
|
+
to: any;
|
|
15
|
+
}): HTMLAnchorElement;
|
|
16
|
+
export {};
|
package/dist/router.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { component, show } from "./element.js";
|
|
2
|
+
import { $signal, staticEffect } from "./signals.js";
|
|
3
|
+
const params = $signal({});
|
|
4
|
+
export const router = {
|
|
5
|
+
path: $signal(location.pathname),
|
|
6
|
+
param: (name) => params()[name],
|
|
7
|
+
};
|
|
8
|
+
export function Router({ routes }) {
|
|
9
|
+
const current = $signal(null);
|
|
10
|
+
const compiledRoutes = routes.map(route => {
|
|
11
|
+
const compiledRoute = {
|
|
12
|
+
path: null,
|
|
13
|
+
raw: route,
|
|
14
|
+
names: [],
|
|
15
|
+
component: route.component,
|
|
16
|
+
};
|
|
17
|
+
compiledRoute.path = new RegExp(`^${route.path.replace(/\/:([^/]+)/g, (_, name) => {
|
|
18
|
+
compiledRoute.names.push(name);
|
|
19
|
+
return '/([^/]+)';
|
|
20
|
+
})}$`);
|
|
21
|
+
return compiledRoute;
|
|
22
|
+
});
|
|
23
|
+
staticEffect(() => {
|
|
24
|
+
const pn = router.path();
|
|
25
|
+
if (pn !== location.pathname) {
|
|
26
|
+
history.pushState({}, "", pn);
|
|
27
|
+
}
|
|
28
|
+
const ps = {};
|
|
29
|
+
let rt = null;
|
|
30
|
+
for (const route of compiledRoutes) {
|
|
31
|
+
const match = pn.match(route.path);
|
|
32
|
+
if (!match)
|
|
33
|
+
continue;
|
|
34
|
+
for (let i = 0; i < route.names.length; i++) {
|
|
35
|
+
const name = route.names[i];
|
|
36
|
+
ps[name] = decodeURIComponent(match[i + 1]);
|
|
37
|
+
}
|
|
38
|
+
rt = route;
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
;
|
|
42
|
+
current(rt);
|
|
43
|
+
params(ps);
|
|
44
|
+
});
|
|
45
|
+
return component(show, compiledRoutes.map((r) => ({
|
|
46
|
+
if: () => r === current(),
|
|
47
|
+
then: () => component(r.component),
|
|
48
|
+
})));
|
|
49
|
+
}
|
|
50
|
+
export function Link({ slot, to }) {
|
|
51
|
+
const a = document.createElement("a");
|
|
52
|
+
a.href = to;
|
|
53
|
+
for (const ch of slot) {
|
|
54
|
+
a.appendChild(ch);
|
|
55
|
+
}
|
|
56
|
+
a.onclick = (e) => {
|
|
57
|
+
if (!e.defaultPrevented &&
|
|
58
|
+
e.button === 0 &&
|
|
59
|
+
!(e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)) {
|
|
60
|
+
e.preventDefault();
|
|
61
|
+
history.pushState({}, "", to);
|
|
62
|
+
router.path(to);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
return a;
|
|
66
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface Signal<T> {
|
|
2
|
+
(): T;
|
|
3
|
+
(newValue: T): void;
|
|
4
|
+
(updater: (prev: T) => T): void;
|
|
5
|
+
}
|
|
6
|
+
export declare function $signal<T>(initialValue: T): Signal<T>;
|
|
7
|
+
export declare function $effect(fn: (() => (() => void) | void) & any): any;
|
|
8
|
+
export declare function staticEffect(fn: (() => (() => void) | void) & any): any;
|
|
9
|
+
export declare function uncollectedStaticEffect(fn: (() => (() => void) | void) & any): any;
|
|
10
|
+
export declare function cleanup(ef: any): void;
|
package/dist/signals.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { collectEffect } from "./element.js";
|
|
2
|
+
let currentEffect;
|
|
3
|
+
let isBatching;
|
|
4
|
+
const pendingEffects = [];
|
|
5
|
+
function scheduleEffect(effectFn) {
|
|
6
|
+
pendingEffects.push(effectFn);
|
|
7
|
+
if (!isBatching) {
|
|
8
|
+
isBatching = true;
|
|
9
|
+
queueMicrotask(() => {
|
|
10
|
+
for (const ef of pendingEffects) {
|
|
11
|
+
if (ef.dyn) {
|
|
12
|
+
cleanup(ef);
|
|
13
|
+
const prev = currentEffect;
|
|
14
|
+
currentEffect = ef;
|
|
15
|
+
runEffectFn(ef);
|
|
16
|
+
currentEffect = prev;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
ef();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
pendingEffects.length = 0;
|
|
23
|
+
isBatching = false;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export function $signal(initialValue) {
|
|
28
|
+
let value = initialValue;
|
|
29
|
+
const subs = new Set();
|
|
30
|
+
return function (newValue) {
|
|
31
|
+
if (!arguments.length) {
|
|
32
|
+
if (currentEffect) {
|
|
33
|
+
subs.add(currentEffect);
|
|
34
|
+
const td = currentEffect.td;
|
|
35
|
+
currentEffect.td = !td ? subs : (Array.isArray(td) ? (td.push(subs), td) : [td, subs]);
|
|
36
|
+
}
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
newValue = typeof newValue === "function" ? newValue(value) : newValue;
|
|
40
|
+
if (newValue !== value) {
|
|
41
|
+
value = newValue;
|
|
42
|
+
subs.forEach(scheduleEffect);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function runEffectFn(ef) {
|
|
47
|
+
const td = ef();
|
|
48
|
+
if (td) {
|
|
49
|
+
if (ef.td) {
|
|
50
|
+
if (Array.isArray(ef.td)) {
|
|
51
|
+
ef.td.push(td);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
ef.td = [ef.td, td];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
ef.td = td;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export function $effect(fn) {
|
|
63
|
+
const prev = currentEffect;
|
|
64
|
+
currentEffect = fn;
|
|
65
|
+
fn.dyn = true;
|
|
66
|
+
runEffectFn(fn);
|
|
67
|
+
currentEffect = prev;
|
|
68
|
+
collectEffect(fn);
|
|
69
|
+
return fn;
|
|
70
|
+
}
|
|
71
|
+
export function staticEffect(fn) {
|
|
72
|
+
fn.td = null;
|
|
73
|
+
const prev = currentEffect;
|
|
74
|
+
currentEffect = fn;
|
|
75
|
+
fn();
|
|
76
|
+
currentEffect = prev;
|
|
77
|
+
collectEffect(fn);
|
|
78
|
+
return fn;
|
|
79
|
+
}
|
|
80
|
+
export function uncollectedStaticEffect(fn) {
|
|
81
|
+
fn.td = null;
|
|
82
|
+
const prev = currentEffect;
|
|
83
|
+
currentEffect = fn;
|
|
84
|
+
fn();
|
|
85
|
+
currentEffect = prev;
|
|
86
|
+
return fn;
|
|
87
|
+
}
|
|
88
|
+
export function cleanup(ef) {
|
|
89
|
+
if (!ef.td)
|
|
90
|
+
return;
|
|
91
|
+
if (ef.td.delete) {
|
|
92
|
+
ef.td.delete(ef);
|
|
93
|
+
}
|
|
94
|
+
else if (typeof ef.td === "function") {
|
|
95
|
+
ef.td();
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
for (const f of ef.td)
|
|
99
|
+
typeof f === "function" ? f() : f.delete(ef);
|
|
100
|
+
}
|
|
101
|
+
}
|