@molecule/app-svelte 3.6.0 → 3.7.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 +62 -5
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +1 -0
- package/dist/hooks/useForm.d.ts +2 -2
- package/dist/hooks/useForm.d.ts.map +1 -1
- package/dist/hooks/useForm.js +1 -1
- package/dist/hooks/useOptimisticToggle.d.ts +67 -0
- package/dist/hooks/useOptimisticToggle.d.ts.map +1 -0
- package/dist/hooks/useOptimisticToggle.js +52 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-11-
|
|
6
|
+
Generated: 2026-11-16T14:06:38.893Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/app-svelte
|
|
@@ -343,9 +343,10 @@ interface FormController<T extends FormValuesConstraint<T> = Record<string, unkn
|
|
|
343
343
|
* record, never a merge of earlier records. With `keepDirtyValues`, edited
|
|
344
344
|
* fields retain their values while pristine fields take the loaded values.
|
|
345
345
|
* A changed non-null `id` still replaces the record and cancels a pending
|
|
346
|
-
* submit. Equal-content baselines cause no update.
|
|
346
|
+
* submit. Equal-content baselines cause no update. `keepOmittedValues` opts
|
|
347
|
+
* into incremental top-level field fills using the current clean baseline.
|
|
347
348
|
*/
|
|
348
|
-
updateValues(values: Partial<T>, resetOptions?:
|
|
349
|
+
updateValues(values: Partial<T>, resetOptions?: FormUpdateOptions): void
|
|
349
350
|
|
|
350
351
|
/**
|
|
351
352
|
* Handles form submission.
|
|
@@ -1444,7 +1445,7 @@ interface UseFormResult<T extends FormValuesConstraint<T>> {
|
|
|
1444
1445
|
/** Restores the baseline (optionally a new one) and clears touched, errors and submit state. */
|
|
1445
1446
|
reset: (values?: Partial<T>) => void
|
|
1446
1447
|
/** Applies externally loaded values without clearing submission state. */
|
|
1447
|
-
updateValues: (values: Partial<T>, resetOptions?:
|
|
1448
|
+
updateValues: (values: Partial<T>, resetOptions?: FormUpdateOptions) => void
|
|
1448
1449
|
/** Whole-form state tuple: edits mark only changed fields dirty; fills preserve them. */
|
|
1449
1450
|
asState(): readonly [
|
|
1450
1451
|
T,
|
|
@@ -1600,6 +1601,39 @@ interface UseOAuthConfig {
|
|
|
1600
1601
|
}
|
|
1601
1602
|
```
|
|
1602
1603
|
|
|
1604
|
+
#### `UseOptimisticToggleOptions`
|
|
1605
|
+
|
|
1606
|
+
Options for {@link useOptimisticToggle}.
|
|
1607
|
+
|
|
1608
|
+
```typescript
|
|
1609
|
+
interface UseOptimisticToggleOptions {
|
|
1610
|
+
/** The loaded server value. Mount the control only after its record exists. */
|
|
1611
|
+
value: boolean
|
|
1612
|
+
/** Saves the DESIRED value and returns the server-confirmed value; reject on failure. */
|
|
1613
|
+
commit: (desired: boolean) => Promise<boolean>
|
|
1614
|
+
/** Receives the newest failed save and the confirmed value restored by the controller. */
|
|
1615
|
+
onError?: (error: unknown, value: boolean) => void
|
|
1616
|
+
}
|
|
1617
|
+
```
|
|
1618
|
+
|
|
1619
|
+
#### `UseOptimisticToggleResult`
|
|
1620
|
+
|
|
1621
|
+
What {@link useOptimisticToggle} returns: the boolean to render as readable stores and
|
|
1622
|
+
an argument-free action (the same names React returns).
|
|
1623
|
+
|
|
1624
|
+
```typescript
|
|
1625
|
+
interface UseOptimisticToggleResult {
|
|
1626
|
+
/** The value to render. */
|
|
1627
|
+
value: Readable<boolean>
|
|
1628
|
+
/** Whether a save is running or waiting. */
|
|
1629
|
+
pending: Readable<boolean>
|
|
1630
|
+
/** The failure of the newest change. */
|
|
1631
|
+
error: Readable<unknown>
|
|
1632
|
+
/** Flips the value shown now and saves it; the same function for the component's life. */
|
|
1633
|
+
toggle: () => void
|
|
1634
|
+
}
|
|
1635
|
+
```
|
|
1636
|
+
|
|
1603
1637
|
#### `UseOptimisticValueResult`
|
|
1604
1638
|
|
|
1605
1639
|
What {@link useOptimisticValue} returns: the state to render as readable
|
|
@@ -3355,6 +3389,29 @@ function useOAuth(config?: MaybeReadable<UseOAuthConfig | undefined>): UseOAuthR
|
|
|
3355
3389
|
|
|
3356
3390
|
**Returns:** OAuth helpers: providers, getOAuthUrl, redirect, and loginViaPopup.
|
|
3357
3391
|
|
|
3392
|
+
#### `useOptimisticToggle(options)`
|
|
3393
|
+
|
|
3394
|
+
Owns the state and save ordering of a follow, favorite, star or subscription control
|
|
3395
|
+
while all markup stays in the app. Pass `toggle` straight to the click handler: it takes
|
|
3396
|
+
no argument and flips the value shown now. `commit(desired)` receives the value to SAVE,
|
|
3397
|
+
never the old one, and returns the server's boolean answer. Only the newest failure
|
|
3398
|
+
rolls the shown value back.
|
|
3399
|
+
|
|
3400
|
+
Saves run one at a time; clicks during a save coalesce to the newest desired value. Use
|
|
3401
|
+
`pending` for feedback, not to disable the control or drop a click. Mount it only after
|
|
3402
|
+
the record loads, key it by record identity, and keep it mounted through refetches. For
|
|
3403
|
+
a flag with a related count, use `useOptimisticValue` with both fields in one value.
|
|
3404
|
+
|
|
3405
|
+
```typescript
|
|
3406
|
+
function useOptimisticToggle(
|
|
3407
|
+
options: MaybeReadable<UseOptimisticToggleOptions>,
|
|
3408
|
+
): UseOptimisticToggleResult
|
|
3409
|
+
```
|
|
3410
|
+
|
|
3411
|
+
- `options` — The server value, the save and an optional error callback (or a readable store of them: a new `value` syncs in while nothing is saving).
|
|
3412
|
+
|
|
3413
|
+
**Returns:** The value, pending and error state as readable stores, and the stable `toggle`.
|
|
3414
|
+
|
|
3358
3415
|
#### `useOptimisticValue(options)`
|
|
3359
3416
|
|
|
3360
3417
|
A server-backed value — a favorite, follow, like or bookmark button, a
|
|
@@ -4698,7 +4755,7 @@ Peer dependencies:
|
|
|
4698
4755
|
- `@molecule/app-auth` ^3.0.0
|
|
4699
4756
|
- `@molecule/app-code-editor` ^1.0.1
|
|
4700
4757
|
- `@molecule/app-device` ^1.0.1
|
|
4701
|
-
- `@molecule/app-forms` ^2.
|
|
4758
|
+
- `@molecule/app-forms` ^2.4.0
|
|
4702
4759
|
- `@molecule/app-http` ^1.0.1
|
|
4703
4760
|
- `@molecule/app-i18n` ^1.0.1
|
|
4704
4761
|
- `@molecule/app-ide` ^1.0.1
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ export * from './useHttp.js';
|
|
|
19
19
|
export * from './useLogger.js';
|
|
20
20
|
export * from './useLogin.js';
|
|
21
21
|
export * from './useOAuth.js';
|
|
22
|
+
export * from './useOptimisticToggle.js';
|
|
22
23
|
export * from './useOptimisticValue.js';
|
|
23
24
|
export * from './usePasswordReset.js';
|
|
24
25
|
export * from './usePlatform.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,cAAc,qBAAqB,CAAA;AACnC,cAAc,kBAAkB,CAAA;AAChC,cAAc,oBAAoB,CAAA;AAClC,cAAc,cAAc,CAAA;AAC5B,cAAc,sBAAsB,CAAA;AACpC,cAAc,wBAAwB,CAAA;AACtC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,cAAc,CAAA;AAC5B,cAAc,cAAc,CAAA;AAC5B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,eAAe,CAAA;AAC7B,cAAc,eAAe,CAAA;AAC7B,cAAc,yBAAyB,CAAA;AACvC,cAAc,uBAAuB,CAAA;AACrC,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,qBAAqB,CAAA;AAC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7 @@ export * from './useHttp.js';
|
|
|
19
19
|
export * from './useLogger.js';
|
|
20
20
|
export * from './useLogin.js';
|
|
21
21
|
export * from './useOAuth.js';
|
|
22
|
+
export * from './useOptimisticToggle.js';
|
|
22
23
|
export * from './useOptimisticValue.js';
|
|
23
24
|
export * from './usePasswordReset.js';
|
|
24
25
|
export * from './usePlatform.js';
|
package/dist/hooks/useForm.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
6
|
import { type Readable } from 'svelte/store';
|
|
7
|
-
import type { FieldRegistration, FieldState, FormController, FormOptions, FormResetOptions, FormState, FormValuesConstraint, RegisterOptions } from '@molecule/app-forms';
|
|
7
|
+
import type { FieldRegistration, FieldState, FormController, FormOptions, FormResetOptions, FormState, FormUpdateOptions, FormValuesConstraint, RegisterOptions } from '@molecule/app-forms';
|
|
8
8
|
import { type MaybeReadable } from './maybe-readable.js';
|
|
9
9
|
/**
|
|
10
10
|
* Options for useForm hook.
|
|
@@ -59,7 +59,7 @@ export interface UseFormResult<T extends FormValuesConstraint<T>> {
|
|
|
59
59
|
/** Restores the baseline (optionally a new one) and clears touched, errors and submit state. */
|
|
60
60
|
reset: (values?: Partial<T>) => void;
|
|
61
61
|
/** Applies externally loaded values without clearing submission state. */
|
|
62
|
-
updateValues: (values: Partial<T>, resetOptions?:
|
|
62
|
+
updateValues: (values: Partial<T>, resetOptions?: FormUpdateOptions) => void;
|
|
63
63
|
/** Whole-form state tuple: edits mark only changed fields dirty; fills preserve them. */
|
|
64
64
|
asState(): readonly [
|
|
65
65
|
T,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useForm.d.ts","sourceRoot":"","sources":["../../src/hooks/useForm.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAgB,KAAK,QAAQ,EAAY,MAAM,cAAc,CAAA;AAEpE,OAAO,KAAK,EACV,iBAAiB,EACjB,UAAU,EACV,cAAc,EACd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M,CAAC,CAAC,CAAC,CAAC,CAUlC"}
|
package/dist/hooks/useForm.js
CHANGED
|
@@ -170,7 +170,7 @@ export function useForm(options = {}) {
|
|
|
170
170
|
const next = typeof value === 'function'
|
|
171
171
|
? value(read())
|
|
172
172
|
: value;
|
|
173
|
-
form.updateValues(name === undefined ? next : { [name]: next }, { keepDirtyValues: true });
|
|
173
|
+
form.updateValues(name === undefined ? next : { [name]: next }, { keepDirtyValues: true, keepOmittedValues: name !== undefined });
|
|
174
174
|
};
|
|
175
175
|
stateTuples.set(name, [edit, fill]);
|
|
176
176
|
return [visible, edit, fill];
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for a server-backed boolean toggle rendered with the app's own markup,
|
|
3
|
+
* porting React's `useOptimisticToggle`.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { type Readable } from 'svelte/store';
|
|
8
|
+
import { type MaybeReadable } from './maybe-readable.js';
|
|
9
|
+
/** Options for {@link useOptimisticToggle}. */
|
|
10
|
+
export interface UseOptimisticToggleOptions {
|
|
11
|
+
/** The loaded server value. Mount the control only after its record exists. */
|
|
12
|
+
value: boolean;
|
|
13
|
+
/** Saves the DESIRED value and returns the server-confirmed value; reject on failure. */
|
|
14
|
+
commit: (desired: boolean) => Promise<boolean>;
|
|
15
|
+
/** Receives the newest failed save and the confirmed value restored by the controller. */
|
|
16
|
+
onError?: (error: unknown, value: boolean) => void;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* What {@link useOptimisticToggle} returns: the boolean to render as readable stores and
|
|
20
|
+
* an argument-free action (the same names React returns).
|
|
21
|
+
*/
|
|
22
|
+
export interface UseOptimisticToggleResult {
|
|
23
|
+
/** The value to render. */
|
|
24
|
+
value: Readable<boolean>;
|
|
25
|
+
/** Whether a save is running or waiting. */
|
|
26
|
+
pending: Readable<boolean>;
|
|
27
|
+
/** The failure of the newest change. */
|
|
28
|
+
error: Readable<unknown>;
|
|
29
|
+
/** Flips the value shown now and saves it; the same function for the component's life. */
|
|
30
|
+
toggle: () => void;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Owns the state and save ordering of a follow, favorite, star or subscription control
|
|
34
|
+
* while all markup stays in the app. Pass `toggle` straight to the click handler: it takes
|
|
35
|
+
* no argument and flips the value shown now. `commit(desired)` receives the value to SAVE,
|
|
36
|
+
* never the old one, and returns the server's boolean answer. Only the newest failure
|
|
37
|
+
* rolls the shown value back.
|
|
38
|
+
*
|
|
39
|
+
* Saves run one at a time; clicks during a save coalesce to the newest desired value. Use
|
|
40
|
+
* `pending` for feedback, not to disable the control or drop a click. Mount it only after
|
|
41
|
+
* the record loads, key it by record identity, and keep it mounted through refetches. For
|
|
42
|
+
* a flag with a related count, use `useOptimisticValue` with both fields in one value.
|
|
43
|
+
*
|
|
44
|
+
* @param options - The server value, the save and an optional error callback (or a
|
|
45
|
+
* readable store of them: a new `value` syncs in while nothing is saving).
|
|
46
|
+
* @returns The value, pending and error state as readable stores, and the stable `toggle`.
|
|
47
|
+
* @example
|
|
48
|
+
* ```svelte
|
|
49
|
+
* <script lang="ts">
|
|
50
|
+
* const subscription = useOptimisticToggle({
|
|
51
|
+
* value: thread.subscribed,
|
|
52
|
+
* commit: async (desired) => {
|
|
53
|
+
* const path = `/threads/${encodeURIComponent(thread.id)}/subscription`
|
|
54
|
+
* const response = desired
|
|
55
|
+
* ? await http.put<{ subscribed: boolean }>(path)
|
|
56
|
+
* : await http.delete<{ subscribed: boolean }>(path)
|
|
57
|
+
* return response.data.subscribed
|
|
58
|
+
* },
|
|
59
|
+
* })
|
|
60
|
+
* const { value, pending } = subscription
|
|
61
|
+
* </script>
|
|
62
|
+
*
|
|
63
|
+
* <button type="button" aria-pressed={$value} aria-busy={$pending} onclick={subscription.toggle}>
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export declare const useOptimisticToggle: (options: MaybeReadable<UseOptimisticToggleOptions>) => UseOptimisticToggleResult;
|
|
67
|
+
//# sourceMappingURL=useOptimisticToggle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useOptimisticToggle.d.ts","sourceRoot":"","sources":["../../src/hooks/useOptimisticToggle.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAW,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAA;AAErD,OAAO,EAAE,KAAK,aAAa,EAAc,MAAM,qBAAqB,CAAA;AAGpE,+CAA+C;AAC/C,MAAM,WAAW,0BAA0B;IACzC,+EAA+E;IAC/E,KAAK,EAAE,OAAO,CAAA;IACd,yFAAyF;IACzF,MAAM,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAC9C,0FAA0F;IAC1F,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,KAAK,IAAI,CAAA;CACnD;AAED;;;GAGG;AACH,MAAM,WAAW,yBAAyB;IACxC,2BAA2B;IAC3B,KAAK,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAA;IACxB,4CAA4C;IAC5C,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAA;IAC1B,wCAAwC;IACxC,KAAK,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAA;IACxB,0FAA0F;IAC1F,MAAM,EAAE,MAAM,IAAI,CAAA;CACnB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,eAAO,MAAM,mBAAmB,GAC9B,SAAS,aAAa,CAAC,0BAA0B,CAAC,KACjD,yBAUF,CAAA"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte hook for a server-backed boolean toggle rendered with the app's own markup,
|
|
3
|
+
* porting React's `useOptimisticToggle`.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { derived } from 'svelte/store';
|
|
8
|
+
import { toReadable } from './maybe-readable.js';
|
|
9
|
+
import { useOptimisticValue } from './useOptimisticValue.js';
|
|
10
|
+
/**
|
|
11
|
+
* Owns the state and save ordering of a follow, favorite, star or subscription control
|
|
12
|
+
* while all markup stays in the app. Pass `toggle` straight to the click handler: it takes
|
|
13
|
+
* no argument and flips the value shown now. `commit(desired)` receives the value to SAVE,
|
|
14
|
+
* never the old one, and returns the server's boolean answer. Only the newest failure
|
|
15
|
+
* rolls the shown value back.
|
|
16
|
+
*
|
|
17
|
+
* Saves run one at a time; clicks during a save coalesce to the newest desired value. Use
|
|
18
|
+
* `pending` for feedback, not to disable the control or drop a click. Mount it only after
|
|
19
|
+
* the record loads, key it by record identity, and keep it mounted through refetches. For
|
|
20
|
+
* a flag with a related count, use `useOptimisticValue` with both fields in one value.
|
|
21
|
+
*
|
|
22
|
+
* @param options - The server value, the save and an optional error callback (or a
|
|
23
|
+
* readable store of them: a new `value` syncs in while nothing is saving).
|
|
24
|
+
* @returns The value, pending and error state as readable stores, and the stable `toggle`.
|
|
25
|
+
* @example
|
|
26
|
+
* ```svelte
|
|
27
|
+
* <script lang="ts">
|
|
28
|
+
* const subscription = useOptimisticToggle({
|
|
29
|
+
* value: thread.subscribed,
|
|
30
|
+
* commit: async (desired) => {
|
|
31
|
+
* const path = `/threads/${encodeURIComponent(thread.id)}/subscription`
|
|
32
|
+
* const response = desired
|
|
33
|
+
* ? await http.put<{ subscribed: boolean }>(path)
|
|
34
|
+
* : await http.delete<{ subscribed: boolean }>(path)
|
|
35
|
+
* return response.data.subscribed
|
|
36
|
+
* },
|
|
37
|
+
* })
|
|
38
|
+
* const { value, pending } = subscription
|
|
39
|
+
* </script>
|
|
40
|
+
*
|
|
41
|
+
* <button type="button" aria-pressed={$value} aria-busy={$pending} onclick={subscription.toggle}>
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export const useOptimisticToggle = (options) => {
|
|
45
|
+
// `commit` gets only the desired boolean, never the previous value or a confirm callback.
|
|
46
|
+
const { value, pending, error, update } = useOptimisticValue(derived(toReadable(options), (current) => ({
|
|
47
|
+
...current,
|
|
48
|
+
commit: (desired) => current.commit(desired),
|
|
49
|
+
})));
|
|
50
|
+
const toggle = () => update((current) => !current);
|
|
51
|
+
return { value, pending, error, toggle };
|
|
52
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/app-svelte",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.7.1",
|
|
4
4
|
"description": "Svelte framework bindings for molecule.dev",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"@molecule/app-auth": "^3.0.0",
|
|
39
39
|
"@molecule/app-code-editor": "^1.0.1",
|
|
40
40
|
"@molecule/app-device": "^1.0.1",
|
|
41
|
-
"@molecule/app-forms": "^2.
|
|
41
|
+
"@molecule/app-forms": "^2.4.0",
|
|
42
42
|
"@molecule/app-http": "^1.0.1",
|
|
43
43
|
"@molecule/app-i18n": "^1.0.1",
|
|
44
44
|
"@molecule/app-ide": "^1.0.1",
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
"@molecule/app-ai-chat": "1.8.0",
|
|
71
71
|
"@molecule/app-ai-models": "1.4.4",
|
|
72
72
|
"@molecule/app-framework-conformance": "0.0.0",
|
|
73
|
-
"@molecule/app-utilities": "1.0.
|
|
73
|
+
"@molecule/app-utilities": "1.0.7",
|
|
74
74
|
"@sveltejs/package": "3.0.2",
|
|
75
75
|
"@sveltejs/vite-plugin-svelte": "7.1.2",
|
|
76
76
|
"@types/node": "26.6.5",
|