@manyducks.co/dolla 2.0.0-alpha.5 → 2.0.0-alpha.50
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 +81 -933
- package/dist/core/context.d.ts +65 -0
- package/dist/{modules → core}/dolla.d.ts +43 -26
- package/dist/core/markup.d.ts +102 -0
- package/dist/core/nodes/dom.d.ts +13 -0
- package/dist/core/nodes/dynamic.d.ts +30 -0
- package/dist/core/nodes/fragment.d.ts +19 -0
- package/dist/core/nodes/html.d.ts +34 -0
- package/dist/core/nodes/outlet.d.ts +20 -0
- package/dist/core/nodes/portal.d.ts +22 -0
- package/dist/core/nodes/repeat.d.ts +28 -0
- package/dist/core/nodes/view.d.ts +97 -0
- package/dist/core/signals-api.d.ts +42 -0
- package/dist/core/signals.d.ts +22 -0
- package/dist/core/store.d.ts +52 -0
- package/dist/core/symbols.d.ts +4 -0
- package/dist/{views → core/views}/default-crash-view.d.ts +1 -1
- package/dist/core/views/fragment.d.ts +8 -0
- package/dist/{views → core/views}/passthrough.d.ts +2 -2
- package/dist/fragment-DjTOSAcw.js +8 -0
- package/dist/fragment-DjTOSAcw.js.map +1 -0
- package/dist/{modules/http.d.ts → http/index.d.ts} +3 -5
- package/dist/index.d.ts +15 -10
- package/dist/index.js +1056 -1216
- package/dist/index.js.map +1 -1
- package/dist/jsx-dev-runtime.d.ts +2 -2
- package/dist/jsx-dev-runtime.js +8 -8
- package/dist/jsx-dev-runtime.js.map +1 -1
- package/dist/jsx-runtime.d.ts +3 -3
- package/dist/jsx-runtime.js +10 -10
- package/dist/jsx-runtime.js.map +1 -1
- package/dist/markup-DZdmoqTk.js +1501 -0
- package/dist/markup-DZdmoqTk.js.map +1 -0
- package/dist/{modules/router.d.ts → router/index.d.ts} +53 -60
- package/dist/{routing.d.ts → router/router.utils.d.ts} +17 -3
- package/dist/router/router.utils.test.d.ts +1 -0
- package/dist/translate/index.d.ts +133 -0
- package/dist/typeChecking.d.ts +2 -98
- package/dist/typeChecking.test.d.ts +1 -0
- package/dist/types.d.ts +17 -7
- package/dist/utils.d.ts +18 -3
- package/docs/http.md +29 -0
- package/docs/i18n.md +38 -0
- package/docs/index.md +10 -0
- package/docs/router.md +80 -0
- package/docs/setup.md +31 -0
- package/docs/signals.md +166 -0
- package/docs/state.md +141 -0
- package/docs/stores.md +62 -0
- package/docs/views.md +208 -0
- package/examples/webcomponent/index.html +14 -0
- package/examples/webcomponent/main.js +165 -0
- package/index.d.ts +2 -2
- package/notes/TODO.md +6 -0
- package/notes/atomic.md +452 -0
- package/notes/context-routes.md +61 -0
- package/notes/custom-nodes.md +17 -0
- package/notes/elimination.md +33 -0
- package/notes/molecule.md +35 -0
- package/notes/readme-scratch.md +260 -0
- package/notes/route-middleware.md +42 -0
- package/notes/scratch.md +330 -7
- package/notes/splitting.md +5 -0
- package/notes/stores.md +53 -0
- package/package.json +13 -10
- package/vite.config.js +5 -10
- package/build.js +0 -34
- package/dist/markup.d.ts +0 -100
- package/dist/modules/language.d.ts +0 -41
- package/dist/modules/render.d.ts +0 -17
- package/dist/nodes/cond.d.ts +0 -26
- package/dist/nodes/html.d.ts +0 -31
- package/dist/nodes/observer.d.ts +0 -29
- package/dist/nodes/outlet.d.ts +0 -22
- package/dist/nodes/portal.d.ts +0 -19
- package/dist/nodes/repeat.d.ts +0 -34
- package/dist/nodes/text.d.ts +0 -19
- package/dist/passthrough-CtoBcpag.js +0 -1245
- package/dist/passthrough-CtoBcpag.js.map +0 -1
- package/dist/signals.d.ts +0 -101
- package/dist/view.d.ts +0 -50
- package/tests/signals.test.js +0 -135
- /package/dist/{routing.test.d.ts → core/signals-api.test.d.ts} +0 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# What can we remove?
|
|
2
|
+
|
|
3
|
+
I still want to get this library with all its features down below 15kb. It's currently at 17.8kb. That said, I don't want to strip out things that are actually useful. The mission of this library is to be batteries-included, which implies some extra weight.
|
|
4
|
+
|
|
5
|
+
What can be removed without compromising the basics?
|
|
6
|
+
|
|
7
|
+
## Events?
|
|
8
|
+
|
|
9
|
+
> 1st ELIMINATED. We're at 16.77kb now.
|
|
10
|
+
|
|
11
|
+
If we have the ability to get contexts and call methods on them, isn't that just a better version of events?
|
|
12
|
+
|
|
13
|
+
## Logger / Built-in Crash View
|
|
14
|
+
|
|
15
|
+
> REDUCED! Removed simple-color-hash in favor of custom OKLCH hash and we're down to 15.9kb.
|
|
16
|
+
|
|
17
|
+
Logger could be a different package. Crashes could be handled by a crash handler you attach.
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
Dolla.onCrash((error) => {
|
|
21
|
+
// Do what you will with this error.
|
|
22
|
+
});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## HTTP Client?
|
|
26
|
+
|
|
27
|
+
Do we really need this? It's kind of a nice wrapper but the only thing I use the middleware for is to add auth headers for API calls, and it's trivial to write a function around `fetch` that does that. No need for the complexity of middleware.
|
|
28
|
+
|
|
29
|
+
Not really. Fetch is hella basic. I just tried to write my own trivial wrapper around fetch and it took a little too much thought. I don't want to do that for every project. Adding middleware to authenticate feels trivial with `http`.
|
|
30
|
+
|
|
31
|
+
## Markup?
|
|
32
|
+
|
|
33
|
+
Can JSX and `html` return DOM nodes directly? Not really, because views need to be mounted and unmounted like DOM nodes but they don't have the same API. Although I could define a minimal DOM-compatible API so that DOM nodes could directly work.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
Like `compose` but it takes an initial value and a function that can set its value asynchronously. Its callback is a tracking context, so it will be re-run when signals called within are updated.
|
|
2
|
+
|
|
3
|
+
```ts
|
|
4
|
+
interface MoleculeGetter<T> {
|
|
5
|
+
(): T;
|
|
6
|
+
<X>(source: MaybeReactive<X>): X;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface MoleculeSetter<T> {
|
|
10
|
+
(next: T): void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface MoleculeFunction<T> {
|
|
14
|
+
(get: MoleculeGetter<T>, set: MoleculeSetter<T>): void | (() => void);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function molecule<T>(initialValue: T, fn: MoleculeFunction<T>) {}
|
|
18
|
+
|
|
19
|
+
const value = molecule(5, (get, set) => {
|
|
20
|
+
// get() returns the current value stored in this hadron
|
|
21
|
+
// get(reactive) returns the value of that reactive and tracks it (== reactive.get())
|
|
22
|
+
// set(value) updates the value stored in this hadron
|
|
23
|
+
// This function will not be called unless there is at least one observer.
|
|
24
|
+
|
|
25
|
+
let interval = setInterval(() => {
|
|
26
|
+
set(get() + 1);
|
|
27
|
+
}, 1000);
|
|
28
|
+
|
|
29
|
+
// Can return a cleanup function to run between invocations.
|
|
30
|
+
// Also called when the last observer stops observing.
|
|
31
|
+
return () => {
|
|
32
|
+
clearInterval(interval);
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
```
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# README
|
|
2
|
+
|
|
3
|
+
```jsx
|
|
4
|
+
import { mount, atom, html } from "@manyducks.co/atomic";
|
|
5
|
+
|
|
6
|
+
function Home() {
|
|
7
|
+
return html` <h1>This is the home page!</h1> `;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// mount to DOM element
|
|
11
|
+
mount(Home, document.body);
|
|
12
|
+
|
|
13
|
+
// render to string
|
|
14
|
+
const string = await render(Home, "/the/path/here");
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
> This note will eventually become the new README. Here I'm laying out my ideal framework API.
|
|
20
|
+
|
|
21
|
+
A basic component.
|
|
22
|
+
|
|
23
|
+
```jsx
|
|
24
|
+
import { mount, state, derive, batch } from "@manyducks.co/dolla";
|
|
25
|
+
|
|
26
|
+
function ExampleView(props, ctx) {
|
|
27
|
+
// Signals: state, derive, effect and batch
|
|
28
|
+
|
|
29
|
+
const count = state(5);
|
|
30
|
+
|
|
31
|
+
const doubled = derive(() => count.value * 2);
|
|
32
|
+
|
|
33
|
+
batch(() => {
|
|
34
|
+
// Perform multiple updates in one go and commit at the end.
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// If effect is called in the body of a view function it will be cleaned up automatically with the view.
|
|
38
|
+
ctx.effect(() => {
|
|
39
|
+
console.log(nested.value);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// Emit and listen for context events.
|
|
43
|
+
ctx.on("event", (e, ...args) => {
|
|
44
|
+
e.cancel();
|
|
45
|
+
});
|
|
46
|
+
ctx.emit("event", ...args);
|
|
47
|
+
|
|
48
|
+
// Get and set context values.
|
|
49
|
+
ctx.set("context value", 5);
|
|
50
|
+
ctx.get("context value");
|
|
51
|
+
|
|
52
|
+
// Provide and use a store.
|
|
53
|
+
const store = ctx.provide(someStore); // provide creates a new instance attached to this view and returns it.
|
|
54
|
+
const store = ctx.use(someStore);
|
|
55
|
+
|
|
56
|
+
return <p>{count}</p>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
mount(ExampleView, document.body);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
<details open>
|
|
63
|
+
<summary>
|
|
64
|
+
<h2>Signals API</h2>
|
|
65
|
+
</summary>
|
|
66
|
+
|
|
67
|
+
The signals API. Dolla's signals use explicit tracking, meaning any function where signal values are tracked take an array of the signals you want to track. This way you know exactly what depends on what at a glance without any kind of hidden tracking logic behind the scenes. You are free to `.get()` the value of a signal without worrying about untracking it first.
|
|
68
|
+
|
|
69
|
+
```jsx
|
|
70
|
+
import { createState } from "@manyducks.co/dolla";
|
|
71
|
+
|
|
72
|
+
const [$count, setCount] = createState(256);
|
|
73
|
+
|
|
74
|
+
$count.get(); // 256; returns the current value
|
|
75
|
+
|
|
76
|
+
const stop = $count.watch((value) => {
|
|
77
|
+
// Runs once immediately, then again whenever the value changes.
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
setCount(512); // Update the value of $count. The new value is set and all watchers run synchronously.
|
|
81
|
+
|
|
82
|
+
stop(); // Stop watching for changes.
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
That is the basic signal API. Signals are all about composability. Here are some more advanced ways of working with them:
|
|
86
|
+
|
|
87
|
+
```jsx
|
|
88
|
+
import { createState, toState, valueOf, derive } from "@manyducks.co/dolla";
|
|
89
|
+
|
|
90
|
+
const [$count, setCount] = createState(72);
|
|
91
|
+
|
|
92
|
+
// Returns the value of the signal passed in. If the value is not a signal it is returned as is.
|
|
93
|
+
const count = valueOf($count);
|
|
94
|
+
const bool = valueOf(true);
|
|
95
|
+
|
|
96
|
+
// Creates a signal containing the value passed in. If the value is already a signal it is returned as is.
|
|
97
|
+
const $bool = toState(true);
|
|
98
|
+
const $anotherCount = toState($count);
|
|
99
|
+
|
|
100
|
+
// Derive a new signal from the value of another. Whenever $count changes, $doubled will follow.
|
|
101
|
+
const $doubled = derive([$count], (count) => count * 2);
|
|
102
|
+
|
|
103
|
+
// Derive a new signal from the values of several others. When any value in the list changes, $sum will be recomputed.
|
|
104
|
+
const $sum = derive([$count, $doubled], (count, doubled) => count + doubled);
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The API if we call it State instead of Signal to distance from the Signal object in standardization process.
|
|
108
|
+
|
|
109
|
+
```jsx
|
|
110
|
+
import { createState, toState, valueOf, derive } from "@manyducks.co/dolla";
|
|
111
|
+
|
|
112
|
+
const [$count, setCount] = createState(72);
|
|
113
|
+
|
|
114
|
+
// Returns the value of the signal passed in. If the value is not a signal it is returned as is.
|
|
115
|
+
const count = valueOf($count);
|
|
116
|
+
const bool = valueOf(true);
|
|
117
|
+
|
|
118
|
+
// Creates a signal containing the value passed in. If the value is already a signal it is returned as is.
|
|
119
|
+
const $bool = toState(true);
|
|
120
|
+
const $anotherCount = toState($count);
|
|
121
|
+
|
|
122
|
+
// Derive a new signal from the value of another. Whenever $count changes, $doubled will follow.
|
|
123
|
+
const $doubled = derive([$count], (count) => count * 2);
|
|
124
|
+
|
|
125
|
+
// Derive a new signal from the values of several others. When any value in the list changes, $sum will be recomputed.
|
|
126
|
+
const $sum = derive([$count, $doubled], (count, doubled) => count + doubled);
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
States also come in a settable variety, with the setter included on the same object. Sometimes you want to pass around a two-way binding and this is what SettableState is for.
|
|
130
|
+
|
|
131
|
+
```jsx
|
|
132
|
+
import { createSettableState, fromSettable, toSettable } from "@manyducks.co/dolla";
|
|
133
|
+
|
|
134
|
+
// Settable states have their setter included.
|
|
135
|
+
const $$value = createSettableState("Test");
|
|
136
|
+
$$value.set("New Value");
|
|
137
|
+
|
|
138
|
+
// They can also be split into a State and Setter
|
|
139
|
+
const [$value, setValue] = fromSettableState($$value);
|
|
140
|
+
|
|
141
|
+
// And a State and Setter can be combined into a SettableState.
|
|
142
|
+
const $$otherValue = toSettableState($value, setValue);
|
|
143
|
+
|
|
144
|
+
// Or discard the setter and make it read-only using the good old toState function:
|
|
145
|
+
const $value = toState($$value);
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Alternative API
|
|
149
|
+
|
|
150
|
+
```jsx
|
|
151
|
+
import { State } from "@manyducks.co/dolla";
|
|
152
|
+
|
|
153
|
+
const [$count, setCount] = State(72);
|
|
154
|
+
|
|
155
|
+
const count = State.unwrap($count);
|
|
156
|
+
const bool = State.unwrap(true);
|
|
157
|
+
|
|
158
|
+
const $bool = State.wrap(true);
|
|
159
|
+
const $sameCount = State.wrap($count);
|
|
160
|
+
|
|
161
|
+
const $doubled = State.from([$count], (count) => count * 2);
|
|
162
|
+
|
|
163
|
+
const $sum = State.from([$count, $doubled], (count, doubled) => count + doubled);
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Yet another
|
|
167
|
+
|
|
168
|
+
```jsx
|
|
169
|
+
import Dolla from "@manyducks.co/dolla";
|
|
170
|
+
|
|
171
|
+
const [$count, setCount] = Dolla.state(72);
|
|
172
|
+
|
|
173
|
+
const count = Dolla.get($count);
|
|
174
|
+
const bool = Dolla.get(true);
|
|
175
|
+
|
|
176
|
+
const $bool = Dolla.toState(true);
|
|
177
|
+
const $sameCount = Dolla.toState($count);
|
|
178
|
+
|
|
179
|
+
const $doubled = Dolla.computed([$count], (count) => count * 2);
|
|
180
|
+
const $sum = Dolla.computed([$count, $doubled], (count, doubled) => count + doubled);
|
|
181
|
+
|
|
182
|
+
// or
|
|
183
|
+
|
|
184
|
+
import { state, computed, get, toState } from "@manyducks.co/dolla";
|
|
185
|
+
|
|
186
|
+
const [$count, setCount] = state(72);
|
|
187
|
+
|
|
188
|
+
const count = get($count);
|
|
189
|
+
const bool = get(true);
|
|
190
|
+
|
|
191
|
+
const $bool = toState(true);
|
|
192
|
+
const $sameCount = toState($count);
|
|
193
|
+
|
|
194
|
+
const $doubled = computed([$count], (count) => count * 2);
|
|
195
|
+
const $sum = computed([$count, $doubled], (count, doubled) => count + doubled);
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Settable signals:
|
|
199
|
+
|
|
200
|
+
```jsx
|
|
201
|
+
import { createSettableState, createSetter, toSettableSignal, fromSettableSignal } from "@manyducks.co/dolla";
|
|
202
|
+
|
|
203
|
+
// Create a SettableSignal, which is basically a signal and its setter combined into a single object.
|
|
204
|
+
const $$settable = createSettableState("Example");
|
|
205
|
+
|
|
206
|
+
// The basic API is identical...
|
|
207
|
+
$$settable.get();
|
|
208
|
+
const stop = $$settable.watch((value) => {
|
|
209
|
+
// ...
|
|
210
|
+
});
|
|
211
|
+
stop();
|
|
212
|
+
|
|
213
|
+
// ... except for the addition of a setter.
|
|
214
|
+
$$settable.set("Set me directly");
|
|
215
|
+
|
|
216
|
+
// When you already have a signal and a setter, they can be combined into one.
|
|
217
|
+
const $$count = toSettableSignal($count, setCount);
|
|
218
|
+
|
|
219
|
+
// This updates the original $signal value.
|
|
220
|
+
$$count.set(386);
|
|
221
|
+
|
|
222
|
+
// TODO: You can also split a SettableSignal into a signal and its setter.
|
|
223
|
+
const [$readable, setReadable] = fromSettableSignal($$settable);
|
|
224
|
+
|
|
225
|
+
// Create a custom setter. Calling this will cap the value to 100.
|
|
226
|
+
const setCountBounded = createSetter($count, (next, current) => {
|
|
227
|
+
return Math.min(100, next);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
setCountBounded((current) => {
|
|
231
|
+
return current + 1;
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
// Or make a proxy $$doubled -- but would you actually want to proxy things like this?
|
|
235
|
+
const [$count, setCount] = createState(5);
|
|
236
|
+
const $doubled = derive([$count], (count) => count * 2);
|
|
237
|
+
const $$doubled = toSettableSignal(
|
|
238
|
+
$doubled,
|
|
239
|
+
createSetter($doubled, (next, current) => {
|
|
240
|
+
setCount(next * 2);
|
|
241
|
+
}),
|
|
242
|
+
);
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
I'm not really sure we need all of this. On the chopping block:
|
|
246
|
+
|
|
247
|
+
- The entire concept of settable signals
|
|
248
|
+
- `createSettableState`
|
|
249
|
+
- `toSettableSignal`
|
|
250
|
+
- `fromSettableSignal`
|
|
251
|
+
- `createSetter`
|
|
252
|
+
|
|
253
|
+
This makes the entire API just four functions:
|
|
254
|
+
|
|
255
|
+
- `createState`
|
|
256
|
+
- `derive`
|
|
257
|
+
- `toState`
|
|
258
|
+
- `valueOf`
|
|
259
|
+
|
|
260
|
+
</details>
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Router Middleware
|
|
2
|
+
|
|
3
|
+
Allow handling route guards, preloading, etc with per-route middleware. When a route is matched, all middleware from higher layers are run again.
|
|
4
|
+
|
|
5
|
+
```js
|
|
6
|
+
Dolla.router.setup({
|
|
7
|
+
middleware: [/* does it make sense to have global middleware? */]
|
|
8
|
+
routes: [
|
|
9
|
+
{ path: "/login", middleware: [auth] },
|
|
10
|
+
{ path: "/", middleware: [auth], routes: [{ path: "/example", view: ExampleView }] }
|
|
11
|
+
]
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
async function auth(ctx) {
|
|
15
|
+
// This check can be implemented however it needs to be for the app.
|
|
16
|
+
const authed = await isAuthorized();
|
|
17
|
+
|
|
18
|
+
if (ctx.path === "/login") {
|
|
19
|
+
if (authed) {
|
|
20
|
+
ctx.redirect("/");
|
|
21
|
+
}
|
|
22
|
+
} else {
|
|
23
|
+
if (!authed) {
|
|
24
|
+
ctx.redirect("/login");
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
// If no redirect has happened and nothing has been returned then we're clear to proceed.
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// A middleware can also return Markup to stay on the URL but show something different.
|
|
31
|
+
async function randomVisitor(ctx) {
|
|
32
|
+
if (Math.random() > 0.99) {
|
|
33
|
+
return <LuckyVisitorView />
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Or preload async data and set a context variable before navigating.
|
|
38
|
+
async function preload(ctx) {
|
|
39
|
+
const data = await fetchData();
|
|
40
|
+
ctx.set("data", data);
|
|
41
|
+
}
|
|
42
|
+
```
|