@manyducks.co/dolla 2.0.0-alpha.4 → 2.0.0-alpha.40

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 (75) hide show
  1. package/README.md +31 -964
  2. package/dist/core/context.d.ts +53 -0
  3. package/dist/{modules → core}/dolla.d.ts +43 -26
  4. package/dist/core/markup.d.ts +90 -0
  5. package/dist/core/nodes/dom.d.ts +13 -0
  6. package/dist/core/nodes/dynamic.d.ts +28 -0
  7. package/dist/core/nodes/html.d.ts +33 -0
  8. package/dist/core/nodes/list.d.ts +28 -0
  9. package/dist/core/nodes/outlet.d.ts +19 -0
  10. package/dist/core/nodes/portal.d.ts +22 -0
  11. package/dist/core/nodes/view.d.ts +78 -0
  12. package/dist/core/ref.d.ts +28 -0
  13. package/dist/core/signals.d.ts +125 -0
  14. package/dist/core/store.d.ts +52 -0
  15. package/dist/core/symbols.d.ts +4 -0
  16. package/dist/{views → core/views}/passthrough.d.ts +1 -1
  17. package/dist/{modules/http.d.ts → http/index.d.ts} +3 -5
  18. package/dist/index.d.ts +14 -11
  19. package/dist/index.js +986 -1216
  20. package/dist/index.js.map +1 -1
  21. package/dist/jsx-dev-runtime.d.ts +2 -2
  22. package/dist/jsx-dev-runtime.js +2 -2
  23. package/dist/jsx-dev-runtime.js.map +1 -1
  24. package/dist/jsx-runtime.d.ts +3 -3
  25. package/dist/jsx-runtime.js +2 -2
  26. package/dist/jsx-runtime.js.map +1 -1
  27. package/dist/markup-DIfh0nwz.js +1442 -0
  28. package/dist/markup-DIfh0nwz.js.map +1 -0
  29. package/dist/{modules/router.d.ts → router/index.d.ts} +37 -48
  30. package/dist/router/router.utils.test.d.ts +1 -0
  31. package/dist/translate/index.d.ts +133 -0
  32. package/dist/typeChecking.d.ts +2 -98
  33. package/dist/typeChecking.test.d.ts +1 -0
  34. package/dist/types.d.ts +12 -14
  35. package/dist/utils.d.ts +18 -3
  36. package/docs/http.md +29 -0
  37. package/docs/i18n.md +38 -0
  38. package/docs/index.md +10 -0
  39. package/docs/router.md +80 -0
  40. package/docs/setup.md +31 -0
  41. package/docs/signals.md +149 -0
  42. package/docs/state.md +141 -0
  43. package/docs/stores.md +62 -0
  44. package/docs/views.md +208 -0
  45. package/index.d.ts +2 -2
  46. package/notes/TODO.md +6 -0
  47. package/notes/atomic.md +146 -0
  48. package/notes/context-routes.md +56 -0
  49. package/notes/context-vars.md +21 -0
  50. package/notes/elimination.md +33 -0
  51. package/notes/readme-scratch.md +244 -0
  52. package/notes/route-middleware.md +42 -0
  53. package/notes/scratch.md +330 -7
  54. package/notes/stores.md +53 -0
  55. package/package.json +14 -10
  56. package/vite.config.js +5 -10
  57. package/build.js +0 -34
  58. package/dist/markup.d.ts +0 -100
  59. package/dist/modules/language.d.ts +0 -41
  60. package/dist/modules/render.d.ts +0 -17
  61. package/dist/nodes/cond.d.ts +0 -26
  62. package/dist/nodes/html.d.ts +0 -31
  63. package/dist/nodes/observer.d.ts +0 -29
  64. package/dist/nodes/outlet.d.ts +0 -22
  65. package/dist/nodes/portal.d.ts +0 -19
  66. package/dist/nodes/repeat.d.ts +0 -34
  67. package/dist/nodes/text.d.ts +0 -19
  68. package/dist/passthrough-BSLd3foL.js +0 -1245
  69. package/dist/passthrough-BSLd3foL.js.map +0 -1
  70. package/dist/signals.d.ts +0 -101
  71. package/dist/view.d.ts +0 -50
  72. package/tests/signals.test.js +0 -135
  73. /package/dist/{routing.test.d.ts → core/signals.test.d.ts} +0 -0
  74. /package/dist/{views → core/views}/default-crash-view.d.ts +0 -0
  75. /package/dist/{routing.d.ts → router/router.utils.d.ts} +0 -0
@@ -0,0 +1,244 @@
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 { mount, state, derive, batch } from "@manyducks.co/dolla";
9
+
10
+ function ExampleView(props, ctx) {
11
+ // Signals: state, derive, effect and batch
12
+
13
+ const count = state(5);
14
+
15
+ const doubled = derive(() => count.value * 2);
16
+
17
+ batch(() => {
18
+ // Perform multiple updates in one go and commit at the end.
19
+ });
20
+
21
+ // If effect is called in the body of a view function it will be cleaned up automatically with the view.
22
+ ctx.effect(() => {
23
+ console.log(nested.value);
24
+ });
25
+
26
+ // Emit and listen for context events.
27
+ ctx.on("event", (e, ...args) => {
28
+ e.cancel();
29
+ });
30
+ ctx.emit("event", ...args);
31
+
32
+ // Get and set context values.
33
+ ctx.set("context value", 5);
34
+ ctx.get("context value");
35
+
36
+ // Provide and use a store.
37
+ const store = ctx.provide(someStore); // provide creates a new instance attached to this view and returns it.
38
+ const store = ctx.use(someStore);
39
+
40
+ return <p>{count}</p>;
41
+ }
42
+
43
+ mount(ExampleView, document.body);
44
+ ```
45
+
46
+ <details open>
47
+ <summary>
48
+ <h2>Signals API</h2>
49
+ </summary>
50
+
51
+ 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.
52
+
53
+ ```jsx
54
+ import { createState } from "@manyducks.co/dolla";
55
+
56
+ const [$count, setCount] = createState(256);
57
+
58
+ $count.get(); // 256; returns the current value
59
+
60
+ const stop = $count.watch((value) => {
61
+ // Runs once immediately, then again whenever the value changes.
62
+ });
63
+
64
+ setCount(512); // Update the value of $count. The new value is set and all watchers run synchronously.
65
+
66
+ stop(); // Stop watching for changes.
67
+ ```
68
+
69
+ That is the basic signal API. Signals are all about composability. Here are some more advanced ways of working with them:
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
+ The API if we call it State instead of Signal to distance from the Signal object in standardization process.
92
+
93
+ ```jsx
94
+ import { createState, toState, valueOf, derive } from "@manyducks.co/dolla";
95
+
96
+ const [$count, setCount] = createState(72);
97
+
98
+ // Returns the value of the signal passed in. If the value is not a signal it is returned as is.
99
+ const count = valueOf($count);
100
+ const bool = valueOf(true);
101
+
102
+ // Creates a signal containing the value passed in. If the value is already a signal it is returned as is.
103
+ const $bool = toState(true);
104
+ const $anotherCount = toState($count);
105
+
106
+ // Derive a new signal from the value of another. Whenever $count changes, $doubled will follow.
107
+ const $doubled = derive([$count], (count) => count * 2);
108
+
109
+ // Derive a new signal from the values of several others. When any value in the list changes, $sum will be recomputed.
110
+ const $sum = derive([$count, $doubled], (count, doubled) => count + doubled);
111
+ ```
112
+
113
+ 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.
114
+
115
+ ```jsx
116
+ import { createSettableState, fromSettable, toSettable } from "@manyducks.co/dolla";
117
+
118
+ // Settable states have their setter included.
119
+ const $$value = createSettableState("Test");
120
+ $$value.set("New Value");
121
+
122
+ // They can also be split into a State and Setter
123
+ const [$value, setValue] = fromSettableState($$value);
124
+
125
+ // And a State and Setter can be combined into a SettableState.
126
+ const $$otherValue = toSettableState($value, setValue);
127
+
128
+ // Or discard the setter and make it read-only using the good old toState function:
129
+ const $value = toState($$value);
130
+ ```
131
+
132
+ Alternative API
133
+
134
+ ```jsx
135
+ import { State } from "@manyducks.co/dolla";
136
+
137
+ const [$count, setCount] = State(72);
138
+
139
+ const count = State.unwrap($count);
140
+ const bool = State.unwrap(true);
141
+
142
+ const $bool = State.wrap(true);
143
+ const $sameCount = State.wrap($count);
144
+
145
+ const $doubled = State.from([$count], (count) => count * 2);
146
+
147
+ const $sum = State.from([$count, $doubled], (count, doubled) => count + doubled);
148
+ ```
149
+
150
+ Yet another
151
+
152
+ ```jsx
153
+ import Dolla from "@manyducks.co/dolla";
154
+
155
+ const [$count, setCount] = Dolla.state(72);
156
+
157
+ const count = Dolla.get($count);
158
+ const bool = Dolla.get(true);
159
+
160
+ const $bool = Dolla.toState(true);
161
+ const $sameCount = Dolla.toState($count);
162
+
163
+ const $doubled = Dolla.computed([$count], (count) => count * 2);
164
+ const $sum = Dolla.computed([$count, $doubled], (count, doubled) => count + doubled);
165
+
166
+ // or
167
+
168
+ import { state, computed, get, toState } from "@manyducks.co/dolla";
169
+
170
+ const [$count, setCount] = state(72);
171
+
172
+ const count = get($count);
173
+ const bool = get(true);
174
+
175
+ const $bool = toState(true);
176
+ const $sameCount = toState($count);
177
+
178
+ const $doubled = computed([$count], (count) => count * 2);
179
+ const $sum = computed([$count, $doubled], (count, doubled) => count + doubled);
180
+ ```
181
+
182
+ Settable signals:
183
+
184
+ ```jsx
185
+ import { createSettableState, createSetter, toSettableSignal, fromSettableSignal } from "@manyducks.co/dolla";
186
+
187
+ // Create a SettableSignal, which is basically a signal and its setter combined into a single object.
188
+ const $$settable = createSettableState("Example");
189
+
190
+ // The basic API is identical...
191
+ $$settable.get();
192
+ const stop = $$settable.watch((value) => {
193
+ // ...
194
+ });
195
+ stop();
196
+
197
+ // ... except for the addition of a setter.
198
+ $$settable.set("Set me directly");
199
+
200
+ // When you already have a signal and a setter, they can be combined into one.
201
+ const $$count = toSettableSignal($count, setCount);
202
+
203
+ // This updates the original $signal value.
204
+ $$count.set(386);
205
+
206
+ // TODO: You can also split a SettableSignal into a signal and its setter.
207
+ const [$readable, setReadable] = fromSettableSignal($$settable);
208
+
209
+ // Create a custom setter. Calling this will cap the value to 100.
210
+ const setCountBounded = createSetter($count, (next, current) => {
211
+ return Math.min(100, next);
212
+ });
213
+
214
+ setCountBounded((current) => {
215
+ return current + 1;
216
+ });
217
+
218
+ // Or make a proxy $$doubled -- but would you actually want to proxy things like this?
219
+ const [$count, setCount] = createState(5);
220
+ const $doubled = derive([$count], (count) => count * 2);
221
+ const $$doubled = toSettableSignal(
222
+ $doubled,
223
+ createSetter($doubled, (next, current) => {
224
+ setCount(next * 2);
225
+ }),
226
+ );
227
+ ```
228
+
229
+ I'm not really sure we need all of this. On the chopping block:
230
+
231
+ - The entire concept of settable signals
232
+ - `createSettableState`
233
+ - `toSettableSignal`
234
+ - `fromSettableSignal`
235
+ - `createSetter`
236
+
237
+ This makes the entire API just four functions:
238
+
239
+ - `createState`
240
+ - `derive`
241
+ - `toState`
242
+ - `valueOf`
243
+
244
+ </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,328 @@
1
1
  # Scratch Note
2
2
 
3
+ Idea: Monomorphic app context. Replaces StoreContext, ViewContext, etc.
4
+
5
+ Routes are baked into the app once again, but
6
+
7
+ ```jsx
8
+ import { createRoot } from "@manyducks.co/dolla";
9
+ import { example } from "./stores/example.js";
10
+
11
+ const root = createRoot();
12
+
13
+ root.use(example());
14
+
15
+ async function auth(_, state, redirect) {
16
+ // route context
17
+ // Routes run through each callback until one resolves to a renderable value.
18
+ // If redirect is called, the route is re-matched and no further callbacks are run for this route.
19
+
20
+ if (state.auth == null) {
21
+ redirect("/login");
22
+ }
23
+ }
24
+
25
+ root.route("/users/*", auth, (C) => {
26
+ C.route("/{#id}/*", (C) => {
27
+ C.route("/", (C) => <UserDetailRoute userId={C.params.id} />);
28
+ C.route("*", "./");
29
+ });
30
+ });
31
+
32
+ root.route("/users/*", auth, (route) => {
33
+ route("/{#id}/*", (route) => {
34
+ // TODO: It's possible to reference the wrong 'route'
35
+ // Track active context and throw error if the one you call belongs to the wrong context?
36
+ route("/", (_, state) => <UserDetailView userId={state.params.id} />);
37
+ route("*", "./");
38
+ });
39
+ });
40
+
41
+ function ExampleView(props, ctx) {
42
+ // ctx.routes returns a special type of outlet that renders children based on
43
+ // the route segments that come after the ones at this ctx.
44
+
45
+ // The weakness of this idea is that routes can't be validated without initializing views.
46
+ return (
47
+ <div>
48
+ <Suspense fallback={<span>Loading...</span>}>
49
+ {ctx.routes((route) => {
50
+ route("/subroute", () => <OtherView />);
51
+
52
+ // Routes can be async.
53
+ route("/other", () => import("some-module"));
54
+ })}
55
+ </Suspense>
56
+ </div>
57
+ );
58
+
59
+ // Also Suspense. This can be simply implemented with events.
60
+ ctx.emit("suspense:begin", uniqueId);
61
+ // Then when done:
62
+ ctx.emit("suspense:end", uniqueId);
63
+
64
+ // The nearest Suspense view will track ids which are in suspense and show fallback content in the meantime.
65
+ }
66
+
67
+ function Suspense(props, ctx) {
68
+ const [$tracked, setTracked] = createState({});
69
+
70
+ ctx.on("suspense:begin", (e) => {
71
+ setTracked((tracked) => {
72
+ return {
73
+ ...tracked,
74
+ [e.detail]: new Date(),
75
+ };
76
+ });
77
+ });
78
+
79
+ ctx.on("suspense:end", (e) => {
80
+ setTracked((tracked) => {
81
+ const updated = Object.assign({}, tracked);
82
+ delete updated[e.detail];
83
+ return updated;
84
+ });
85
+ });
86
+
87
+ // TODO: Hide suspended view without unmounting it. This might take special logic.
88
+ }
89
+
90
+ // Can also pass markup directly if you don't need the context.
91
+ root.route("/", auth, <HomeRoute />);
92
+
93
+ // Static redirect.
94
+ root.route("*", "/");
95
+
96
+ // Programmatic redirect.
97
+ root.route("*", (C) => {
98
+ C.log("hit wildcard");
99
+ C.redirect("/");
100
+ });
101
+
102
+ root.mount(document.body);
103
+
104
+ // generate an HTML string for server side rendering.
105
+ root.toString("/some/path");
106
+ ```
107
+
108
+ ---
109
+
110
+ ```js
111
+ class ClockStore extends Store {
112
+
113
+
114
+ constructor() {
115
+
116
+ }
117
+ }
118
+
119
+ class CounterStore extends Store {
120
+ // Could have better name. This will catch any
121
+ // this.emit('counter:increment') or this.emit('counter:decrement') calls
122
+ // and update the state according to these functions.
123
+ value = new Emittable('counter', 0, {
124
+ increment: state => state + 1,
125
+ decrement: state => state - 1
126
+ });
127
+ }
128
+
129
+ type CounterEvents = {
130
+ increment: [amount: number];
131
+ decrement: [amount: number];
132
+ }
133
+
134
+
135
+
136
+ ```
137
+
138
+ ---
139
+
140
+ Bring the $ back and the name full circle.
141
+
142
+ ```js
143
+ import { $, $$ } from "@manyducks.co/dolla";
144
+
145
+ // Shorthand dolla sign
146
+
147
+ // An initial value (with optional options object) creates a state.
148
+ const [$count, setCount] = $(0);
149
+ // = createState(0)
150
+
151
+ // An array and a function derives a state.
152
+ const $doubled = $.map([$count], (count) => count * 2);
153
+ // = derive([$count], (count) => count * 2);
154
+
155
+ // A state returns the same state.
156
+ const $sameCount = $.from($count);
157
+ const $wrapped = $.from({ message: "This is a state with no setter." });
158
+ // = toState($count)
159
+
160
+ // Get value from a state. Values that are not states are returned directly.
161
+ const count = $.get($count);
162
+ ```
163
+
164
+ What about other operators like RxJS?
165
+
166
+ ```js
167
+ // These would be functionally equivalent.
168
+ const $doubled = $count.pipe($.map((count) => count * 2));
169
+ const $doubled = $.map([$count], (count) => count * 2);
170
+
171
+ // Chainable. Get doubled value, but only update if it's between 10 and 100.
172
+ const $boundedDouble = $count.pipe(
173
+ // Transforms the value
174
+ $.map((count) => count * 2),
175
+
176
+ // Receives the value when it changes without affecting the output.
177
+ // Only receives values while this state is actively being watched.
178
+ $.tap((count) => console.log(`doubled value is ${count}`))
179
+
180
+ // Value only changes if it's within the range.
181
+ $.filter((count) => count >= 10 && count <= 100),
182
+ );
183
+
184
+ // Could have a top level pipe operator
185
+ const $boundedDouble = $.pipe(
186
+ [$count],
187
+ $.map((count) => count * 2),
188
+ $.tap((count) => console.log(`doubled value is ${count}`))
189
+ $.filter((count) => count >= 10 && count <= 100),
190
+ );
191
+
192
+ // Could also be chainable
193
+ const $boundedDouble = $count
194
+ .map((count) => count * 2)
195
+ .tap((count) => console.log(`doubled value is ${count}`))
196
+ .filter((count) => count >= 10 && count <= 100);
197
+
198
+ // I kind of like this more than the current derive. It's cleaner.
199
+ $count.map(c => c * 2);
200
+ $count.merge([$other], (c, o) => c * o);
201
+
202
+ // Another way to merge multiple.
203
+ $.merge([$count, $other], (c, o) => c * o);
204
+
205
+ // What if you want to add something in the middle?
206
+
207
+ const $example = $count
208
+ .map((count) => count * 2)
209
+ .tap((count) => console.log(`doubled value is ${count}`))
210
+ .merge([$other1, $other2], (count, other1, other2) => /* ... */)
211
+ .filter((value) => value >= 10 && value <= 100);
212
+
213
+ // Is this a good pattern?
214
+ $count
215
+ .merge([$other], (count, other) => count * other)
216
+ .merge([$another], (merged, another) => merged * another);
217
+ // I think it gets a little weird to follow.
218
+
219
+ // equivalent to
220
+ derive(
221
+ [
222
+ derive([$count, $other], (count, other) => count * other),
223
+ $another
224
+ ],
225
+ (merged, another) => merged * another)
226
+ // Is this a pattern? Yeah, I guess I do that. Just never in line like that.
227
+
228
+ // Do we want to handle errors?
229
+ // I feel like errors usually happen in watchers though.
230
+ $boundedDouble.watch((value) => {
231
+ // Received a value.
232
+ }, (error) => {
233
+ // Something threw an error.
234
+ });
235
+ // Or like this.
236
+ $boundedDouble.watch({
237
+ change: (value) => {
238
+ // Received a value.
239
+ // This code is most likely to throw an error.
240
+ // Should errors here be passed to the error callback?
241
+ // What is the point if you can just try/catch?
242
+
243
+ // Although if you don't then Dolla could use this to catch
244
+ // and trace errors better than it does now.
245
+ },
246
+ error: (error) => {
247
+ // Something threw an error.
248
+ }
249
+ });
250
+
251
+ // Filter derives a new state where the value only updates if the function returns truthy.
252
+ const $evens = $count.pipe($.filter((count) => count % 1 === 0));
253
+ // This is equivalent to
254
+ const $events = $.map([$count], (count) => count, { equals: (a, b) => a % 1 === 0 });
255
+
256
+ function filter(...args) {
257
+ if (isArray(args[0]) && isFunction(args[1])) {
258
+ // Standalone signature. Returns a new derived state.
259
+ } else if (args.length === 1 && isFunction(args[1])) {
260
+ // Curried signature. Returns a function that takes an array of states
261
+ // and returns one with args[1] as the equality check.
262
+ }
263
+ }
264
+ ```
265
+
266
+ And you can write your own operators that implement these two signatures.
267
+
268
+ ```js
269
+ // Here's one I might want to include.
270
+ // Use this to prevent ever getting a null value.
271
+ compare((next, previous) => next ?? previous ?? "default");
272
+
273
+ function compare(...args) {}
274
+ ```
275
+
276
+ ---
277
+
278
+ I've been looking into other libraries that don't make you track your dependencies specifically. I think this is weird and unhinged to be honest. Calling functions with side effects that magically re-run things when the value changes is a truly weird and unexpected lifecycle. At least if you're explicitly tracking dependencies you know exactly what depends on what at a glance. Getting the computer to figure it out for you doesn't seem smart.
279
+
280
+ ```js
281
+ import { $ } from "@manyducks.co/dolla";
282
+
283
+ const [count, setCount] = $(0);
284
+
285
+ const doubled = $.computed(() => count() * 2);
286
+
287
+ $.effect(() => {
288
+ console.log(doubled());
289
+ });
290
+
291
+ $.batch(() => {
292
+ // Set multiple things but defer updates to after this function returns.
293
+ });
294
+
295
+ // Helpers on $; can plug into template as is.
296
+ $.if(
297
+ $.computed(() => count() > 5),
298
+ <span>Greater than 5!</span>,
299
+ <span>Not greater than 5...</span>,
300
+ );
301
+
302
+ const switched = $.switch(count, [[1, "one"], [2, "two"], [3, "three"]], "more...");
303
+
304
+ $.repeat()
305
+
306
+ // TODO: How feasible is this?
307
+ <Repeat each={}>
308
+ {(item, index) => {
309
+
310
+ }}
311
+ </Repeat>
312
+
313
+ <Show when={condition}>
314
+ Condition is true.
315
+ </Show>
316
+
317
+ // Get
318
+ count();
319
+
320
+ // Set
321
+ count(52);
322
+ ```
323
+
324
+ ---
325
+
3
326
  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
327
 
5
328
  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 +334,8 @@ Doing this would make it possible to access things inside the Dolla app from _ou
11
334
  import Dolla from "@manyducks.co/dolla";
12
335
 
13
336
  // 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
337
+ Dolla.i18n.setup({
338
+ initialLanguage: Dolla.i18n.detect({ fallback: "ja" }), // Detect user's language and fall back to passed value
16
339
  languages: [
17
340
  { name: "ja", path: "/static/locales/ja.json" },
18
341
  {
@@ -26,8 +349,8 @@ Dolla.language.setup({
26
349
  ]
27
350
  });
28
351
 
29
- Dolla.language.$current
30
- Dolla.language.t$()
352
+ Dolla.i18n.$locale
353
+ Dolla.i18n.t$()
31
354
 
32
355
  // A single setup call to keep things contained (must happen before mount)
33
356
  Dolla.router.setup({
@@ -75,10 +398,10 @@ debug.log("HELLO");
75
398
  debug.warn("THIS IS A SCOPED LOGGER");
76
399
 
77
400
  // Efficiently and safely read and mutate the DOM using Dolla's render batching
78
- Dolla.render.read(() => {
401
+ Dolla.batch.read(() => {
79
402
  // Reference DOM nodes
80
403
  });
81
- Dolla.render.update(() => {
404
+ Dolla.batch.write(() => {
82
405
  // Mutate the DOM as part of Dolla's next batch
83
406
  }, "some-key");
84
407
 
@@ -93,7 +416,7 @@ function SomeView (props: SomeViewProps, ctx: Dolla.ViewContext) {
93
416
  const debug = Dolla.createLogger("SomeView");
94
417
 
95
418
  // returns a signal and a setter function
96
- const [$someValue, setSomeValue] = Dolla.createSignal(4);
419
+ const [$someValue, setSomeValue] = Dolla.createState(4);
97
420
 
98
421
  // Router is now a part of the Dolla object
99
422
  Dolla.router.$path;
@@ -0,0 +1,53 @@
1
+ # Stores
2
+
3
+ Ideas for updating the API.
4
+
5
+ ```js
6
+ function CounterStore(initialCount = 0, ctx) {
7
+ const [$value, setValue] = createState(initialCount);
8
+
9
+ ctx.on("counter:increment", (e) => {
10
+ e.stop(); // Stop this event from bubbling up to counters at higher levels (if any).
11
+ setValue((current) => current + 1);
12
+ });
13
+
14
+ ctx.on("counter:decrement", (e) => {
15
+ e.stop();
16
+ setValue((current) => current - 1);
17
+ });
18
+
19
+ // Events can be emitted from this context in a store.
20
+ ctx.emit("otherEvent");
21
+
22
+ ctx.onMount(() => {
23
+ // Setup
24
+ // This is called based on the context the store is attached to.
25
+ // If Dolla, it's called when the app is mounted. If ViewContext, it's called when the view is mounted.
26
+ });
27
+ ctx.onUnmount(() => {
28
+ // Cleanup
29
+ });
30
+
31
+ // Context variables will be accessible on the same context (e.g. the view this is attached to and below)
32
+ ctx.get("context variable");
33
+ ctx.set("context variable", "context variable value");
34
+
35
+ // Stores don't have to return anything, but if they do it becomes accessible with `ctx.use(Store)`.
36
+ return $value;
37
+ }
38
+
39
+ // Attach it to the app.
40
+ Dolla.provide(CounterStore, 0);
41
+
42
+ function ExampleView(props, ctx) {
43
+ // ctx.use lets you access the return value
44
+ // but the events will still be received and handled regardless
45
+ const $count = ctx.use(Counter);
46
+
47
+ return html`
48
+ <button onclick=${() => this.emit("counter:decrement")}>-1</button>
49
+ <span>${$count}</span>
50
+ <button onclick=${() => this.emit("counter:increment")}>+1</button>
51
+ `;
52
+ }
53
+ ```