@manyducks.co/dolla 2.0.0-alpha.1 → 2.0.0-alpha.10

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.
Files changed (42) hide show
  1. package/README.md +196 -473
  2. package/dist/index.d.ts +10 -33
  3. package/dist/index.js +792 -675
  4. package/dist/index.js.map +1 -1
  5. package/dist/jsx-dev-runtime.d.ts +1 -1
  6. package/dist/jsx-dev-runtime.js +2 -2
  7. package/dist/jsx-dev-runtime.js.map +1 -1
  8. package/dist/jsx-runtime.d.ts +1 -1
  9. package/dist/jsx-runtime.js +2 -2
  10. package/dist/jsx-runtime.js.map +1 -1
  11. package/dist/markup.d.ts +37 -23
  12. package/dist/modules/dolla.d.ts +41 -17
  13. package/dist/modules/i18n.d.ts +83 -0
  14. package/dist/modules/router.d.ts +9 -10
  15. package/dist/nodes/cond.d.ts +9 -10
  16. package/dist/nodes/html.d.ts +14 -10
  17. package/dist/nodes/observer.d.ts +9 -10
  18. package/dist/nodes/outlet.d.ts +10 -11
  19. package/dist/nodes/portal.d.ts +6 -7
  20. package/dist/nodes/repeat.d.ts +15 -16
  21. package/dist/nodes/text.d.ts +8 -9
  22. package/dist/passthrough-9kwwjgWk.js +1279 -0
  23. package/dist/passthrough-9kwwjgWk.js.map +1 -0
  24. package/dist/state.d.ts +101 -0
  25. package/dist/types.d.ts +12 -12
  26. package/dist/view.d.ts +28 -7
  27. package/dist/views/default-crash-view.d.ts +18 -0
  28. package/dist/views/passthrough.d.ts +5 -0
  29. package/notes/context-vars.md +21 -0
  30. package/notes/readme-scratch.md +222 -0
  31. package/notes/route-middleware.md +42 -0
  32. package/notes/scratch.md +42 -5
  33. package/package.json +8 -12
  34. package/tests/{signals.test.js → state.test.js} +6 -6
  35. package/vite.config.js +1 -0
  36. package/dist/fragment-DHJiX0-a.js +0 -1241
  37. package/dist/fragment-DHJiX0-a.js.map +0 -1
  38. package/dist/modules/language.d.ts +0 -41
  39. package/dist/signals.d.ts +0 -101
  40. package/dist/views/default-crash-page.d.ts +0 -8
  41. package/dist/views/default-view.d.ts +0 -2
  42. package/dist/views/fragment.d.ts +0 -2
@@ -0,0 +1,21 @@
1
+ # Idea: Context Variables
2
+
3
+ In designing how Dolla's version of 'context' works, I've been going through a few different ideas. The simplest seems to be the ability to store _context variables_ that, once set, are accessible on the same context or any child context.
4
+
5
+ ```js
6
+ function SomeView(props, ctx) {
7
+ ctx.set("key", 5);
8
+
9
+ // ... and in a child view do
10
+ ctx.get("key");
11
+ // which returns null if the value isn't present.
12
+ // It's like localStorage for the view tree.
13
+ }
14
+ ```
15
+
16
+ They can be typed, but always with a possibility to return null.
17
+
18
+ ```js
19
+ const value = ctx.get<number>("key");
20
+ // value is number | null to force the programmer to check it.
21
+ ```
@@ -0,0 +1,222 @@
1
+ # README
2
+
3
+ > This note will eventually become the new README. Here I'm laying out my ideal framework API.
4
+
5
+ A basic component.
6
+
7
+ ```jsx
8
+ import Dolla, { createState, derive } from "@manyducks.co/dolla";
9
+
10
+ function ExampleView(props, ctx) {
11
+ const [$count, setCount] = createState(5);
12
+ const $doubled = derive([$count], (n) => n * 2);
13
+
14
+ ctx.watch([$count], (count) => {
15
+ ctx.log("value of count is now %n", count);
16
+ });
17
+
18
+ return <p>{$count}</p>;
19
+ }
20
+
21
+ Dolla.mount(document.body, ExampleView);
22
+ ```
23
+
24
+ <details open>
25
+ <summary>
26
+ <h2>Signals API</h2>
27
+ </summary>
28
+
29
+ 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.
30
+
31
+ ```jsx
32
+ import { createState } from "@manyducks.co/dolla";
33
+
34
+ const [$count, setCount] = createState(256);
35
+
36
+ $count.get(); // 256; returns the current value
37
+
38
+ const stop = $count.watch((value) => {
39
+ // Runs once immediately, then again whenever the value changes.
40
+ });
41
+
42
+ setCount(512); // Update the value of $count. The new value is set and all watchers run synchronously.
43
+
44
+ stop(); // Stop watching for changes.
45
+ ```
46
+
47
+ That is the basic signal API. Signals are all about composability. Here are some more advanced ways of working with them:
48
+
49
+ ```jsx
50
+ import { createState, toState, valueOf, derive } from "@manyducks.co/dolla";
51
+
52
+ const [$count, setCount] = createState(72);
53
+
54
+ // Returns the value of the signal passed in. If the value is not a signal it is returned as is.
55
+ const count = valueOf($count);
56
+ const bool = valueOf(true);
57
+
58
+ // Creates a signal containing the value passed in. If the value is already a signal it is returned as is.
59
+ const $bool = toState(true);
60
+ const $anotherCount = toState($count);
61
+
62
+ // Derive a new signal from the value of another. Whenever $count changes, $doubled will follow.
63
+ const $doubled = derive([$count], (count) => count * 2);
64
+
65
+ // Derive a new signal from the values of several others. When any value in the list changes, $sum will be recomputed.
66
+ const $sum = derive([$count, $doubled], (count, doubled) => count + doubled);
67
+ ```
68
+
69
+ The API if we call it State instead of Signal to distance from the Signal object in standardization process.
70
+
71
+ ```jsx
72
+ import { createState, toState, valueOf, derive } from "@manyducks.co/dolla";
73
+
74
+ const [$count, setCount] = createState(72);
75
+
76
+ // Returns the value of the signal passed in. If the value is not a signal it is returned as is.
77
+ const count = valueOf($count);
78
+ const bool = valueOf(true);
79
+
80
+ // Creates a signal containing the value passed in. If the value is already a signal it is returned as is.
81
+ const $bool = toState(true);
82
+ const $anotherCount = toState($count);
83
+
84
+ // Derive a new signal from the value of another. Whenever $count changes, $doubled will follow.
85
+ const $doubled = derive([$count], (count) => count * 2);
86
+
87
+ // Derive a new signal from the values of several others. When any value in the list changes, $sum will be recomputed.
88
+ const $sum = derive([$count, $doubled], (count, doubled) => count + doubled);
89
+ ```
90
+
91
+ 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.
92
+
93
+ ```jsx
94
+ import { createSettableState, fromSettable, toSettable } from "@manyducks.co/dolla";
95
+
96
+ // Settable states have their setter included.
97
+ const $$value = createSettableState("Test");
98
+ $$value.set("New Value");
99
+
100
+ // They can also be split into a State and Setter
101
+ const [$value, setValue] = fromSettableState($$value);
102
+
103
+ // And a State and Setter can be combined into a SettableState.
104
+ const $$otherValue = toSettableState($value, setValue);
105
+
106
+ // Or discard the setter and make it read-only using the good old toState function:
107
+ const $value = toState($$value);
108
+ ```
109
+
110
+ Alternative API
111
+
112
+ ```jsx
113
+ import { State } from "@manyducks.co/dolla";
114
+
115
+ const [$count, setCount] = State(72);
116
+
117
+ const count = State.unwrap($count);
118
+ const bool = State.unwrap(true);
119
+
120
+ const $bool = State.wrap(true);
121
+ const $sameCount = State.wrap($count);
122
+
123
+ const $doubled = State.from([$count], (count) => count * 2);
124
+
125
+ const $sum = State.from([$count, $doubled], (count, doubled) => count + doubled);
126
+ ```
127
+
128
+ Yet another
129
+
130
+ ```jsx
131
+ import Dolla from "@manyducks.co/dolla";
132
+
133
+ const [$count, setCount] = Dolla.state(72);
134
+
135
+ const count = Dolla.get($count);
136
+ const bool = Dolla.get(true);
137
+
138
+ const $bool = Dolla.toState(true);
139
+ const $sameCount = Dolla.toState($count);
140
+
141
+ const $doubled = Dolla.computed([$count], (count) => count * 2);
142
+ const $sum = Dolla.computed([$count, $doubled], (count, doubled) => count + doubled);
143
+
144
+ // or
145
+
146
+ import { state, computed, get, toState } from "@manyducks.co/dolla";
147
+
148
+ const [$count, setCount] = state(72);
149
+
150
+ const count = get($count);
151
+ const bool = get(true);
152
+
153
+ const $bool = toState(true);
154
+ const $sameCount = toState($count);
155
+
156
+ const $doubled = computed([$count], (count) => count * 2);
157
+ const $sum = computed([$count, $doubled], (count, doubled) => count + doubled);
158
+ ```
159
+
160
+ Settable signals:
161
+
162
+ ```jsx
163
+ import { createSettableState, createSetter, toSettableSignal, fromSettableSignal } from "@manyducks.co/dolla";
164
+
165
+ // Create a SettableSignal, which is basically a signal and its setter combined into a single object.
166
+ const $$settable = createSettableState("Example");
167
+
168
+ // The basic API is identical...
169
+ $$settable.get();
170
+ const stop = $$settable.watch((value) => {
171
+ // ...
172
+ });
173
+ stop();
174
+
175
+ // ... except for the addition of a setter.
176
+ $$settable.set("Set me directly");
177
+
178
+ // When you already have a signal and a setter, they can be combined into one.
179
+ const $$count = toSettableSignal($count, setCount);
180
+
181
+ // This updates the original $signal value.
182
+ $$count.set(386);
183
+
184
+ // TODO: You can also split a SettableSignal into a signal and its setter.
185
+ const [$readable, setReadable] = fromSettableSignal($$settable);
186
+
187
+ // Create a custom setter. Calling this will cap the value to 100.
188
+ const setCountBounded = createSetter($count, (next, current) => {
189
+ return Math.min(100, next);
190
+ });
191
+
192
+ setCountBounded((current) => {
193
+ return current + 1;
194
+ });
195
+
196
+ // Or make a proxy $$doubled -- but would you actually want to proxy things like this?
197
+ const [$count, setCount] = createState(5);
198
+ const $doubled = derive([$count], (count) => count * 2);
199
+ const $$doubled = toSettableSignal(
200
+ $doubled,
201
+ createSetter($doubled, (next, current) => {
202
+ setCount(next * 2);
203
+ }),
204
+ );
205
+ ```
206
+
207
+ I'm not really sure we need all of this. On the chopping block:
208
+
209
+ - The entire concept of settable signals
210
+ - `createSettableState`
211
+ - `toSettableSignal`
212
+ - `fromSettableSignal`
213
+ - `createSetter`
214
+
215
+ This makes the entire API just four functions:
216
+
217
+ - `createState`
218
+ - `derive`
219
+ - `toState`
220
+ - `valueOf`
221
+
222
+ </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
+ ```
package/notes/scratch.md CHANGED
@@ -1,5 +1,42 @@
1
1
  # Scratch Note
2
2
 
3
+ Bring the $ back and the name full circle.
4
+
5
+ ```js
6
+ import { $, $$ } from "@manyducks.co/dolla";
7
+
8
+ // Shorthand dolla sign
9
+
10
+ // An initial value (with optional options object) creates a state.
11
+ const [$count, setCount] = $(0);
12
+ // = createState(0)
13
+
14
+ // An array and a function derives a state.
15
+ const $doubled = $([$count], (count) => count * 2);
16
+ // = derive([$count], (count) => count * 2);
17
+
18
+ // A state returns the same state.
19
+ const $sameCount = $.of($count);
20
+ const $wrapped = $.of({ message: "This is a state with no setter." });
21
+ // = toState($count)
22
+
23
+ // Get value from a state. Values that are not states are returned directly.
24
+ const count = $.value($count);
25
+
26
+ // An initial value creates a SettableState
27
+ const $$count = $$(5);
28
+ // = createSettableState(5);
29
+
30
+ // Merge state and setter into a SettableState
31
+ const $$count = $$($count, setCount);
32
+ // = toSettableState($count, setCount);
33
+
34
+ // Split a SettableState into a state and setter
35
+ const [$count, setCount] = $($$count);
36
+ ```
37
+
38
+ ---
39
+
3
40
  What if Dolla was just a global object that you don't instantiate. I have never personally run into a use case for having more than one app on a page at once. In all my projects, the page and the app are synonymous.
4
41
 
5
42
  Doing this would make it possible to access things inside the Dolla app from _outside_ code such as Quill blots. Effectively all code that has access to your Dolla import is _inside_ the app.
@@ -11,8 +48,8 @@ Doing this would make it possible to access things inside the Dolla app from _ou
11
48
  import Dolla from "@manyducks.co/dolla";
12
49
 
13
50
  // Languages: add translation, set language and get localized string as a signal
14
- Dolla.language.setup({
15
- initialLanguage: Dolla.language.detect({ fallback: "ja" }), // Detect user's language and fall back to passed value
51
+ Dolla.i18n.setup({
52
+ initialLanguage: Dolla.i18n.detect({ fallback: "ja" }), // Detect user's language and fall back to passed value
16
53
  languages: [
17
54
  { name: "ja", path: "/static/locales/ja.json" },
18
55
  {
@@ -26,8 +63,8 @@ Dolla.language.setup({
26
63
  ]
27
64
  });
28
65
 
29
- Dolla.language.$current
30
- Dolla.language.t$()
66
+ Dolla.i18n.$locale
67
+ Dolla.i18n.t$()
31
68
 
32
69
  // A single setup call to keep things contained (must happen before mount)
33
70
  Dolla.router.setup({
@@ -93,7 +130,7 @@ function SomeView (props: SomeViewProps, ctx: Dolla.ViewContext) {
93
130
  const debug = Dolla.createLogger("SomeView");
94
131
 
95
132
  // returns a signal and a setter function
96
- const [$someValue, setSomeValue] = Dolla.createSignal(4);
133
+ const [$someValue, setSomeValue] = Dolla.createState(4);
97
134
 
98
135
  // Router is now a part of the Dolla object
99
136
  Dolla.router.$path;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@manyducks.co/dolla",
3
- "version": "2.0.0-alpha.1",
3
+ "version": "2.0.0-alpha.10",
4
4
  "description": "Front-end components, routing and state management.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -36,20 +36,16 @@
36
36
  "types": "./jsx-dev-runtime.d.ts"
37
37
  }
38
38
  },
39
- "dependencies": {
40
- "fetch-ponyfill": "^7.1.0",
41
- "history": "^5.3.0",
42
- "nanoid": "^5.0.4",
43
- "simple-color-hash": "^1.0.2",
44
- "vite": "^6.0.7"
45
- },
46
39
  "devDependencies": {
47
- "@types/node": "^18.17.6",
40
+ "@types/node": "^22.10.6",
48
41
  "csstype": "^3.1.3",
49
42
  "esbuild": "^0.24.2",
43
+ "history": "^5.3.0",
50
44
  "htm": "^3.1.1",
51
- "prettier": "^3.2.4",
52
- "typescript": "^5.3.3",
53
- "zod": "^3.22.4"
45
+ "nanoid": "^5.0.9",
46
+ "prettier": "^3.4.2",
47
+ "simple-color-hash": "^1.0.2",
48
+ "typescript": "^5.7.3",
49
+ "vite": "^6.0.7"
54
50
  }
55
51
  }
@@ -1,10 +1,10 @@
1
1
  import test from "node:test";
2
2
  import assert from "node:assert";
3
3
 
4
- import { signal, derive } from "../lib/index.js";
4
+ import { createState, derive } from "../dist/index.js";
5
5
 
6
6
  test("signal", (t) => {
7
- const [$count, setCount] = signal(5);
7
+ const [$count, setCount] = createState(5);
8
8
 
9
9
  const defaultWatcher = t.mock.fn();
10
10
  const stopDefault = $count.watch(defaultWatcher);
@@ -42,8 +42,8 @@ test("signal", (t) => {
42
42
  });
43
43
 
44
44
  test("derive", (t) => {
45
- const [$one, setOne] = signal(5);
46
- const [$two, setTwo] = signal(20);
45
+ const [$one, setOne] = createState(5);
46
+ const [$two, setTwo] = createState(20);
47
47
 
48
48
  const deriveSum = t.mock.fn((one, two) => one + two);
49
49
  const deriveProduct = t.mock.fn((one, two) => one * two);
@@ -101,9 +101,9 @@ test("derive", (t) => {
101
101
  });
102
102
 
103
103
  test("derive nested signals", (t) => {
104
- const [$value, setValue] = signal(5);
104
+ const [$value, setValue] = createState(5);
105
105
 
106
- const [$object, setObject] = signal({
106
+ const [$object, setObject] = createState({
107
107
  href: derive([$value], (value) => `/projects/${value}/test`),
108
108
  });
109
109
 
package/vite.config.js CHANGED
@@ -6,6 +6,7 @@ export default defineConfig({
6
6
  // minify: "terser",
7
7
  // minify: false,
8
8
  sourcemap: true,
9
+
9
10
  lib: {
10
11
  entry: {
11
12
  index: resolve(__dirname, "src/index.ts"),