@davidcal/frosth 1.0.0
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/LICENSE +21 -0
- package/README.md +263 -0
- package/package.json +29 -0
- package/src/constants.js +1 -0
- package/src/create_fragment.js +40 -0
- package/src/create_self.js +127 -0
- package/src/create_signal.js +35 -0
- package/src/create_signal_raw.js +24 -0
- package/src/h.js +48 -0
- package/src/render.js +105 -0
- package/src/sigew.js +236 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 David P. Callanan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
# Frosth - Yet another UI framework
|
|
2
|
+
|
|
3
|
+
## Motivation
|
|
4
|
+
|
|
5
|
+
Frontend component frameworks are still a mess (controversial, I know).
|
|
6
|
+
I remember switching to Svelte before React hooks were released. I then switched to Solid before Svelte had its chance to shine.
|
|
7
|
+
When building production-grade tools that prioritize reliability and UX over aesthetics, I found that 50% of my development time was spent fighting with these component frameworks.
|
|
8
|
+
That's why I built Frosth. It's not meant for all apps yet. It only supports client-side rendering. It's best suited for internal tools or dashboards where interaction desync is a big no-no.
|
|
9
|
+
- Async first: Not in a hacked way by slapping Suspense all over the codebase. Initialization is gradual at the state-management level, not bolted onto the UI later.
|
|
10
|
+
- Developers are treated with respect: The process of good software architecture is a solved problem - coming from experience and wisdom. A component framework that forces the "right" architecture is like telling junior developers that they will never be trusted to learn by failing.
|
|
11
|
+
- CRUD-last: Encapsulation is a solved problem. Sometimes code should be kept CRUD-like, sometimes behaviour and state should be encapsulated. An experienced developer is already trained to organize code effectively. Component frameworks don't need something new. Imagine a codebase where for every argument in the constructor, you must attach a getter and setter. You would be shocked! But this is effectively what React enforces for every component, the props are required to be changeable as the lifetime of the component progresses. Solid.js makes it easier to have read-only props, but is still inherently CRUD-based, not behaviour-based.
|
|
12
|
+
|
|
13
|
+
## Setup
|
|
14
|
+
|
|
15
|
+
**Quickstart:** `npx degit davidcallanan/frosth-template`.
|
|
16
|
+
|
|
17
|
+
**Manual:**
|
|
18
|
+
|
|
19
|
+
- `pnpm add -D vite`
|
|
20
|
+
- `pnpm add @davidcal/frosth`
|
|
21
|
+
- `import { mount, obtain, installarea } from "@davidcal/frosth";`
|
|
22
|
+
- `window.addEventListener("load", () => mount(document.getElementById("app"), App));`
|
|
23
|
+
- Start dev server: `vite`
|
|
24
|
+
|
|
25
|
+
## Examples
|
|
26
|
+
|
|
27
|
+
Hello, world:
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
import { mount, obtain, installarea } from "@davidcal/frosth";
|
|
31
|
+
|
|
32
|
+
const App = obtain(async ({ t, s }) => {
|
|
33
|
+
const [installed, installable] = installarea(s);
|
|
34
|
+
|
|
35
|
+
t.installable = installable;
|
|
36
|
+
|
|
37
|
+
installed.set(() => [
|
|
38
|
+
(s) => s.$h1({}, "Hello, world!"),
|
|
39
|
+
]);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
window.addEventListener("load", () => mount(document.getElementById("app"), App));
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Signals are the most basic reactive primitive.
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
import { create_self, create_signal } from "@davidcal/frosth";
|
|
49
|
+
|
|
50
|
+
const name = create_signal(() => "John"); // signal values must be functions
|
|
51
|
+
|
|
52
|
+
name.set(() => "Mary");
|
|
53
|
+
|
|
54
|
+
console.log(name()); // read non-reactively
|
|
55
|
+
|
|
56
|
+
const s = create_self();
|
|
57
|
+
|
|
58
|
+
s.create_effect((s) => {
|
|
59
|
+
console.log(name(s)); // read reactively by passing the reactive scope `s`
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Signal values can themselves be reactive, so you can freely mix and match declarative and imperative logic:
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
const first_name = create_signal(() => "John");
|
|
67
|
+
const last_name = create_signal(() => "Doe");
|
|
68
|
+
|
|
69
|
+
const display_name = create_signal(first_name);
|
|
70
|
+
|
|
71
|
+
display_name.set((s) => `${first_name(s)} ${last_name(s)}`); // all signals expose a reactive scope
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Create a component using `obtain`, and make it renderable using `installarea`.
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
import { obtain, installarea } from "@davidcal/forsth";
|
|
78
|
+
|
|
79
|
+
const Component = obtain(async ({ t, s }) => {
|
|
80
|
+
const [installed, installable] = installarea(s);
|
|
81
|
+
|
|
82
|
+
t.installable = installable; // Components do not need UI associated with them. To attach UI, you simply implement the installable interface.
|
|
83
|
+
|
|
84
|
+
await new Promise((res) => setTimeout(res, 1000)); // Any component can be async
|
|
85
|
+
|
|
86
|
+
installed.set(() => s.$h1({}, "Analyzing data...")); // Components can have multiple loading steps using sequential initialization
|
|
87
|
+
|
|
88
|
+
await new Promise((res) => setTimeout(res, 1000));
|
|
89
|
+
|
|
90
|
+
installed.set(() => s.$h1({ class: () => "bg-green-100" }, "Welcome to my app!"));
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Data can be loaded in parallel:
|
|
95
|
+
|
|
96
|
+
```
|
|
97
|
+
const Component = obtain(async ({ t, s }) => {
|
|
98
|
+
t.videos = t.Promise; // `t` is the equivalent of `this`
|
|
99
|
+
t.friends = t.Promise; // Promises should be declared in the first microtask
|
|
100
|
+
|
|
101
|
+
// (a) Using async-await
|
|
102
|
+
|
|
103
|
+
(async () => {
|
|
104
|
+
const response = await fetch(`https://youtube.com/?user=${await t.friends[0]}`);
|
|
105
|
+
const data = await response.json();
|
|
106
|
+
t.videos = data.myVideos;
|
|
107
|
+
})();
|
|
108
|
+
|
|
109
|
+
// (b) Using then-chaining
|
|
110
|
+
|
|
111
|
+
fetch("https://facemash.com/")
|
|
112
|
+
.then(response => response.json())
|
|
113
|
+
.then(data => t.friends = data.myFriends)
|
|
114
|
+
;
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const instance = Component({ s });
|
|
118
|
+
|
|
119
|
+
instance.friends.then(console.log);
|
|
120
|
+
instance.videos.then(console.log);
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Attach cleanup code to any reactive scope using `on_dispose`:
|
|
124
|
+
|
|
125
|
+
```
|
|
126
|
+
const Component = obtain(async ({ t, s }) => {
|
|
127
|
+
const count = create_signal(() => 0);
|
|
128
|
+
|
|
129
|
+
const interval = setInterval(() => {
|
|
130
|
+
count.set(() => count() + 1);
|
|
131
|
+
}, 1000);
|
|
132
|
+
|
|
133
|
+
s.on_dispose(() => {
|
|
134
|
+
clearInterval(interval);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
s.create_effect((s) => {
|
|
138
|
+
const count$ = count(s);
|
|
139
|
+
const controller = new AbortController();
|
|
140
|
+
|
|
141
|
+
const timeout = setTimeout(() => {
|
|
142
|
+
fetch(`https://monitoring.com/api/log?count=${count$}`, { signal: controller.signal });
|
|
143
|
+
}, Math.random() * 2000);
|
|
144
|
+
|
|
145
|
+
s.on_dispose(() => {
|
|
146
|
+
clearTimeout(timeout);
|
|
147
|
+
controller.abort();
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Multiple installables are keyed by reference to the wrapper `(s) => ...`:
|
|
154
|
+
|
|
155
|
+
```
|
|
156
|
+
const TodoItem = obtain(async ({ t, s }) => {
|
|
157
|
+
const [installed, installable] = installarea(s);
|
|
158
|
+
|
|
159
|
+
t.installable = installable;
|
|
160
|
+
|
|
161
|
+
installed.set(() => s.$h2({
|
|
162
|
+
class: () => "bg-blue-100 m-2",
|
|
163
|
+
}, "Todo: buy coffee"));
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
const Component = obtain(async ({ t, s }) => {
|
|
167
|
+
const [installed, installable] = installarea(s);
|
|
168
|
+
|
|
169
|
+
t.installable = installable;
|
|
170
|
+
|
|
171
|
+
installed.set(() => [
|
|
172
|
+
(s) => s.$div({}, "Click the button to add a new todo item"),
|
|
173
|
+
(s) => s.$button({
|
|
174
|
+
class: () => "border-gray border-2 cursor-pointer",
|
|
175
|
+
onclick: () => () => { // Take care to use two functions here
|
|
176
|
+
const prev = installed() ?? [];
|
|
177
|
+
|
|
178
|
+
installed.set(() => [
|
|
179
|
+
...prev,
|
|
180
|
+
(s) => TodoItem({ s }),
|
|
181
|
+
]);
|
|
182
|
+
},
|
|
183
|
+
}, "Add item!"),
|
|
184
|
+
]);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
Refs are just signals:
|
|
190
|
+
|
|
191
|
+
```
|
|
192
|
+
const Component = obtain(async ({ t, s }) => {
|
|
193
|
+
const [installed, installable] = installarea(s);
|
|
194
|
+
|
|
195
|
+
t.installable = installable;
|
|
196
|
+
|
|
197
|
+
const ref_text = create_signal();
|
|
198
|
+
|
|
199
|
+
installed.set(() => [
|
|
200
|
+
(s) => s.$h1({
|
|
201
|
+
ref: () => ref_text.set,
|
|
202
|
+
}, "Click the button to change my color"),
|
|
203
|
+
(s) => s.$button({
|
|
204
|
+
onclick: () => () => {
|
|
205
|
+
if (ref_text() === undefined) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
ref_text().style.color = "blue";
|
|
210
|
+
ref_text().style.backgroundColor = "black";
|
|
211
|
+
},
|
|
212
|
+
}, "Change color of text"),
|
|
213
|
+
]);
|
|
214
|
+
});
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
Client-side routing is easy:
|
|
218
|
+
|
|
219
|
+
```
|
|
220
|
+
import { A, path } from "@davidcal/frosth";
|
|
221
|
+
|
|
222
|
+
const Nav = obtain(async ({ t, s }) => {
|
|
223
|
+
const [installed, installable] = installarea(s);
|
|
224
|
+
|
|
225
|
+
t.installable = installable;
|
|
226
|
+
|
|
227
|
+
installed.set(() => [
|
|
228
|
+
(s) => A({ s, href: "/", children: "Home" }),
|
|
229
|
+
(s) => A({ s, href: "/about", children: "About" }),
|
|
230
|
+
]);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
const App = obtain(async ({ t, s }) => {
|
|
234
|
+
const [installed, installable] = installarea(s);
|
|
235
|
+
|
|
236
|
+
t.installable = installable;
|
|
237
|
+
|
|
238
|
+
installed.set(() => [
|
|
239
|
+
(s) => Nav({ s }),
|
|
240
|
+
obtain(async ({ t, s }) => {
|
|
241
|
+
const [installed, installable] = installarea(s);
|
|
242
|
+
|
|
243
|
+
t.installable = installable;
|
|
244
|
+
|
|
245
|
+
s.create_effect((s) => {
|
|
246
|
+
const path$ = path(s);
|
|
247
|
+
|
|
248
|
+
if (current_path === "/") {
|
|
249
|
+
installed.set(() => Home);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (current.path === "/about) {
|
|
254
|
+
installed.set(() => About);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
installed.set(() => (s) => s.$h1({}, "404"));
|
|
259
|
+
});
|
|
260
|
+
}),
|
|
261
|
+
]);
|
|
262
|
+
});
|
|
263
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@davidcal/frosth",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Async-first CRUD-last UI framework that treats encapsulation as a solved problem.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/sigew.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/sigew.js",
|
|
9
|
+
"./package.json": "./package.json"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"src"
|
|
13
|
+
],
|
|
14
|
+
"keywords": [
|
|
15
|
+
"ui",
|
|
16
|
+
"framework",
|
|
17
|
+
"signals",
|
|
18
|
+
"async"
|
|
19
|
+
],
|
|
20
|
+
"author": "David P. Callanan",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/davidcallanan/frosth"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"unsuspended-promise": "^1.0.1"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/constants.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const MARK_INSTALLABLE = Symbol("MARK:INSTALLABLE");
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
class Fragment {
|
|
2
|
+
constructor(parent) {
|
|
3
|
+
this.start = document.createComment("fragment");
|
|
4
|
+
this.end = document.createComment("/fragment");
|
|
5
|
+
parent.appendChild(this.start);
|
|
6
|
+
parent.appendChild(this.end);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
clear() {
|
|
10
|
+
while (this.start.nextSibling && this.start.nextSibling !== this.end) {
|
|
11
|
+
this.start.nextSibling.remove();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
appendChild(child) {
|
|
16
|
+
this.end.parentNode.insertBefore(child, this.end);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
set innerHTML(html) {
|
|
20
|
+
this.clear();
|
|
21
|
+
|
|
22
|
+
if (html === "") {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const temp = document.createElement("template");
|
|
27
|
+
temp.innerHTML = html;
|
|
28
|
+
|
|
29
|
+
for (const node of temp.content.childNodes) {
|
|
30
|
+
this.appendChild(node.cloneNode(true));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
set innerText(text) {
|
|
35
|
+
this.clear();
|
|
36
|
+
this.appendChild(document.createTextNode(text));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const create_fragment = (parent) => new Fragment(parent);
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { create_signal } from "./create_signal.js";
|
|
2
|
+
import { h } from "./h.js";
|
|
3
|
+
|
|
4
|
+
class Self {
|
|
5
|
+
constructor(parent, run_effect, options) {
|
|
6
|
+
options ??= {};
|
|
7
|
+
|
|
8
|
+
this._parent = parent;
|
|
9
|
+
this._children = new Set();
|
|
10
|
+
this._is_disposed = false;
|
|
11
|
+
this._run_effect = run_effect;
|
|
12
|
+
this._dispose_handlers = [];
|
|
13
|
+
this._abort_controller = new AbortController();
|
|
14
|
+
|
|
15
|
+
if (this._parent !== undefined) {
|
|
16
|
+
this._parent._children.add(this);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (this._parent === undefined && options.supress_static_lifetime !== true) {
|
|
20
|
+
console.warn("Created top-level self which will never be disposed (static lifetime).");
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
create_effect(callback) {
|
|
25
|
+
let child;
|
|
26
|
+
|
|
27
|
+
const run = () => {
|
|
28
|
+
if (child._is_disposed) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
callback(child);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
child = create_self(this, run);
|
|
36
|
+
|
|
37
|
+
run();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* One can use a housed signal if a component should be reconstructed fresh when a dependency changes.
|
|
43
|
+
*
|
|
44
|
+
* Any dependencies that should participate in this tracking must simply be read immediately, with a fixed value passed, inside the housed signal value function.
|
|
45
|
+
*/
|
|
46
|
+
create_housed_signal(initial) {
|
|
47
|
+
const signal = create_signal(() => ({
|
|
48
|
+
self: undefined,
|
|
49
|
+
value: initial,
|
|
50
|
+
}));
|
|
51
|
+
|
|
52
|
+
const housed = (self) => signal(self).value;
|
|
53
|
+
|
|
54
|
+
housed.set = (get_value) => {
|
|
55
|
+
const prev_self = signal().self;
|
|
56
|
+
|
|
57
|
+
if (prev_self !== undefined && typeof prev_self[Symbol.dispose] === "function") {
|
|
58
|
+
prev_self[Symbol.dispose]();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Intentionally not implementing async-dispose, it serves no purpose in JavaScript from my preliminary analysis.
|
|
62
|
+
// If you are familiar with unsuspended promises, you will see why.
|
|
63
|
+
|
|
64
|
+
const new_self = create_self(this);
|
|
65
|
+
|
|
66
|
+
signal.set(() => ({
|
|
67
|
+
self: new_self,
|
|
68
|
+
value: get_value(new_self),
|
|
69
|
+
}));
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
return housed;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
get is_disposed() {
|
|
76
|
+
return this._is_disposed;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
get abort_signal() {
|
|
80
|
+
return this._abort_controller.signal;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
on_dispose(handler) {
|
|
84
|
+
this._dispose_handlers.push(handler);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
[Symbol.dispose]() {
|
|
88
|
+
if (this._is_disposed) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
this._is_disposed = true;
|
|
93
|
+
this._abort_controller.abort();
|
|
94
|
+
|
|
95
|
+
for (const child of this._children) {
|
|
96
|
+
child[Symbol.dispose]();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
this._children.clear();
|
|
100
|
+
|
|
101
|
+
if (this._parent !== undefined) {
|
|
102
|
+
this._parent._children.delete(this);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
for (const handle_dispose of this._dispose_handlers) {
|
|
106
|
+
handle_dispose();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Creates a reactivity context that must be passed around explicitely.
|
|
113
|
+
*/
|
|
114
|
+
export const create_self = (parent, run_effect, options) => {
|
|
115
|
+
return new Proxy(
|
|
116
|
+
new Self(parent, run_effect, options),
|
|
117
|
+
{
|
|
118
|
+
get: (target, prop, receiver) => {
|
|
119
|
+
if (typeof prop === "string" && prop.startsWith("$")) {
|
|
120
|
+
return (...rest) => h(target, prop.substring(1), ...rest);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return Reflect.get(target, prop, receiver);
|
|
124
|
+
},
|
|
125
|
+
}
|
|
126
|
+
);
|
|
127
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { create_signal_raw } from "./create_signal_raw.js";
|
|
2
|
+
|
|
3
|
+
const error_value = () => {
|
|
4
|
+
throw new Error("Signal value must be a reactive function. Wrap the value in either `() =>` or `(self) =>` depending on whether or not the current value should be reactive, beyond the mere reactivity of the signal.");
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const create_signal = (initial) => {
|
|
8
|
+
if (initial === undefined) {
|
|
9
|
+
initial = () => undefined;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (typeof initial !== "function") {
|
|
13
|
+
error_value();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const raw = create_signal_raw({
|
|
17
|
+
retrieve: initial,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const result = (self) => {
|
|
21
|
+
return raw(self).retrieve();
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
result.set = (value) => {
|
|
25
|
+
if (typeof value !== "function") {
|
|
26
|
+
error_value();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
raw.set({
|
|
30
|
+
retrieve: value,
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
return result;
|
|
35
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const create_signal_raw = (initial) => {
|
|
2
|
+
let value = initial;
|
|
3
|
+
|
|
4
|
+
const subscribers = new Set();
|
|
5
|
+
|
|
6
|
+
const get = (self) => {
|
|
7
|
+
if (self?._run_effect) {
|
|
8
|
+
subscribers.add(self._run_effect);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return value;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const set = (new_value) => {
|
|
15
|
+
value = new_value;
|
|
16
|
+
for (const effect of subscribers) {
|
|
17
|
+
effect();
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
get.set = set;
|
|
22
|
+
|
|
23
|
+
return get;
|
|
24
|
+
};
|
package/src/h.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { create_fragment } from "./create_fragment.js";
|
|
2
|
+
import { render } from "./render.js";
|
|
3
|
+
|
|
4
|
+
export const h = (s, tag, props, children) => {
|
|
5
|
+
const el = document.createElement(tag);
|
|
6
|
+
|
|
7
|
+
for (const key of Object.keys(props)) {
|
|
8
|
+
if (key.startsWith("on")) {
|
|
9
|
+
const event = key.slice(2);
|
|
10
|
+
let prev;
|
|
11
|
+
|
|
12
|
+
s.create_effect(() => {
|
|
13
|
+
const handler = props[key](s);
|
|
14
|
+
|
|
15
|
+
if (prev) {
|
|
16
|
+
el.removeEventListener(event, prev);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
el.addEventListener(event, handler);
|
|
20
|
+
|
|
21
|
+
prev = handler;
|
|
22
|
+
});
|
|
23
|
+
} else if (key === "ref") {
|
|
24
|
+
s.create_effect(() => {
|
|
25
|
+
const ref_fn = props[key](s);
|
|
26
|
+
ref_fn(() => el);
|
|
27
|
+
});
|
|
28
|
+
} else {
|
|
29
|
+
s.create_effect((s) => {
|
|
30
|
+
el.setAttribute(key, props[key](s));
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (typeof children === "string") {
|
|
36
|
+
el.textContent = children;
|
|
37
|
+
|
|
38
|
+
return el;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (children !== undefined) {
|
|
42
|
+
const fragment = create_fragment(el);
|
|
43
|
+
const state = { prev: [] };
|
|
44
|
+
render(s, fragment, children, state);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return el;
|
|
48
|
+
};
|
package/src/render.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { MARK_INSTALLABLE } from "./constants.js";
|
|
2
|
+
import { create_fragment } from "./create_fragment.js";
|
|
3
|
+
import { create_self } from "./create_self.js";
|
|
4
|
+
|
|
5
|
+
const render_diff = (s, prev, next, target) => {
|
|
6
|
+
const prev_map = new Map(prev.map((p) => [p.item, p]));
|
|
7
|
+
const next_set = new Set(next);
|
|
8
|
+
|
|
9
|
+
for (const p of prev) {
|
|
10
|
+
if (!next_set.has(p.item)) {
|
|
11
|
+
p.item_self[Symbol.dispose]();
|
|
12
|
+
p.fragment.clear();
|
|
13
|
+
p.fragment.start.remove();
|
|
14
|
+
p.fragment.end.remove();
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const next_slots = next.map((item) => {
|
|
19
|
+
if (prev_map.has(item)) {
|
|
20
|
+
return prev_map.get(item);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const item_self = create_self(s);
|
|
24
|
+
const fragment = create_fragment(target);
|
|
25
|
+
const new_state = { prev: [] };
|
|
26
|
+
|
|
27
|
+
render(item_self, fragment, item(item_self), new_state);
|
|
28
|
+
|
|
29
|
+
return { item, item_self, fragment, state: new_state };
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
return next_slots;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const render = async (s, target, installed, state) => {
|
|
36
|
+
console.log("rendering", installed);
|
|
37
|
+
|
|
38
|
+
if (Array.isArray(installed)) {
|
|
39
|
+
if (state.prev.length === 0) {
|
|
40
|
+
target.innerHTML = "";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
state.prev = render_diff(s, state.prev, installed, target);
|
|
44
|
+
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
target.innerHTML = "Loading...";
|
|
49
|
+
|
|
50
|
+
if (installed === undefined) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
state.prev = [];
|
|
55
|
+
|
|
56
|
+
if (installed instanceof HTMLElement) {
|
|
57
|
+
if (s.is_disposed) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
target.innerHTML = "";
|
|
62
|
+
target.appendChild(installed);
|
|
63
|
+
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const installable = await installed.installable;
|
|
68
|
+
|
|
69
|
+
if (installable === undefined) {
|
|
70
|
+
if (s.is_disposed) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
console.log("have", await installed, installable);
|
|
75
|
+
console.error("Tried to render 'undefined'.");
|
|
76
|
+
target.innerHTML = "Error (tried to render 'undefined')";
|
|
77
|
+
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (installable[MARK_INSTALLABLE] !== true) {
|
|
82
|
+
if (s.is_disposed) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
console.warn("Encountered bizarre object to render:", installed);
|
|
87
|
+
target.innerHTML = "Error (encountered bizzare object to render)";
|
|
88
|
+
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (s.is_disposed) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
target.innerHTML = "";
|
|
97
|
+
const fragment = create_fragment(target);
|
|
98
|
+
|
|
99
|
+
const new_state = { prev: [] };
|
|
100
|
+
|
|
101
|
+
s.create_effect((s) => {
|
|
102
|
+
const installed = installable.installed(s);
|
|
103
|
+
render(s, fragment, installed, new_state);
|
|
104
|
+
});
|
|
105
|
+
};
|
package/src/sigew.js
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { unsuspended_promise, create_unsuspended_promise } from "unsuspended-promise";
|
|
2
|
+
import { create_fragment } from "./create_fragment.js";
|
|
3
|
+
import { create_self } from "./create_self.js";
|
|
4
|
+
import { create_signal } from "./create_signal.js";
|
|
5
|
+
import { h } from "./h.js";
|
|
6
|
+
import { render } from "./render.js";
|
|
7
|
+
import { MARK_INSTALLABLE } from "./constants.js";
|
|
8
|
+
|
|
9
|
+
export { create_self };
|
|
10
|
+
export { create_signal };
|
|
11
|
+
export { h };
|
|
12
|
+
|
|
13
|
+
export const installarea = (s) => {
|
|
14
|
+
const storage = s.create_housed_signal(() => undefined);
|
|
15
|
+
|
|
16
|
+
const inferred = (s) => {
|
|
17
|
+
const func_or_array = storage(s);
|
|
18
|
+
|
|
19
|
+
if (func_or_array === undefined) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (!Array.isArray(func_or_array)) {
|
|
24
|
+
const func = func_or_array;
|
|
25
|
+
|
|
26
|
+
return [func];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const array = func_or_array;
|
|
30
|
+
|
|
31
|
+
return array;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const installed = (s) => inferred(s);
|
|
35
|
+
installed.set = (value) => storage.set(value);
|
|
36
|
+
|
|
37
|
+
const installable = {
|
|
38
|
+
[MARK_INSTALLABLE]: true,
|
|
39
|
+
installed: (s) => inferred(s),
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
return [installed, installable];
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const DIFFED = Symbol("diffed");
|
|
46
|
+
|
|
47
|
+
export const create_install = (parent) => {
|
|
48
|
+
const install = (children) => {
|
|
49
|
+
parent.innerHTML = "";
|
|
50
|
+
|
|
51
|
+
if (!Array.isArray(children)) {
|
|
52
|
+
children = [children];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
for (const child of children) {
|
|
56
|
+
if (child instanceof HTMLElement) {
|
|
57
|
+
parent.appendChild(child);
|
|
58
|
+
} else if (typeof child === "function") {
|
|
59
|
+
const fragment = create_fragment(parent);
|
|
60
|
+
|
|
61
|
+
fragment.innerText = "Loading...";
|
|
62
|
+
|
|
63
|
+
child({
|
|
64
|
+
install: create_install(fragment),
|
|
65
|
+
});
|
|
66
|
+
} else {
|
|
67
|
+
console.error("Tried to install nonsensical element", child);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
install.diffed = (compute) => ({ [DIFFED]: compute });
|
|
73
|
+
|
|
74
|
+
return install;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export const mount = async (target, create) => {
|
|
78
|
+
const s = create_self(undefined, undefined, {
|
|
79
|
+
supress_static_lifetime: true,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const app = create({
|
|
83
|
+
s,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
target.innerHTML = "Loading...";
|
|
87
|
+
|
|
88
|
+
const installable = await app.installable;
|
|
89
|
+
|
|
90
|
+
target.innerHTML = "";
|
|
91
|
+
|
|
92
|
+
if (installable === undefined) {
|
|
93
|
+
console.error("Component that is mounted must expose an 'installable' property for inclusion in the DOM. Use `t.installable = installable;`");
|
|
94
|
+
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const state = { prev: [] };
|
|
99
|
+
|
|
100
|
+
s.create_effect((s) => {
|
|
101
|
+
const installed = installable.installed(s);
|
|
102
|
+
render(s, target, installed, state);
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export const obtain_component = (factory) => {
|
|
107
|
+
return ({ s, ...rest }) => {
|
|
108
|
+
const child_self = create_self(s);
|
|
109
|
+
|
|
110
|
+
return factory({
|
|
111
|
+
s: child_self,
|
|
112
|
+
...rest,
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export const extract_installable = (factory) => {
|
|
118
|
+
let uly_install = undefined;
|
|
119
|
+
let queued_installables = undefined;
|
|
120
|
+
|
|
121
|
+
const install = (installables) => {
|
|
122
|
+
if (uly_install === undefined) {
|
|
123
|
+
queued_installables = installables;
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
uly_install(installables);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
return [
|
|
131
|
+
factory({
|
|
132
|
+
install,
|
|
133
|
+
}),
|
|
134
|
+
({ install }) => {
|
|
135
|
+
uly_install = install;
|
|
136
|
+
|
|
137
|
+
if (queued_installables) {
|
|
138
|
+
uly_install(queued_installables);
|
|
139
|
+
queued_installables = undefined;
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
];
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const fancy_wrap = (create) => {
|
|
146
|
+
return (kwargs) => {
|
|
147
|
+
const sym_promise = Symbol("[t.Promise]");
|
|
148
|
+
const sym_rej = Symbol("[t.Rej]");
|
|
149
|
+
|
|
150
|
+
const basket = new Map();
|
|
151
|
+
|
|
152
|
+
const t = new Proxy({}, {
|
|
153
|
+
get: (_, prop) => {
|
|
154
|
+
if (prop === "Promise") {
|
|
155
|
+
return sym_promise;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (prop === "Rej") {
|
|
159
|
+
throw new Error("todo");
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return final[prop];
|
|
163
|
+
},
|
|
164
|
+
set: (_, prop, value) => {
|
|
165
|
+
if (value === sym_promise) {
|
|
166
|
+
const [promise, res, rej] = create_unsuspended_promise();
|
|
167
|
+
|
|
168
|
+
basket.set(prop, [promise, res, rej]);
|
|
169
|
+
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const [__, res, rej] = basket.get(prop) ?? [];
|
|
174
|
+
|
|
175
|
+
basket.set(prop, [value]);
|
|
176
|
+
|
|
177
|
+
if (res !== undefined) {
|
|
178
|
+
res(value);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return true;
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
create({
|
|
186
|
+
...kwargs,
|
|
187
|
+
t,
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
const final = new Proxy({}, {
|
|
191
|
+
get: (target, prop) => {
|
|
192
|
+
if (basket.has(prop)) {
|
|
193
|
+
return basket.get(prop)[0];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return target[prop];
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
return final;
|
|
201
|
+
};
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
export const obtain = (create_component) => {
|
|
205
|
+
return obtain_component(unsuspended_promise(fancy_wrap(create_component)));
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
export const path = create_signal();
|
|
209
|
+
|
|
210
|
+
path.set(() => window.location.pathname);
|
|
211
|
+
|
|
212
|
+
window.addEventListener("popstate", () => {
|
|
213
|
+
path.set(() => window.location.pathname);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
export const A = obtain(async ({ t, s, href, children, ...props }) => {
|
|
217
|
+
const [installed, installable] = installarea(s);
|
|
218
|
+
|
|
219
|
+
t.installable = installable;
|
|
220
|
+
|
|
221
|
+
installed.set(() => [
|
|
222
|
+
(s) => s.$a({
|
|
223
|
+
...props,
|
|
224
|
+
href: () => href,
|
|
225
|
+
onclick: () => (e) => {
|
|
226
|
+
if (e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
e.preventDefault();
|
|
231
|
+
window.history.pushState(null, "", href);
|
|
232
|
+
path.set(() => href);
|
|
233
|
+
},
|
|
234
|
+
}, children),
|
|
235
|
+
]);
|
|
236
|
+
});
|