effect-atom-svelte 0.4.1 → 0.5.1
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 +6 -7
- package/dist/Form.d.ts +58 -24
- package/dist/Hooks.svelte.d.ts +117 -61
- package/dist/Hooks.svelte.js +184 -73
- package/dist/HydrationBoundary.svelte +24 -17
- package/dist/HydrationBoundary.svelte.d.ts +23 -16
- package/dist/Inspector.d.ts +32 -16
- package/dist/Inspector.js +29 -14
- package/dist/RegistryContext.d.ts +14 -5
- package/dist/RegistryContext.js +3 -1
- package/dist/RegistryProvider.svelte +4 -2
- package/dist/RegistryProvider.svelte.d.ts +2 -1
- package/dist/ScopedAtom.d.ts +3 -2
- package/dist/ScopedAtom.js +5 -3
- package/dist/SvelteKit.d.ts +94 -3
- package/dist/SvelteKit.js +168 -8
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/internal/follow.svelte.d.ts +7 -0
- package/dist/internal/follow.svelte.js +15 -0
- package/dist/internal/form/engine.js +140 -45
- package/dist/internal/form/field.js +11 -4
- package/dist/internal/form/types.d.ts +9 -0
- package/dist/internal/hydration.d.ts +2 -1
- package/dist/internal/hydration.js +6 -2
- package/dist/internal/kitVersion.d.ts +5 -0
- package/dist/internal/kitVersion.js +11 -0
- package/dist/internal/nodeInternals.js +2 -2
- package/dist/internal/remoteQueries.d.ts +5 -0
- package/dist/internal/remoteQueries.js +11 -0
- package/package.json +8 -7
package/README.md
CHANGED
|
@@ -8,14 +8,14 @@ This is a community project by Jarred Norris. It is not part of Effect, and the
|
|
|
8
8
|
|
|
9
9
|
## Requirements
|
|
10
10
|
|
|
11
|
-
- `effect` 4.
|
|
12
|
-
- Svelte 5.57.2 or later.
|
|
13
|
-
- SvelteKit is optional. The docs site and its tests run on SvelteKit 3. On SvelteKit 2, the error hooks in `effect-atom-svelte/sveltekit` work with less detail, and reach `<svelte:boundary>` only with `kit.experimental.handleRenderingErrors` turned on.
|
|
11
|
+
- `effect` 4.1.x. The peer range is `~4.1.0`, not `^4.1.0`: the bindings use parts of the atom registry that aren't public API, which a minor release of `effect` can change.
|
|
12
|
+
- Svelte 5.57.2 or later. The async hooks (`useAtomSuspense`, `useAtomResult`) depend on Svelte's experimental async support. `RegistryProvider`, `useAtom`, `useAtomValue` and the other hooks work without it, on the server too. `HydrationBoundary` works without it but can't update atoms that already exist while the page hydrates.
|
|
13
|
+
- SvelteKit is optional. The docs site and its tests run on SvelteKit 3. On SvelteKit 2, the error hooks in `effect-atom-svelte/sveltekit` work with less detail, and reach `<svelte:boundary>` only with `kit.experimental.handleRenderingErrors` turned on. `remoteQueryAtom` needs SvelteKit 3 with `experimental.remoteFunctions`.
|
|
14
14
|
|
|
15
15
|
## Installation
|
|
16
16
|
|
|
17
17
|
```sh
|
|
18
|
-
npm install effect-atom-svelte "effect@~4.
|
|
18
|
+
npm install effect-atom-svelte "effect@~4.1.0"
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
Turn on Svelte's async mode. In SvelteKit 3, pass it to `sveltekit()` in `vite.config.ts`:
|
|
@@ -64,9 +64,8 @@ On the server, the provider creates a registry for each request, so visitors nev
|
|
|
64
64
|
const greeting = useAtomSuspense(greetingAtom);
|
|
65
65
|
</script>
|
|
66
66
|
|
|
67
|
-
<button onclick={() => (count.current += 1)}
|
|
68
|
-
|
|
69
|
-
>
|
|
67
|
+
<button onclick={() => (count.current += 1)}>Add one</button>
|
|
68
|
+
<p>{count.current} × 2 = {doubled.current}</p>
|
|
70
69
|
|
|
71
70
|
<svelte:boundary>
|
|
72
71
|
<p>{await greeting.current}</p>
|
package/dist/Form.d.ts
CHANGED
|
@@ -86,8 +86,8 @@ export interface MakeOptions {
|
|
|
86
86
|
/** When a field's error first shows. Defaults to `"blur"`. */
|
|
87
87
|
readonly validateOn?: Timing | undefined;
|
|
88
88
|
/**
|
|
89
|
-
* When
|
|
90
|
-
*
|
|
89
|
+
* When a field's error updates once it has shown, or once the form has been submitted. Defaults
|
|
90
|
+
* to `"input"`.
|
|
91
91
|
*/
|
|
92
92
|
readonly revalidateOn?: Timing | undefined;
|
|
93
93
|
/** How long to wait after a change before validating. Defaults to none. */
|
|
@@ -139,7 +139,6 @@ export interface MakeOptionsWithRuntime<R, ER> extends MakeOptions {
|
|
|
139
139
|
* );
|
|
140
140
|
* ```
|
|
141
141
|
*
|
|
142
|
-
* @see {@link https://atom.jarrednorris.dev/forms | Forms}
|
|
143
142
|
* @stability unstable
|
|
144
143
|
* @since 0.4.0
|
|
145
144
|
* @category constructors
|
|
@@ -196,12 +195,17 @@ type ItemOf<V> = NonNullable<V> extends readonly (infer Item)[] ? Item : never;
|
|
|
196
195
|
*/
|
|
197
196
|
export type AnyPath = readonly (string | number)[];
|
|
198
197
|
/**
|
|
199
|
-
* HTML input constraints read from a field's Schema checks
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
*
|
|
198
|
+
* HTML input constraints read from a field's Schema checks. Other checks set nothing, and neither
|
|
199
|
+
* do exclusive bounds.
|
|
200
|
+
*
|
|
201
|
+
* - `required`: where the Schema rejects an empty string, from a `minlength` of 1 or more on a key
|
|
202
|
+
* that isn't optional.
|
|
203
|
+
* - `minlength`: from `isMinLength`, `isNonEmpty` and `isBetweenLength`.
|
|
204
|
+
* - `maxlength`: from `isMaxLength` and `isBetweenLength`.
|
|
205
|
+
* - `min`: from `isGreaterThanOrEqualTo` and `isBetween`.
|
|
206
|
+
* - `max`: from `isLessThanOrEqualTo` and `isBetween`.
|
|
207
|
+
* - `pattern`: from an `isPattern` anchored with `^` and `$`, without `|`, and with no flags but
|
|
208
|
+
* `u` or `v`: an HTML pattern must match the whole value, a Schema pattern anywhere in it.
|
|
205
209
|
*
|
|
206
210
|
* @stability unstable
|
|
207
211
|
* @since 0.4.0
|
|
@@ -225,7 +229,7 @@ export interface FieldAttrs {
|
|
|
225
229
|
/**
|
|
226
230
|
* One field of a form instance. Every property is reactive in a component. `value` can be
|
|
227
231
|
* assigned, so `bind:value={field.value}` works; an assignment counts as input for validation
|
|
228
|
-
* timing
|
|
232
|
+
* timing. Errors from `mapError` or `setErrors` clear as `setErrors` describes.
|
|
229
233
|
*
|
|
230
234
|
* @stability unstable
|
|
231
235
|
* @since 0.4.0
|
|
@@ -242,7 +246,12 @@ export interface Field<V> {
|
|
|
242
246
|
readonly issue: string | undefined;
|
|
243
247
|
/** Whether the user has left the field. `useForm`'s `attrs.onblur` sets it. */
|
|
244
248
|
touched: boolean;
|
|
245
|
-
/**
|
|
249
|
+
/**
|
|
250
|
+
* Whether the value differs from the initial value. A field in an array item compares with that
|
|
251
|
+
* item's initial value, wherever the item has moved, so removing or moving items leaves the
|
|
252
|
+
* others clean. An item added since (by `append` or `replace`) has no initial value: its fields
|
|
253
|
+
* are dirty.
|
|
254
|
+
*/
|
|
246
255
|
readonly dirty: boolean;
|
|
247
256
|
/**
|
|
248
257
|
* Whether the form's validation is running or waiting for its debounce: the whole form's flag,
|
|
@@ -263,6 +272,11 @@ export interface Field<V> {
|
|
|
263
272
|
* touched.
|
|
264
273
|
*/
|
|
265
274
|
readonly attrs: FieldAttrs;
|
|
275
|
+
/**
|
|
276
|
+
* HTML constraints read from the field's Schema checks, such as `maxlength`, kept out of `attrs`:
|
|
277
|
+
* spread them on a native input, `<input {...field.attrs} {...field.constraints} />`, for the
|
|
278
|
+
* browser's own limits too. `Constraints` lists the checks that set each.
|
|
279
|
+
*/
|
|
266
280
|
readonly constraints: Constraints;
|
|
267
281
|
/** What leaving the field does, for a custom control that has no blur event. */
|
|
268
282
|
readonly blur: () => void;
|
|
@@ -380,6 +394,9 @@ export type ValidationError = FormValidationError;
|
|
|
380
394
|
* never compete with the form's errors, and an `onsubmit` that prevents the browser's submit,
|
|
381
395
|
* submits the form, and moves focus to the first field with an error when validation fails.
|
|
382
396
|
*
|
|
397
|
+
* `attrs.onsubmit` calls `submit()` with no arguments. A form whose `payload` takes arguments needs
|
|
398
|
+
* its own `onsubmit`.
|
|
399
|
+
*
|
|
383
400
|
* @stability unstable
|
|
384
401
|
* @since 0.4.0
|
|
385
402
|
* @category models
|
|
@@ -420,13 +437,21 @@ export interface Instance<T, D, A, E, Args> extends Group<T> {
|
|
|
420
437
|
readonly submitting: boolean;
|
|
421
438
|
/**
|
|
422
439
|
* The last submit's result, holding only failures `mapError` didn't claim. A validation that
|
|
423
|
-
* couldn't run at all, such as when the form's runtime failed to build, ends here as a defect
|
|
440
|
+
* couldn't run at all, such as when the form's runtime failed to build, ends here as a defect, and
|
|
441
|
+
* so does a throw from `payload`, from `mapError` (beside the failure it was given) or from the
|
|
442
|
+
* atom's write, such as an `Atom.fn` that throws before returning its Effect.
|
|
424
443
|
*/
|
|
425
444
|
readonly submitResult: AsyncResult.AsyncResult<A, E>;
|
|
426
445
|
/**
|
|
427
|
-
* A form-level error, on no field
|
|
428
|
-
*
|
|
429
|
-
*
|
|
446
|
+
* A form-level error, on no field, from one of two sources:
|
|
447
|
+
*
|
|
448
|
+
* - **`form` from `mapError` or `setErrors`.** It shows at once, and clears as `setErrors`
|
|
449
|
+
* describes.
|
|
450
|
+
* - **A check on the whole Struct that gives no `path`.** It shows only after `submit` or a
|
|
451
|
+
* `validate()` of the whole form. From then on it follows the latest check, clearing once the
|
|
452
|
+
* values pass it, or on reset.
|
|
453
|
+
*
|
|
454
|
+
* While both are set, the first shows.
|
|
430
455
|
*/
|
|
431
456
|
readonly rootError: string | undefined;
|
|
432
457
|
/**
|
|
@@ -445,14 +470,19 @@ export interface Instance<T, D, A, E, Args> extends Group<T> {
|
|
|
445
470
|
}>;
|
|
446
471
|
/** Whether the values differ from the last successful submit's. */
|
|
447
472
|
readonly dirtySinceSubmit: boolean;
|
|
448
|
-
/**
|
|
473
|
+
/**
|
|
474
|
+
* Attributes to spread on the `<form>` element: see `FormAttrs`. `attrs.onsubmit` calls
|
|
475
|
+
* `submit()` with no arguments. A form whose `payload` takes arguments needs its own `onsubmit`.
|
|
476
|
+
*/
|
|
449
477
|
readonly attrs: FormAttrs;
|
|
450
478
|
/**
|
|
451
479
|
* Decodes the values and, when they decode, writes the submit atom and waits for its result. A
|
|
452
480
|
* call while a submit runs gets that submit's result. A submit that settles after a reset doesn't
|
|
453
481
|
* touch the fresh form. Instances that submit to the same `Atom.fn` share its one result: if
|
|
454
482
|
* another instance writes it while this submit runs, this submit resolves with the other's
|
|
455
|
-
* result, so give each instance its own atom.
|
|
483
|
+
* result, so give each instance its own atom. When the `RegistryProvider` that holds the form
|
|
484
|
+
* is destroyed mid-submit, the submit resolves as interrupted. `attrs.onsubmit` calls `submit()`
|
|
485
|
+
* with no arguments. A form whose `payload` takes arguments needs its own `onsubmit`.
|
|
456
486
|
*/
|
|
457
487
|
readonly submit: (...args: [Args] extends [undefined] ? [] : [args: Args]) => Promise<Exit.Exit<A, E | ValidationError>>;
|
|
458
488
|
/**
|
|
@@ -466,6 +496,9 @@ export interface Instance<T, D, A, E, Args> extends Group<T> {
|
|
|
466
496
|
* Sets several values at once. Not counted as input on any field, so an untouched field's error
|
|
467
497
|
* waits for its own timing; a touched field whose check it breaks shows its error at once, as
|
|
468
498
|
* after another field's change. Like any change, it clears external errors where values changed.
|
|
499
|
+
* Array items pair with the current ones by position: each takes the key, touched state and
|
|
500
|
+
* errors of the item at its index, and its `dirty` compares with that item's initial value. To
|
|
501
|
+
* go back to the initial or submitted values, use `reset` or `revertToLastSubmit`.
|
|
469
502
|
*/
|
|
470
503
|
readonly setValues: (values: T | ((previous: T) => T)) => void;
|
|
471
504
|
/**
|
|
@@ -483,8 +516,9 @@ export interface Instance<T, D, A, E, Args> extends Group<T> {
|
|
|
483
516
|
/**
|
|
484
517
|
* Puts errors on fields or on the form; they show at once, and keep `valid` false while set. A
|
|
485
518
|
* field's error clears when the value at its path changes, including from a change to a parent or
|
|
486
|
-
* a child path. The form-level error (`form`) clears on any change to
|
|
487
|
-
* next submit. Both clear on reset; blur, `validate` and a write of the
|
|
519
|
+
* a child path, and stays across a submit. The form-level error (`form`) clears on any change to
|
|
520
|
+
* the values, and at the next submit. Both clear on reset; blur, `validate` and a write of the
|
|
521
|
+
* same values leave them.
|
|
488
522
|
* They don't stop a submit: it checks only the Schema, and the server checks the values again.
|
|
489
523
|
*/
|
|
490
524
|
readonly setErrors: (errors: Errors<T>) => void;
|
|
@@ -530,8 +564,9 @@ export interface BaseInstanceOptions<T> {
|
|
|
530
564
|
readonly initialValues: T | (() => T);
|
|
531
565
|
/**
|
|
532
566
|
* Storage for the values that you own, such as an `Atom.kvs` draft that survives a reload. A
|
|
533
|
-
* write to it from code counts as a change made in code, not as
|
|
534
|
-
* that the atom
|
|
567
|
+
* write to it from code counts as a change made in code, not as input, as with `setValues`. So
|
|
568
|
+
* does a field write that the atom stores differently, such as trimmed: if the field is already
|
|
569
|
+
* touched, a broken check shows at once instead of waiting for its own timing.
|
|
535
570
|
*/
|
|
536
571
|
readonly values?: Atom.Writable<T> | undefined;
|
|
537
572
|
/**
|
|
@@ -552,9 +587,8 @@ export interface InstanceOptions<T, D, A, E, W, Args> extends BaseInstanceOption
|
|
|
552
587
|
/**
|
|
553
588
|
* Claims a typed failure: return errors to put on fields or the form, or `undefined` to leave
|
|
554
589
|
* the failure in `submitResult`. Defects and interruptions are never passed here. The errors
|
|
555
|
-
* show at once and keep `valid` false while set.
|
|
556
|
-
*
|
|
557
|
-
* next submit and on reset.
|
|
590
|
+
* show at once and keep `valid` false while set. They clear as `setErrors` describes, and all of
|
|
591
|
+
* `mapError`'s errors, field errors included, also clear at the next submit.
|
|
558
592
|
*/
|
|
559
593
|
readonly mapError?: ((error: E) => Errors<T> | undefined) | undefined;
|
|
560
594
|
/** Submit after each change that validates. Only for a submit that takes no arguments. */
|
package/dist/Hooks.svelte.d.ts
CHANGED
|
@@ -56,8 +56,9 @@ export interface WriteOptions {
|
|
|
56
56
|
readonly signal?: AbortSignal | undefined;
|
|
57
57
|
}
|
|
58
58
|
/**
|
|
59
|
-
* Reads an atom, optionally through a transform. The
|
|
60
|
-
*
|
|
59
|
+
* Reads an atom, optionally through a transform. The hook
|
|
60
|
+
* [holds](https://atom.jarrednorris.dev/lifetimes#held-atoms) the atom while something reactive
|
|
61
|
+
* reads `.current`, and lets it go when nothing does. The transform runs again only when the atom,
|
|
61
62
|
* or state the transform reads, changes, so a transform that builds an object returns the same
|
|
62
63
|
* object until then. A read outside a reactive context, such as in an event handler, runs it again.
|
|
63
64
|
*
|
|
@@ -96,11 +97,12 @@ export declare function useAtomValue<A, B>(input: AtomInput<Atom.Atom<A>>, f: (v
|
|
|
96
97
|
*/
|
|
97
98
|
export declare const useAtom: <R, W>(input: AtomInput<Atom.Writable<R, W>>) => AtomState<R, W>;
|
|
98
99
|
/**
|
|
99
|
-
*
|
|
100
|
-
* it. Use it in a component that outlives the readers, such as a
|
|
101
|
-
* value while they come and go.
|
|
100
|
+
* [Holds](https://atom.jarrednorris.dev/lifetimes#held-atoms) an atom while the component lives,
|
|
101
|
+
* even when no component reads it. Use it in a component that outlives the readers, such as a
|
|
102
|
+
* layout, so the atom keeps its value while they come and go. Holding an atom that hasn't been
|
|
103
|
+
* computed also computes it.
|
|
102
104
|
*
|
|
103
|
-
* **Example** (Keeping a connection open while a component
|
|
105
|
+
* **Example** (Keeping a connection open while a component lives)
|
|
104
106
|
*
|
|
105
107
|
* ```ts
|
|
106
108
|
* import { useAtomMount } from "effect-atom-svelte";
|
|
@@ -116,22 +118,34 @@ export declare const useAtom: <R, W>(input: AtomInput<Atom.Writable<R, W>>) => A
|
|
|
116
118
|
*/
|
|
117
119
|
export declare const useAtomMount: (input: AtomInput<Atom.Atom<unknown>>) => void;
|
|
118
120
|
/**
|
|
119
|
-
* Returns a setter. The atom
|
|
120
|
-
* state between calls and is not disposed
|
|
121
|
+
* Returns a setter. The hook [holds](https://atom.jarrednorris.dev/lifetimes#held-atoms) the atom
|
|
122
|
+
* while the component lives, so an `Atom.fn` keeps its state between calls and is not disposed
|
|
123
|
+
* between set and read.
|
|
121
124
|
*
|
|
122
125
|
* In `value` mode a function passed to the setter is an updater, called with the current value. To
|
|
123
126
|
* store a function in an atom, wrap it: `set(() => handler)`.
|
|
124
127
|
*
|
|
125
|
-
* In `promise` and `promiseExit` modes the setter
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
* the call
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
128
|
+
* In `promise` and `promiseExit` modes the setter returns a promise that settles with the atom's
|
|
129
|
+
* next settled result:
|
|
130
|
+
*
|
|
131
|
+
* - **After the component is destroyed.** The wait holds the atom, so a call still in flight keeps
|
|
132
|
+
* running until it settles, unless the `RegistryProvider` that created the registry is destroyed:
|
|
133
|
+
* that interrupts the call and settles it as interrupted.
|
|
134
|
+
* - **`signal`.** Aborting `signal` settles the promise as interrupted; a signal that is already
|
|
135
|
+
* aborted settles without writing, as `fetch` does. The call itself runs on while something holds
|
|
136
|
+
* the atom; write `Atom.Interrupt` to stop it. Neither `Atom.Interrupt` nor `Atom.Reset` stops a
|
|
137
|
+
* `concurrent` `Atom.fn`'s calls: they run until it is disposed, and a later call waits on them
|
|
138
|
+
* too.
|
|
139
|
+
* - **Shared result.** Calls on one `Atom.fn` share its single result. By default a new call
|
|
140
|
+
* interrupts the one in flight, and every pending call settles with the newest call's result. With
|
|
141
|
+
* `concurrent: true`, calls run together and settle with the oldest running call's success once
|
|
142
|
+
* all have finished, or with the first failure at once.
|
|
143
|
+
* - **A write that throws.** As an `Atom.fn` that throws before returning its Effect does, it
|
|
144
|
+
* settles the call as that defect: `promiseExit` resolves with `Exit.die`, `promise` rejects with
|
|
145
|
+
* the error.
|
|
146
|
+
* - **`Atom.Reset`.** It has no result to wait for, so these modes leave it out of their types and
|
|
147
|
+
* reject it; reset with a `value` mode setter. A reset written while a promise-mode call is pending
|
|
148
|
+
* settles that promise as interrupted.
|
|
135
149
|
*
|
|
136
150
|
* **Example** (Updating an atom from its current value)
|
|
137
151
|
*
|
|
@@ -160,7 +174,9 @@ export declare function useAtomSet<A, E, W>(input: AtomInput<Atom.Writable<Async
|
|
|
160
174
|
readonly mode?: WriteMode | undefined;
|
|
161
175
|
}): (value: W, options?: WriteOptions) => undefined | Promise<A> | Promise<Exit.Exit<A, E>>;
|
|
162
176
|
/**
|
|
163
|
-
* Returns a function that runs the atom again.
|
|
177
|
+
* Returns a function that runs the atom again. It also
|
|
178
|
+
* [holds](https://atom.jarrednorris.dev/lifetimes#held-atoms) the atom while the component lives,
|
|
179
|
+
* so the refreshed result is kept even when nothing on screen reads it.
|
|
164
180
|
*
|
|
165
181
|
* **Example** (Running an atom again on click)
|
|
166
182
|
*
|
|
@@ -179,12 +195,18 @@ export declare function useAtomSet<A, E, W>(input: AtomInput<Atom.Writable<Async
|
|
|
179
195
|
export declare const useAtomRefresh: (input: AtomInput<Atom.Atom<unknown>>) => (() => void);
|
|
180
196
|
/**
|
|
181
197
|
* Calls `f` on every change while the component lives, and with the current value first when
|
|
182
|
-
* `immediate` is set. The atom
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
* change
|
|
187
|
-
*
|
|
198
|
+
* `immediate` is set. The hook [holds](https://atom.jarrednorris.dev/lifetimes#held-atoms) the atom
|
|
199
|
+
* and computes it once the component mounts, never on the server, so a derived or effect atom that
|
|
200
|
+
* nothing else reads still runs and reports its changes.
|
|
201
|
+
*
|
|
202
|
+
* A change raised while another component is reading an atom reaches `f` on a microtask, so `f` can
|
|
203
|
+
* write `$state` (Svelte forbids that during a read). Changes after it wait their turn, so `f` sees
|
|
204
|
+
* every change in order; any other change reaches it synchronously. Nothing reaches `f` once the
|
|
205
|
+
* component is destroyed, not even a change still waiting for its microtask.
|
|
206
|
+
*
|
|
207
|
+
* If the atom's first computation throws, the error is left to the atom's readers and `f` hears the
|
|
208
|
+
* atom once it recovers; with `immediate`, it throws in the component, as there is no value to call
|
|
209
|
+
* `f` with.
|
|
188
210
|
*
|
|
189
211
|
* **Example** (Saving every change)
|
|
190
212
|
*
|
|
@@ -203,20 +225,30 @@ export declare const useAtomSubscribe: <A>(input: AtomInput<Atom.Atom<A>>, f: (v
|
|
|
203
225
|
readonly immediate?: boolean | undefined;
|
|
204
226
|
}) => void;
|
|
205
227
|
/**
|
|
206
|
-
* Sets starting values as `AtomRegistry.make({ initialValues })` would
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
* first
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
* the
|
|
228
|
+
* Sets starting values from a component, as `AtomRegistry.make({ initialValues })` would.
|
|
229
|
+
*
|
|
230
|
+
* - **Once while the atom is held.** Each atom takes the first value a component gives it. Another
|
|
231
|
+
* call while something still holds the atom, as from a component mounting again or a second
|
|
232
|
+
* component with its own value, doesn't set it again; once the atom has been disposed, the next
|
|
233
|
+
* component to start it sets it again. An atom already in use without a value, read by another
|
|
234
|
+
* component, is set to it, as `@effect/atom-react` does.
|
|
235
|
+
* - **Held while the component lives.** The hook
|
|
236
|
+
* [holds](https://atom.jarrednorris.dev/lifetimes#held-atoms) its atoms without computing them:
|
|
237
|
+
* the first component to read an atom computes it, starting from the value, so a value set in a
|
|
238
|
+
* layout is still there when a page reads it later.
|
|
239
|
+
* - **Once per render on the server.** Each server render sets its values again and renders them
|
|
240
|
+
* without running the atom, so an atom that can only compute in the browser can start from a
|
|
241
|
+
* value on the server. Renders at the same time on a shared registry share the value the first
|
|
242
|
+
* one applied, until the last of them ends.
|
|
243
|
+
* - **Wrapped and derived atoms.** A wrapper such as `Atom.withRefresh`, `Atom.swr` or
|
|
244
|
+
* `Atom.debounce` passes the value to its source. The atom starts from the value and still reads
|
|
245
|
+
* its sources, so a derived atom computes again when one of them changes.
|
|
246
|
+
* - **Effect and stream atoms.** In the browser, an atom that runs an effect or a stream still runs
|
|
247
|
+
* it when it is first read, as with `AtomRegistry.make`, and shows the value until the run
|
|
248
|
+
* finishes. To keep the value instead, as for data a `load` function already fetched, pass the
|
|
249
|
+
* entry as `Atom.initialValue(atom, value, { skipInitialRun: true })` (Effect 4.1): the atom then
|
|
250
|
+
* runs once it is refreshed, or once an atom its function reads before returning the effect
|
|
251
|
+
* changes.
|
|
220
252
|
*
|
|
221
253
|
* **Example** (Starting an atom from a prop)
|
|
222
254
|
*
|
|
@@ -235,7 +267,7 @@ export declare const useAtomSubscribe: <A>(input: AtomInput<Atom.Atom<A>>, f: (v
|
|
|
235
267
|
* @since 0.1.0
|
|
236
268
|
* @category hooks
|
|
237
269
|
*/
|
|
238
|
-
export declare const useAtomInitialValues: (initialValues: Iterable<readonly [Atom.Atom<unknown>, unknown]>) => void;
|
|
270
|
+
export declare const useAtomInitialValues: (initialValues: Iterable<readonly [Atom.Atom<unknown>, unknown, Atom.InitialValueOptions?]>) => void;
|
|
239
271
|
/**
|
|
240
272
|
* Reads an `AtomRef`, following it when the getter returns a different ref. For a writable ref,
|
|
241
273
|
* `current` can be assigned too, which sets the ref, so `bind:value={name.current}` works; a
|
|
@@ -243,7 +275,7 @@ export declare const useAtomInitialValues: (initialValues: Iterable<readonly [At
|
|
|
243
275
|
*
|
|
244
276
|
* A ref has no registry. On the server, one created at module level is shared by every request,
|
|
245
277
|
* so don't write a visitor's data to it there: see
|
|
246
|
-
* https://atom.jarrednorris.dev/server-rendering#module-state-is-shared-between-visitors
|
|
278
|
+
* [Module state is shared between visitors](https://atom.jarrednorris.dev/server-rendering#module-state-is-shared-between-visitors).
|
|
247
279
|
*
|
|
248
280
|
* **Example** (Reading a ref)
|
|
249
281
|
*
|
|
@@ -251,8 +283,8 @@ export declare const useAtomInitialValues: (initialValues: Iterable<readonly [At
|
|
|
251
283
|
* import { useAtomRef } from "effect-atom-svelte";
|
|
252
284
|
* import { profile } from "./refs.ts";
|
|
253
285
|
*
|
|
254
|
-
* const
|
|
255
|
-
* // In the markup: {
|
|
286
|
+
* const user = useAtomRef(profile);
|
|
287
|
+
* // In the markup: {user.current.name}
|
|
256
288
|
* ```
|
|
257
289
|
*
|
|
258
290
|
* @stability unstable
|
|
@@ -309,7 +341,10 @@ export declare const useAtomRefPropValue: <A, K extends keyof A>(input: AtomInpu
|
|
|
309
341
|
* @category models
|
|
310
342
|
*/
|
|
311
343
|
export interface ResultOptions {
|
|
312
|
-
/**
|
|
344
|
+
/**
|
|
345
|
+
* Treat a refreshing result as pending, so the await waits for the refresh instead of resolving
|
|
346
|
+
* with the current value.
|
|
347
|
+
*/
|
|
313
348
|
readonly suspendOnWaiting?: boolean | undefined;
|
|
314
349
|
/**
|
|
315
350
|
* Run a server-rendered atom again once the page has hydrated. Overrides `RegistryProvider`'s
|
|
@@ -319,11 +354,18 @@ export interface ResultOptions {
|
|
|
319
354
|
}
|
|
320
355
|
/**
|
|
321
356
|
* Awaits an async atom's first result, then returns a live handle to its `AsyncResult`. Use it as
|
|
322
|
-
* a top-level `await` in a component script; server rendering waits for it
|
|
323
|
-
*
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
*
|
|
357
|
+
* a top-level `await` in a component script; server rendering waits for it. With a serialization
|
|
358
|
+
* key the browser starts from the server's result instead of running the atom again, unless
|
|
359
|
+
* `revalidateOnHydrate` is set, the server's result was still `waiting`, or the atom is a
|
|
360
|
+
* `staleTime` (`Atom.swr`) query that is stale or failed with no earlier success.
|
|
361
|
+
*
|
|
362
|
+
* Call it before the script's first `await` (for several atoms, in one `Promise.all`); an atom
|
|
363
|
+
* read after that `await` runs again in the browser. See
|
|
364
|
+
* [Call hooks before the first await](https://atom.jarrednorris.dev/hydration#call-hooks-before-the-first-await).
|
|
365
|
+
*
|
|
366
|
+
* With a getter, only the first atom is awaited: when the getter picks another atom the handle
|
|
367
|
+
* follows it, starting from that atom's current result (often `Initial`), and the component's
|
|
368
|
+
* await does not run again. On the server, an atom with a `withServerValue` override
|
|
327
369
|
* reads as that value and is never computed. An atom that has not started, whose result is
|
|
328
370
|
* `Initial` with nothing running, as an `Atom.fn` nothing has called, keeps the await pending in the
|
|
329
371
|
* browser until something writes it; on the server, where nothing will during the render, the
|
|
@@ -354,12 +396,15 @@ export declare const useAtomResult: <A, E>(input: AtomInput<Atom.Atom<AsyncResul
|
|
|
354
396
|
*/
|
|
355
397
|
export interface SuspenseOptions {
|
|
356
398
|
/**
|
|
357
|
-
* Treat a refreshing result as pending, so
|
|
358
|
-
*
|
|
399
|
+
* Treat a refreshing result as pending, so the await waits for the refresh instead of resolving
|
|
400
|
+
* with the current value. The boundary keeps showing its content meanwhile; use
|
|
359
401
|
* `$effect.pending()` to show progress.
|
|
360
402
|
*/
|
|
361
403
|
readonly suspendOnWaiting?: boolean | undefined;
|
|
362
|
-
/**
|
|
404
|
+
/**
|
|
405
|
+
* Resolve with the whole `Success` or `Failure` result instead of the value, rather than
|
|
406
|
+
* rejecting when the atom fails.
|
|
407
|
+
*/
|
|
363
408
|
readonly includeFailure?: boolean | undefined;
|
|
364
409
|
/**
|
|
365
410
|
* Run a server-rendered atom again once the page has hydrated. Overrides `RegistryProvider`'s
|
|
@@ -370,14 +415,26 @@ export interface SuspenseOptions {
|
|
|
370
415
|
/**
|
|
371
416
|
* Exposes an async atom as a promise for `await` in markup or `$derived(await ...)`. The promise is
|
|
372
417
|
* stable while the result is unchanged, and a new one is issued when the result changes, so Svelte
|
|
373
|
-
* re-runs dependents only on real updates.
|
|
374
|
-
*
|
|
375
|
-
*
|
|
376
|
-
*
|
|
377
|
-
*
|
|
378
|
-
*
|
|
379
|
-
*
|
|
380
|
-
*
|
|
418
|
+
* re-runs dependents only on real updates. With a serialization key the browser starts from the
|
|
419
|
+
* server's result instead of running the atom again, unless `revalidateOnHydrate` is set, the
|
|
420
|
+
* server's result was still `waiting`, or the atom is a `staleTime` (`Atom.swr`) query that is
|
|
421
|
+
* stale or failed with no earlier success.
|
|
422
|
+
*
|
|
423
|
+
* Call it before the script's first `await`; an atom read after that `await` runs again in the
|
|
424
|
+
* browser. See
|
|
425
|
+
* [Call hooks before the first await](https://atom.jarrednorris.dev/hydration#call-hooks-before-the-first-await).
|
|
426
|
+
*
|
|
427
|
+
* - **A new promise.** Unchanged means the same atom and the same result object: a refresh issues
|
|
428
|
+
* a new promise even if its value is equal, and so does a read after every reader of a pending
|
|
429
|
+
* promise went away, as that wait was interrupted.
|
|
430
|
+
* - **Failures.** The promise rejects with `Cause.squash` of the cause, or resolves with the
|
|
431
|
+
* `Failure` when `includeFailure` is set.
|
|
432
|
+
* - **On the server.** An atom with a `withServerValue` override resolves from that value and is
|
|
433
|
+
* never computed; if the value is `Initial`, the promise rejects, so read it inside a
|
|
434
|
+
* `<svelte:boundary>` with a `pending` snippet.
|
|
435
|
+
* - **An atom nothing has started.** One whose result is `Initial` with nothing running, as an
|
|
436
|
+
* `Atom.fn` nothing has called, stays pending in the browser until something writes it, and
|
|
437
|
+
* rejects on the server.
|
|
381
438
|
*
|
|
382
439
|
* **Example** (Awaiting an atom in the markup)
|
|
383
440
|
*
|
|
@@ -447,7 +504,6 @@ export declare function useAtomSuspense<A, E>(input: AtomInput<Atom.Atom<AsyncRe
|
|
|
447
504
|
* </form>
|
|
448
505
|
* ```
|
|
449
506
|
*
|
|
450
|
-
* @see {@link https://atom.jarrednorris.dev/forms | Forms}
|
|
451
507
|
* @stability unstable
|
|
452
508
|
* @since 0.4.0
|
|
453
509
|
* @category hooks
|