clarusjs 0.9.0 → 0.11.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/dist/check/generate.js +15 -8
- package/dist/cli/check.d.ts +7 -0
- package/dist/cli/check.js +14 -6
- package/dist/cli/explain.js +1 -1
- package/dist/cli/index.js +6 -3
- package/dist/cli/project.d.ts +7 -2
- package/dist/cli/project.js +4 -3
- package/dist/compiler/emit-client.d.ts +2 -1
- package/dist/compiler/emit-client.js +19 -7
- package/dist/compiler/emit-server.d.ts +1 -1
- package/dist/compiler/emit-server.js +4 -1
- package/dist/compiler/errors.d.ts +14 -1
- package/dist/compiler/html-expr.d.ts +21 -0
- package/dist/compiler/html-expr.js +24 -1
- package/dist/compiler/index.d.ts +23 -2
- package/dist/compiler/index.js +5 -3
- package/dist/compiler/ir.d.ts +6 -1
- package/dist/compiler/ir.js +104 -4
- package/dist/compiler/lint.d.ts +82 -0
- package/dist/compiler/lint.js +177 -0
- package/dist/compiler/parse.d.ts +34 -0
- package/dist/compiler/parse.js +11 -0
- package/dist/compiler/script.js +27 -0
- package/dist/fetch/index.d.ts +149 -0
- package/dist/fetch/index.js +312 -0
- package/dist/forms/index.d.ts +21 -2
- package/dist/forms/types.d.ts +20 -3
- package/dist/runtime/attrs.js +10 -2
- package/dist/runtime/dom.d.ts +15 -7
- package/dist/runtime/dom.js +33 -13
- package/dist/runtime/env.d.ts +8 -1
- package/dist/runtime/env.js +8 -1
- package/dist/runtime/hot.d.ts +2 -0
- package/dist/runtime/hot.js +51 -0
- package/dist/runtime/html.d.ts +9 -7
- package/dist/runtime/html.js +12 -7
- package/dist/runtime/navigate.d.ts +5 -0
- package/dist/runtime/navigation.d.ts +85 -13
- package/dist/runtime/navigation.js +77 -11
- package/dist/runtime/serialize.d.ts +1 -1
- package/dist/runtime/serialize.js +1 -1
- package/dist/runtime/server.d.ts +18 -1
- package/dist/runtime/server.js +42 -4
- package/dist/runtime/signals.d.ts +97 -4
- package/dist/runtime/signals.js +88 -4
- package/dist/server/handler.d.ts +115 -5
- package/dist/server/handler.js +233 -10
- package/dist/server/head.d.ts +4 -1
- package/dist/server/manifest.d.ts +14 -1
- package/dist/server/views.d.ts +64 -6
- package/dist/server/views.js +46 -0
- package/dist/vite/aliases.js +3 -3
- package/dist/vite/build.d.ts +2 -1
- package/dist/vite/build.js +1 -1
- package/dist/vite/manifest.d.ts +15 -0
- package/dist/vite/manifest.js +2 -2
- package/dist/vite/node.js +4 -1
- package/dist/vite/plugin.d.ts +47 -3
- package/dist/vite/plugin.js +197 -106
- package/dist/vite/sitemap.d.ts +1 -0
- package/package.json +5 -1
|
@@ -8,6 +8,15 @@ const browser = typeof document !== 'undefined';
|
|
|
8
8
|
/**
|
|
9
9
|
* The current page's URL, as a store: `let here = shared(currentUrl)` in an island's script. `null` on the server
|
|
10
10
|
* (what the server renders), so pass the page's path as a prop when the server HTML needs it.
|
|
11
|
+
* @example
|
|
12
|
+
* ```tscl
|
|
13
|
+
* <script>
|
|
14
|
+
* import { currentUrl } from 'clarusjs/navigation'
|
|
15
|
+
* let here = shared(currentUrl)
|
|
16
|
+
* </script>
|
|
17
|
+
* <a href="/guide" [class]="here?.pathname === '/guide' ? 'active' : ''">Guide</a>
|
|
18
|
+
* ```
|
|
19
|
+
* @see https://clarusjs.com/reference/navigation#currenturl
|
|
11
20
|
* @see https://clarusjs.com/guide/fragment-navigation
|
|
12
21
|
*/
|
|
13
22
|
export const currentUrl = state(browser ? new URL(location.href) : null);
|
|
@@ -25,8 +34,15 @@ if (browser) {
|
|
|
25
34
|
* Go to `to`, as a link click would: the page's navigator swaps or renders it. Resolves once the new page is shown,
|
|
26
35
|
* and also when the navigation is cancelled or a newer one replaces it (so check `currentUrl` when it matters);
|
|
27
36
|
* rejects when it fails.
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* async function save() {
|
|
40
|
+
* await post(form)
|
|
41
|
+
* await navigate('/account', { replace: true })
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
* @see https://clarusjs.com/reference/navigation#navigate
|
|
28
45
|
* @see https://clarusjs.com/guide/fragment-navigation
|
|
29
|
-
* @see https://clarusjs.com/guide/client-routes
|
|
30
46
|
*/
|
|
31
47
|
export async function navigate(to, options = {}) {
|
|
32
48
|
if (!browser)
|
|
@@ -45,13 +61,24 @@ export async function navigate(to, options = {}) {
|
|
|
45
61
|
* Stops the navigation in progress, like the browser's stop button: its requests are cancelled and, where the URL was
|
|
46
62
|
* held while loading (push and replace in browsers with precommit handlers), page and URL stay as they were. Hooks
|
|
47
63
|
* see it through `t.signal`. Like the stop button, it also stops the page's own loading (images still arriving).
|
|
48
|
-
* @
|
|
64
|
+
* @example
|
|
65
|
+
* ```tscl
|
|
66
|
+
* <button (click)="abort()">Stop loading</button>
|
|
67
|
+
* ```
|
|
68
|
+
* @see https://clarusjs.com/reference/navigation#abort
|
|
49
69
|
*/
|
|
50
70
|
export function abort() {
|
|
51
71
|
if (browser)
|
|
52
72
|
stop();
|
|
53
73
|
}
|
|
54
|
-
/**
|
|
74
|
+
/**
|
|
75
|
+
* Back one entry, like the browser's back button.
|
|
76
|
+
* @example
|
|
77
|
+
* ```tscl
|
|
78
|
+
* <button (click)="back()">Back</button>
|
|
79
|
+
* ```
|
|
80
|
+
* @see https://clarusjs.com/reference/navigation#back
|
|
81
|
+
*/
|
|
55
82
|
export function back() {
|
|
56
83
|
if (browser)
|
|
57
84
|
history.back();
|
|
@@ -66,21 +93,35 @@ function hook(type, listener) {
|
|
|
66
93
|
/**
|
|
67
94
|
* Before any navigation (links, forms, back/forward, `navigate()`): `nav.cancel()` stops it, for example to keep
|
|
68
95
|
* unsaved work. Back/forward can only be cancelled where the browser allows it.
|
|
69
|
-
* @
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* beforeNavigate((to, nav) => {
|
|
99
|
+
* if (dirty && !confirm('Leave without saving?')) nav.cancel()
|
|
100
|
+
* })
|
|
101
|
+
* ```
|
|
102
|
+
* @see https://clarusjs.com/reference/navigation#beforenavigate
|
|
70
103
|
*/
|
|
71
104
|
export function beforeNavigate(fn) {
|
|
72
105
|
hook('navigate', (e) => fn(new URL(e.destination.url), { type: e.navigationType, cancel: () => e.preventDefault() }));
|
|
73
106
|
}
|
|
74
107
|
/**
|
|
75
108
|
* After a navigation within the page finished: the new page is in and its islands attached.
|
|
76
|
-
* @
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* afterNavigate((to) => analytics.page(to.pathname))
|
|
112
|
+
* ```
|
|
113
|
+
* @see https://clarusjs.com/reference/navigation#afternavigate
|
|
77
114
|
*/
|
|
78
115
|
export function afterNavigate(fn) {
|
|
79
116
|
hook('navigatesuccess', () => fn(new URL(location.href)));
|
|
80
117
|
}
|
|
81
118
|
/**
|
|
82
119
|
* A navigation failed (a fragment or a client page could not be loaded). The page then loads normally.
|
|
83
|
-
* @
|
|
120
|
+
* @example
|
|
121
|
+
* ```ts
|
|
122
|
+
* onNavigateError((to, error) => report(error, { url: to.href }))
|
|
123
|
+
* ```
|
|
124
|
+
* @see https://clarusjs.com/reference/navigation#onnavigateerror
|
|
84
125
|
*/
|
|
85
126
|
export function onNavigateError(fn) {
|
|
86
127
|
// Where it was going: a navigation that failed before its URL changed (held while loading) never became current.
|
|
@@ -92,7 +133,15 @@ export function onNavigateError(fn) {
|
|
|
92
133
|
* The navigation in progress, as a store (`let nav = shared(navigating)`): `null` when idle, else where it goes and
|
|
93
134
|
* its phase, `'loading'` (the next page is fetched) or `'swapping'` (it replaces this one). For CSS alone,
|
|
94
135
|
* `<html data-cl-navigating>` carries the phase.
|
|
95
|
-
* @
|
|
136
|
+
* @example
|
|
137
|
+
* ```tscl
|
|
138
|
+
* <script>
|
|
139
|
+
* import { navigating } from 'clarusjs/navigation'
|
|
140
|
+
* let nav = shared(navigating)
|
|
141
|
+
* </script>
|
|
142
|
+
* @if (nav) { <div class="progress" role="status">Loading {{ nav.to.pathname }}…</div> }
|
|
143
|
+
* ```
|
|
144
|
+
* @see https://clarusjs.com/reference/navigation#navigating
|
|
96
145
|
*/
|
|
97
146
|
export const navigating = state(null);
|
|
98
147
|
const before = [];
|
|
@@ -122,7 +171,12 @@ let done;
|
|
|
122
171
|
* Page changes run as view transitions where the browser has them (`document.startViewTransition`), so CSS
|
|
123
172
|
* `::view-transition-*` rules animate them; elsewhere they stay instant. Call it once, from any island's script.
|
|
124
173
|
* `beforeSwap` hooks run before the browser's snapshot, `afterSwap` hooks once the new page is in.
|
|
125
|
-
* @
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* // in the layout's island script
|
|
177
|
+
* viewTransitions()
|
|
178
|
+
* ```
|
|
179
|
+
* @see https://clarusjs.com/reference/navigation#viewtransitions
|
|
126
180
|
*/
|
|
127
181
|
export function viewTransitions() {
|
|
128
182
|
transitions = true;
|
|
@@ -137,7 +191,11 @@ function inBetween(hooks, fn) {
|
|
|
137
191
|
* Runs once the next page is loaded, before it replaces this one (animate out, save state). Return a promise to
|
|
138
192
|
* make the navigation wait; `t.signal` aborts when a newer navigation takes over. A hook that throws makes the page
|
|
139
193
|
* load normally.
|
|
140
|
-
* @
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* beforeSwap(() => document.querySelector('main')?.animate({ opacity: [1, 0] }, 150).finished)
|
|
197
|
+
* ```
|
|
198
|
+
* @see https://clarusjs.com/reference/navigation#beforeswap
|
|
141
199
|
*/
|
|
142
200
|
export function beforeSwap(fn) {
|
|
143
201
|
inBetween(before, fn);
|
|
@@ -145,7 +203,11 @@ export function beforeSwap(fn) {
|
|
|
145
203
|
/**
|
|
146
204
|
* Runs once the new page is in and its islands attached (animate in, move focus). Return a promise to make the
|
|
147
205
|
* navigation wait for it.
|
|
148
|
-
* @
|
|
206
|
+
* @example
|
|
207
|
+
* ```ts
|
|
208
|
+
* afterSwap(() => document.querySelector('h1')?.focus())
|
|
209
|
+
* ```
|
|
210
|
+
* @see https://clarusjs.com/reference/navigation#afterswap
|
|
149
211
|
*/
|
|
150
212
|
export function afterSwap(fn) {
|
|
151
213
|
inBetween(after, fn);
|
|
@@ -154,7 +216,11 @@ export function afterSwap(fn) {
|
|
|
154
216
|
* A page's URL from its route, as its file names it, with the site's base: `href('/releases/[version]',
|
|
155
217
|
* { version: '0.8.0' })` is `'/releases/0.8.0'`. With .clarusjs/routes.d.ts (written by `clarusjs check`), a route
|
|
156
218
|
* that doesn't exist or a missing param is a type error. Params are encoded.
|
|
157
|
-
* @
|
|
219
|
+
* @example
|
|
220
|
+
* ```tscl
|
|
221
|
+
* <a [href]="href('/releases/[version]', { version: '0.8.0' })">0.8.0</a>
|
|
222
|
+
* ```
|
|
223
|
+
* @see https://clarusjs.com/reference/navigation#href
|
|
158
224
|
*/
|
|
159
225
|
export function href(route,
|
|
160
226
|
// Without the generated routes, params are optional; with them, required exactly when the route has some.
|
package/dist/runtime/server.d.ts
CHANGED
|
@@ -3,7 +3,24 @@ export { escape, json, blockHtml, forHtml, regionHtml } from './html.ts';
|
|
|
3
3
|
/**
|
|
4
4
|
* Wrap a stateful component's HTML in its island boundary: name, client chunk URL, props (`p`) and, when the
|
|
5
5
|
* component has state, the state values it rendered with (`s`), so the browser starts from the same values.
|
|
6
|
+
* @internal Compiled components call this; apps don't.
|
|
6
7
|
*/
|
|
7
8
|
export declare function island(name: string, src: string, props: Record<string, unknown>, html: string, state?: Record<string, unknown>, attach?: string, paths?: Record<string, unknown>): string;
|
|
8
|
-
/** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. */
|
|
9
|
+
/** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. @internal */
|
|
9
10
|
export declare const attr: typeof attrHtml;
|
|
11
|
+
/**
|
|
12
|
+
* Serialize data for the browser (an island's props and state, a client route's data), refusing h() output first: it's
|
|
13
|
+
* HTML for server-rendered pages, and serialized it would only be a confusing "it is a function" error.
|
|
14
|
+
* @internal clarusjs's own server code calls this; apps don't.
|
|
15
|
+
*/
|
|
16
|
+
export declare function toBrowser(value: unknown, path?: string, seen?: Set<object>): string;
|
|
17
|
+
/**
|
|
18
|
+
* The data a component's loader returned for the page being rendered (#97), or nothing outside such a render. Read by
|
|
19
|
+
* compiled components that have a loader. @internal
|
|
20
|
+
*/
|
|
21
|
+
export declare function loaded(key: string): object;
|
|
22
|
+
/**
|
|
23
|
+
* Render with component loaders' results available to `loaded()`. Rendering is synchronous, so one render's data never
|
|
24
|
+
* reaches another's; the previous data is restored afterwards, even when the render throws. @internal
|
|
25
|
+
*/
|
|
26
|
+
export declare function withLoaded<T>(data: Map<string, object>, render: () => T): T;
|
package/dist/runtime/server.js
CHANGED
|
@@ -1,17 +1,55 @@
|
|
|
1
1
|
// Server-side helpers used by compiled render() functions. Runs at build time or per request, never in the browser.
|
|
2
|
-
import { attrHtml, escape } from './html.js';
|
|
2
|
+
import { attrHtml, escape, HTML } from './html.js';
|
|
3
3
|
import { stringify } from './serialize.js';
|
|
4
4
|
export { escape, json, blockHtml, forHtml, regionHtml } from './html.js';
|
|
5
5
|
/**
|
|
6
6
|
* Wrap a stateful component's HTML in its island boundary: name, client chunk URL, props (`p`) and, when the
|
|
7
7
|
* component has state, the state values it rendered with (`s`), so the browser starts from the same values.
|
|
8
|
+
* @internal Compiled components call this; apps don't.
|
|
8
9
|
*/
|
|
9
10
|
export function island(name, src, props, html, state, attach, paths) {
|
|
10
|
-
const s = state === undefined ? '' : ` s="${escape(
|
|
11
|
+
const s = state === undefined ? '' : ` s="${escape(toBrowser(state))}"`;
|
|
11
12
|
const a = attach && attach !== 'load' ? ` a="${escape(attach)}"` : ''; // when to attach: see runtime/loader.ts
|
|
12
13
|
// A component bound two ways carries each binding's path for the forms engine (clarusjs/forms), given by its parent.
|
|
13
14
|
const d = Object.entries(paths ?? {}).map(([model, path]) => ` data-cl-path-${escape(model)}="${escape(String(path))}"`).join('');
|
|
14
|
-
return `<cl-island c="${escape(name)}" src="${escape(src)}" p="${escape(
|
|
15
|
+
return `<cl-island c="${escape(name)}" src="${escape(src)}" p="${escape(toBrowser(props))}"${s}${a}${d}>${html}</cl-island>`;
|
|
15
16
|
}
|
|
16
|
-
/** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. */
|
|
17
|
+
/** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. @internal */
|
|
17
18
|
export const attr = attrHtml;
|
|
19
|
+
/**
|
|
20
|
+
* Serialize data for the browser (an island's props and state, a client route's data), refusing h() output first: it's
|
|
21
|
+
* HTML for server-rendered pages, and serialized it would only be a confusing "it is a function" error.
|
|
22
|
+
* @internal clarusjs's own server code calls this; apps don't.
|
|
23
|
+
*/
|
|
24
|
+
export function toBrowser(value, path = '', seen = new Set()) {
|
|
25
|
+
if (value && typeof value === 'object' && !seen.has(value)) {
|
|
26
|
+
if (value[HTML] === true)
|
|
27
|
+
throw new TypeError(`[clarusjs] cannot send ${path || 'the value'} to the browser: it is h() output (HTML for server-rendered pages); an island's props and a client route's data can only be data`);
|
|
28
|
+
seen.add(value);
|
|
29
|
+
for (const [key, item] of Object.entries(value))
|
|
30
|
+
toBrowser(item, path ? `${path}.${key}` : key, seen);
|
|
31
|
+
}
|
|
32
|
+
return path ? '' : stringify(value);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The data a component's loader returned for the page being rendered (#97), or nothing outside such a render. Read by
|
|
36
|
+
* compiled components that have a loader. @internal
|
|
37
|
+
*/
|
|
38
|
+
export function loaded(key) {
|
|
39
|
+
return globalThis.__clLoaded?.get(key) ?? {};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Render with component loaders' results available to `loaded()`. Rendering is synchronous, so one render's data never
|
|
43
|
+
* reaches another's; the previous data is restored afterwards, even when the render throws. @internal
|
|
44
|
+
*/
|
|
45
|
+
export function withLoaded(data, render) {
|
|
46
|
+
const scope = globalThis;
|
|
47
|
+
const outer = scope.__clLoaded;
|
|
48
|
+
scope.__clLoaded = data;
|
|
49
|
+
try {
|
|
50
|
+
return render();
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
scope.__clLoaded = outer;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -1,24 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A value that can change, and the code that reads it runs again: `get()`, `set(value)`, and the value it started with.
|
|
3
|
+
* In an island's script you read and write a `state()` as a plain variable; in a `.ts` module, through these.
|
|
4
|
+
* @see https://clarusjs.com/reference/runtime#state
|
|
5
|
+
*/
|
|
1
6
|
export type Signal<T> = {
|
|
2
7
|
get(): T;
|
|
3
8
|
set(v: T): void;
|
|
4
9
|
readonly initial: T;
|
|
5
10
|
};
|
|
11
|
+
/**
|
|
12
|
+
* A value to read, not write: what `derived()` gives.
|
|
13
|
+
* @see https://clarusjs.com/reference/runtime#derived
|
|
14
|
+
*/
|
|
6
15
|
export type ReadSignal<T> = {
|
|
7
16
|
get(): T;
|
|
8
17
|
};
|
|
9
|
-
/**
|
|
18
|
+
/**
|
|
19
|
+
* Run `fn`; everything it creates (effects, onDispose cleanups, nested scopes) is undone by the returned dispose. An
|
|
20
|
+
* island's script already runs in one, disposed when the island leaves the page.
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const stop = scope(() => {
|
|
24
|
+
* effect(() => console.log(count.get()))
|
|
25
|
+
* })
|
|
26
|
+
* stop() // the effect stops
|
|
27
|
+
* ```
|
|
28
|
+
* @see https://clarusjs.com/reference/runtime#scope
|
|
29
|
+
*/
|
|
10
30
|
export declare function scope(fn: () => void): () => void;
|
|
11
|
-
/**
|
|
31
|
+
/**
|
|
32
|
+
* Run `fn` when the enclosing scope is disposed (an island leaving the page, an @if branch or @for row going away).
|
|
33
|
+
* Outside a scope it does nothing.
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* if (browser) {
|
|
37
|
+
* const onResize = () => (width = innerWidth)
|
|
38
|
+
* addEventListener('resize', onResize)
|
|
39
|
+
* onDispose(() => removeEventListener('resize', onResize))
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
* @see https://clarusjs.com/reference/runtime#ondispose
|
|
43
|
+
*/
|
|
12
44
|
export declare function onDispose(fn: () => void): void;
|
|
13
|
-
/**
|
|
45
|
+
/**
|
|
46
|
+
* Read signals without making the running effect (or template expression) depend on them: it doesn't run again
|
|
47
|
+
* when they change.
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* effect(() => {
|
|
51
|
+
* log(query.get(), untrack(() => page.get())) // runs again when query changes, not page
|
|
52
|
+
* })
|
|
53
|
+
* ```
|
|
54
|
+
* @see https://clarusjs.com/reference/runtime#untrack
|
|
55
|
+
*/
|
|
14
56
|
export declare function untrack<T>(fn: () => T): T;
|
|
15
|
-
/**
|
|
57
|
+
/**
|
|
58
|
+
* Run `fn`, then the effects its writes affect, once each: they never see some of its writes without the others.
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* batch(() => {
|
|
62
|
+
* first.set('Ada')
|
|
63
|
+
* last.set('Lovelace')
|
|
64
|
+
* }) // an effect reading both runs once, with both names
|
|
65
|
+
* ```
|
|
66
|
+
* @see https://clarusjs.com/reference/runtime#batch
|
|
67
|
+
*/
|
|
16
68
|
export declare function batch(fn: () => void): void;
|
|
69
|
+
/**
|
|
70
|
+
* A reactive value: what reads it (the template, an effect, a derived value) updates when it changes. In an island's
|
|
71
|
+
* script, `let count = state(0)` is read and written as a plain variable (`count++`); in a `.ts` module (a store),
|
|
72
|
+
* through `get()` and `set()`.
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* // stores/cart.ts
|
|
76
|
+
* export const items = state<string[]>([])
|
|
77
|
+
* export const add = (item: string) => items.set([...items.get(), item])
|
|
78
|
+
* ```
|
|
79
|
+
* @see https://clarusjs.com/reference/runtime#state
|
|
80
|
+
*/
|
|
17
81
|
export declare function state<T>(initial: T): Signal<T>;
|
|
82
|
+
/**
|
|
83
|
+
* Run `fn` now and again whenever a signal it read changes. Returns a function that stops it; in an island it stops
|
|
84
|
+
* when the island leaves the page.
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* effect(() => {
|
|
88
|
+
* document.title = `${count.get()} new messages`
|
|
89
|
+
* })
|
|
90
|
+
* ```
|
|
91
|
+
* @see https://clarusjs.com/reference/runtime#effect
|
|
92
|
+
*/
|
|
18
93
|
export declare function effect(fn: () => void): () => void;
|
|
94
|
+
/**
|
|
95
|
+
* A value computed from other signals, kept up to date as they change. Read-only. In an island's script it reads as
|
|
96
|
+
* a plain variable.
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* let price = state(20)
|
|
100
|
+
* let quantity = state(3)
|
|
101
|
+
* const total = derived(() => price * quantity) // 60, then follows both
|
|
102
|
+
* ```
|
|
103
|
+
* @see https://clarusjs.com/reference/runtime#derived
|
|
104
|
+
*/
|
|
19
105
|
export declare function derived<T>(fn: () => T): ReadSignal<T>;
|
|
20
106
|
/**
|
|
21
107
|
* Use a store (a module-level `state()`) in an island: `let items = shared(todos)` makes `items` read and write the
|
|
22
108
|
* store, so every island using it stays in sync. On the server it renders the store's initial value.
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* import { todos } from '../stores/todos.ts'
|
|
112
|
+
* let items = shared(todos)
|
|
113
|
+
* function add(text) { items = [...items, text] } // every island showing todos updates
|
|
114
|
+
* ```
|
|
115
|
+
* @see https://clarusjs.com/reference/runtime#shared
|
|
23
116
|
*/
|
|
24
117
|
export declare function shared<T>(store: Signal<T>): Signal<T>;
|
package/dist/runtime/signals.js
CHANGED
|
@@ -2,7 +2,18 @@
|
|
|
2
2
|
let current = null;
|
|
3
3
|
// Cleanups registered while a scope() is running: effect stops, event listener removals, child scopes.
|
|
4
4
|
let currentScope = null;
|
|
5
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* Run `fn`; everything it creates (effects, onDispose cleanups, nested scopes) is undone by the returned dispose. An
|
|
7
|
+
* island's script already runs in one, disposed when the island leaves the page.
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const stop = scope(() => {
|
|
11
|
+
* effect(() => console.log(count.get()))
|
|
12
|
+
* })
|
|
13
|
+
* stop() // the effect stops
|
|
14
|
+
* ```
|
|
15
|
+
* @see https://clarusjs.com/reference/runtime#scope
|
|
16
|
+
*/
|
|
6
17
|
export function scope(fn) {
|
|
7
18
|
const prev = currentScope;
|
|
8
19
|
const cleanups = [];
|
|
@@ -18,11 +29,33 @@ export function scope(fn) {
|
|
|
18
29
|
onDispose(dispose);
|
|
19
30
|
return dispose;
|
|
20
31
|
}
|
|
21
|
-
/**
|
|
32
|
+
/**
|
|
33
|
+
* Run `fn` when the enclosing scope is disposed (an island leaving the page, an @if branch or @for row going away).
|
|
34
|
+
* Outside a scope it does nothing.
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* if (browser) {
|
|
38
|
+
* const onResize = () => (width = innerWidth)
|
|
39
|
+
* addEventListener('resize', onResize)
|
|
40
|
+
* onDispose(() => removeEventListener('resize', onResize))
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
* @see https://clarusjs.com/reference/runtime#ondispose
|
|
44
|
+
*/
|
|
22
45
|
export function onDispose(fn) {
|
|
23
46
|
currentScope?.push(fn);
|
|
24
47
|
}
|
|
25
|
-
/**
|
|
48
|
+
/**
|
|
49
|
+
* Read signals without making the running effect (or template expression) depend on them: it doesn't run again
|
|
50
|
+
* when they change.
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* effect(() => {
|
|
54
|
+
* log(query.get(), untrack(() => page.get())) // runs again when query changes, not page
|
|
55
|
+
* })
|
|
56
|
+
* ```
|
|
57
|
+
* @see https://clarusjs.com/reference/runtime#untrack
|
|
58
|
+
*/
|
|
26
59
|
export function untrack(fn) {
|
|
27
60
|
const prev = current;
|
|
28
61
|
current = null;
|
|
@@ -35,7 +68,17 @@ export function untrack(fn) {
|
|
|
35
68
|
}
|
|
36
69
|
// Effects waiting for the current batch to end (null outside a batch).
|
|
37
70
|
let queue = null;
|
|
38
|
-
/**
|
|
71
|
+
/**
|
|
72
|
+
* Run `fn`, then the effects its writes affect, once each: they never see some of its writes without the others.
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* batch(() => {
|
|
76
|
+
* first.set('Ada')
|
|
77
|
+
* last.set('Lovelace')
|
|
78
|
+
* }) // an effect reading both runs once, with both names
|
|
79
|
+
* ```
|
|
80
|
+
* @see https://clarusjs.com/reference/runtime#batch
|
|
81
|
+
*/
|
|
39
82
|
export function batch(fn) {
|
|
40
83
|
if (queue)
|
|
41
84
|
return fn();
|
|
@@ -51,6 +94,18 @@ export function batch(fn) {
|
|
|
51
94
|
e.run();
|
|
52
95
|
}
|
|
53
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* A reactive value: what reads it (the template, an effect, a derived value) updates when it changes. In an island's
|
|
99
|
+
* script, `let count = state(0)` is read and written as a plain variable (`count++`); in a `.ts` module (a store),
|
|
100
|
+
* through `get()` and `set()`.
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* // stores/cart.ts
|
|
104
|
+
* export const items = state<string[]>([])
|
|
105
|
+
* export const add = (item: string) => items.set([...items.get(), item])
|
|
106
|
+
* ```
|
|
107
|
+
* @see https://clarusjs.com/reference/runtime#state
|
|
108
|
+
*/
|
|
54
109
|
export function state(initial) {
|
|
55
110
|
let value = initial;
|
|
56
111
|
const subs = new Set();
|
|
@@ -77,6 +132,17 @@ export function state(initial) {
|
|
|
77
132
|
},
|
|
78
133
|
};
|
|
79
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Run `fn` now and again whenever a signal it read changes. Returns a function that stops it; in an island it stops
|
|
137
|
+
* when the island leaves the page.
|
|
138
|
+
* @example
|
|
139
|
+
* ```ts
|
|
140
|
+
* effect(() => {
|
|
141
|
+
* document.title = `${count.get()} new messages`
|
|
142
|
+
* })
|
|
143
|
+
* ```
|
|
144
|
+
* @see https://clarusjs.com/reference/runtime#effect
|
|
145
|
+
*/
|
|
80
146
|
export function effect(fn) {
|
|
81
147
|
const e = {
|
|
82
148
|
deps: new Set(),
|
|
@@ -104,6 +170,17 @@ export function effect(fn) {
|
|
|
104
170
|
return stop;
|
|
105
171
|
}
|
|
106
172
|
// ponytail: eager derived, may compute twice on diamond graphs; switch to push-pull (alien-signals style) when nested derived lands
|
|
173
|
+
/**
|
|
174
|
+
* A value computed from other signals, kept up to date as they change. Read-only. In an island's script it reads as
|
|
175
|
+
* a plain variable.
|
|
176
|
+
* @example
|
|
177
|
+
* ```ts
|
|
178
|
+
* let price = state(20)
|
|
179
|
+
* let quantity = state(3)
|
|
180
|
+
* const total = derived(() => price * quantity) // 60, then follows both
|
|
181
|
+
* ```
|
|
182
|
+
* @see https://clarusjs.com/reference/runtime#derived
|
|
183
|
+
*/
|
|
107
184
|
export function derived(fn) {
|
|
108
185
|
const s = state(undefined);
|
|
109
186
|
effect(() => s.set(fn()));
|
|
@@ -112,6 +189,13 @@ export function derived(fn) {
|
|
|
112
189
|
/**
|
|
113
190
|
* Use a store (a module-level `state()`) in an island: `let items = shared(todos)` makes `items` read and write the
|
|
114
191
|
* store, so every island using it stays in sync. On the server it renders the store's initial value.
|
|
192
|
+
* @example
|
|
193
|
+
* ```ts
|
|
194
|
+
* import { todos } from '../stores/todos.ts'
|
|
195
|
+
* let items = shared(todos)
|
|
196
|
+
* function add(text) { items = [...items, text] } // every island showing todos updates
|
|
197
|
+
* ```
|
|
198
|
+
* @see https://clarusjs.com/reference/runtime#shared
|
|
115
199
|
*/
|
|
116
200
|
export function shared(store) {
|
|
117
201
|
return store;
|