brumate 0.6.2 → 0.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.
Files changed (96) hide show
  1. package/check.d.ts +3 -3
  2. package/dist/{boot-DkbcqdUz.d.ts → boot-CMy0f_iC.d.ts} +2 -2
  3. package/dist/cli.js +77 -6
  4. package/dist/cli.js.map +1 -1
  5. package/dist/{client-C3vRNuLi.js → client-Bm0XbjHr.js} +274 -113
  6. package/dist/client-Bm0XbjHr.js.map +1 -0
  7. package/dist/{client-CqGDbWd_.d.ts → client-DnFF9_lv.d.ts} +40 -14
  8. package/dist/{client-routes-BtGpYCef.js → client-routes-BmPvNr4s.js} +10 -1
  9. package/dist/client-routes-BmPvNr4s.js.map +1 -0
  10. package/dist/{context-btHuB01Z.d.ts → context-BNgBXVUQ.d.ts} +7 -5
  11. package/dist/{context-CTiSzQRc.js → context-CU4Fpn5a.js} +10 -13
  12. package/dist/context-CU4Fpn5a.js.map +1 -0
  13. package/dist/{defer-M18UKyfG.js → defer-BWwxv7Qf.js} +2 -2
  14. package/dist/{defer-M18UKyfG.js.map → defer-BWwxv7Qf.js.map} +1 -1
  15. package/dist/{devtools-D9Q7a41M.js → devtools-Cl6AhENB.js} +4 -4
  16. package/dist/{devtools-D9Q7a41M.js.map → devtools-Cl6AhENB.js.map} +1 -1
  17. package/dist/{devtools-panel-DTQtljeA.js → devtools-panel-DLIeWcAj.js} +32 -9
  18. package/dist/devtools-panel-DLIeWcAj.js.map +1 -0
  19. package/dist/events-LgLdp1R7.js.map +1 -1
  20. package/dist/{features-CbgII-o7.js → features-DwMYKBIC.js} +6 -2
  21. package/dist/features-DwMYKBIC.js.map +1 -0
  22. package/dist/{files-DtDH5AR4.js → files-BuQvpBwF.js} +94 -3
  23. package/dist/files-BuQvpBwF.js.map +1 -0
  24. package/dist/{html-t1JpkODI.js → html-Cf5uKhLs.js} +3 -3
  25. package/dist/html-Cf5uKhLs.js.map +1 -0
  26. package/dist/index.d.ts +44 -0
  27. package/dist/index.js +3 -3
  28. package/dist/index.js.map +1 -1
  29. package/dist/{inspect-Bonq6EJO.js → inspect-BSHv1_25.js} +2739 -2080
  30. package/dist/inspect-BSHv1_25.js.map +1 -0
  31. package/dist/{loader-C-S3yzep.js → loader-BPNSRA2A.js} +5 -5
  32. package/dist/{loader-C-S3yzep.js.map → loader-BPNSRA2A.js.map} +1 -1
  33. package/dist/{rpc-C_znDZ_w.js → rpc-DqfCaYgF.js} +3 -3
  34. package/dist/{rpc-C_znDZ_w.js.map → rpc-DqfCaYgF.js.map} +1 -1
  35. package/dist/runtime/bundles.d.ts +1 -1
  36. package/dist/runtime/bundles.js +2 -0
  37. package/dist/runtime/bundles.js.map +1 -1
  38. package/dist/runtime/client.d.ts +4 -4
  39. package/dist/runtime/client.js +5 -5
  40. package/dist/runtime/context.d.ts +2 -2
  41. package/dist/runtime/context.js +1 -1
  42. package/dist/runtime/devtools.d.ts +2 -2
  43. package/dist/runtime/devtools.js +1 -1
  44. package/dist/runtime/events.d.ts +1 -1
  45. package/dist/runtime/hot.d.ts +1 -1
  46. package/dist/runtime/html.d.ts +1 -1
  47. package/dist/runtime/html.js +1 -1
  48. package/dist/runtime/loader.d.ts +2 -2
  49. package/dist/runtime/loader.js +2 -2
  50. package/dist/runtime/server.d.ts +4 -4
  51. package/dist/runtime/server.js +5 -5
  52. package/dist/runtime/signals.d.ts +2 -2
  53. package/dist/runtime/signals.js +43 -5
  54. package/dist/runtime/signals.js.map +1 -1
  55. package/dist/runtime/static-loader.d.ts +2 -2
  56. package/dist/runtime/static-loader.js +1 -1
  57. package/dist/runtime/store.d.ts +2 -0
  58. package/dist/runtime/store.js +1 -0
  59. package/dist/runtime/testing.d.ts +2 -2
  60. package/dist/runtime/testing.js +2 -2
  61. package/dist/{search-params-CM1RWR2P.js → search-params-C64liRBj.js} +34 -9
  62. package/dist/search-params-C64liRBj.js.map +1 -0
  63. package/dist/{server-dP_ucRiJ.d.ts → server-0rS5vUW1.d.ts} +37 -10
  64. package/dist/{server-BcT9icQ4.js → server-BVObWh-w.js} +132 -16
  65. package/dist/server-BVObWh-w.js.map +1 -0
  66. package/dist/{shared-CzbnWkVA.d.ts → shared-CuJdWifz.d.ts} +16 -3
  67. package/dist/{signals-BwD4u22K.d.ts → signals-DBnq9QRW.d.ts} +46 -6
  68. package/dist/{static-loader-DysAET-w.d.ts → static-loader-BYbXVJDY.d.ts} +2 -2
  69. package/dist/{static-loader-Bt2WDkY1.js → static-loader-V4V1Oy6G.js} +13 -5
  70. package/dist/static-loader-V4V1Oy6G.js.map +1 -0
  71. package/dist/store-BIBAYZo-.js +293 -0
  72. package/dist/store-BIBAYZo-.js.map +1 -0
  73. package/dist/store-xlTzAM_r.d.ts +171 -0
  74. package/dist/{stream-DRjx0keF.js → stream-RXLadXx1.js} +90 -4
  75. package/dist/stream-RXLadXx1.js.map +1 -0
  76. package/dist/vite.d.ts +1 -1
  77. package/dist/vite.js +128 -13
  78. package/dist/vite.js.map +1 -1
  79. package/elements.d.ts +349 -79
  80. package/package.json +5 -1
  81. package/skills/brumate/SKILL.md +1 -1
  82. package/skills/brumate/references/errors.md +127 -9
  83. package/skills/brumate/references/signals.md +37 -30
  84. package/skills/brumate/references/state.md +3 -2
  85. package/dist/client-C3vRNuLi.js.map +0 -1
  86. package/dist/client-routes-BtGpYCef.js.map +0 -1
  87. package/dist/context-CTiSzQRc.js.map +0 -1
  88. package/dist/devtools-panel-DTQtljeA.js.map +0 -1
  89. package/dist/features-CbgII-o7.js.map +0 -1
  90. package/dist/files-DtDH5AR4.js.map +0 -1
  91. package/dist/html-t1JpkODI.js.map +0 -1
  92. package/dist/inspect-Bonq6EJO.js.map +0 -1
  93. package/dist/search-params-CM1RWR2P.js.map +0 -1
  94. package/dist/server-BcT9icQ4.js.map +0 -1
  95. package/dist/static-loader-Bt2WDkY1.js.map +0 -1
  96. package/dist/stream-DRjx0keF.js.map +0 -1
@@ -38,7 +38,7 @@ A `.brm` file is a component: scripts for its state, an optional `<style>`, and
38
38
  - Markup is never a value in code or in an attribute. Use `#template name(parameters) { … }` and call it as `{name(arguments)}`.
39
39
  - Browser wrappers of a handler come from `brumate/events`: `prevent`, `stop`, `once`, `capture`, `transition`. Write them in the attribute, with keys as string literals. `prevent(handler, 'Enter')` prevents only Enter with Ctrl, Meta and Alt up. `'Mod+ArrowDown'` names Ctrl or Meta. Calling `event.preventDefault()` in a handler is too late.
40
40
  - A server function is an `async` function with `'use server'` on its first line. It reads no variable of the instance script. Pass values as arguments.
41
- - Do not use `$ref`, `$watch`, `$settled`, `$cleanup`, `$store`, `$get`, `$set`, or modifiers such as `onSubmit:prevent`.
41
+ - Do not use `$ref`, `$watch`, `$settled`, `$cleanup`, `$get`, `$set`, or modifiers such as `onSubmit:prevent`.
42
42
 
43
43
  ## References
44
44
 
@@ -1051,14 +1051,15 @@ https://brumate-docs.vercel.app/docs/errors#brm079
1051
1051
  const theme = $getContext(ThemeContext);
1052
1052
  ```
1053
1053
 
1054
- ## BRM080: $getContext takes one argument, the context
1054
+ ## BRM080: $getContext takes the context, and a fallback for a read without a provider
1055
1055
 
1056
- `$getContext` has no argument or more than one.
1056
+ `$getContext` has no argument or more than two.
1057
1057
 
1058
1058
  https://brumate-docs.vercel.app/docs/errors#brm080
1059
1059
 
1060
1060
  ```brm fragment
1061
1061
  const theme = $getContext(ThemeContext);
1062
+ const locale = $getContext(LocaleContext, undefined);
1062
1063
  ```
1063
1064
 
1064
1065
  ## BRM081: $setContext takes two arguments, the context and the value
@@ -1349,9 +1350,9 @@ let { open } = $props<{ open?: Bindable<boolean> }>();
1349
1350
  const bound = () => $isBound(open);
1350
1351
  ```
1351
1352
 
1352
- ## BRM108: $isPending needs an async $memo as its only argument
1353
+ ## BRM108: $isPending needs async $memos as its arguments
1353
1354
 
1354
- The argument of `$isPending` is not a memo that holds a promise.
1355
+ An argument of `$isPending` is not a memo that holds a promise, or the call has none.
1355
1356
 
1356
1357
  https://brumate-docs.vercel.app/docs/errors#brm108
1357
1358
 
@@ -1430,7 +1431,7 @@ let { ref } = $props<{ ref?: Bindable<HTMLButtonElement> }>();
1430
1431
 
1431
1432
  ## BRM115: "{name}" is the props object, so read one key at a time, destructure it, or spread it in markup
1432
1433
 
1433
- The props object or the rest of `$props()` is used as a whole outside a markup spread: spread in code, passed to a function, `$memo`, `$setContext`, a signal or a variable, or given as a value in markup, such as `title=rest` or `<Badge {rest} />`. The copy loses the `ref` and the live props.
1434
+ The props object or the rest of `$props()` is used as a whole outside a markup spread: spread in code, passed to a function, `$memo`, `$setContext`, a signal or a variable, or given as a value in markup, such as `title=rest` or `<Badge {rest} />`. The copy loses the `ref` and the live props. A spread may hold the rest as a branch of `?:`, `&&`, `||` or `??` next to an object literal, `undefined`, `null` or `false`, such as `{...(open ? rest : {})}`.
1434
1435
 
1435
1436
  https://brumate-docs.vercel.app/docs/errors#brm115
1436
1437
 
@@ -1615,6 +1616,36 @@ https://brumate-docs.vercel.app/docs/errors#brm132
1615
1616
  const double = $memo(count * 2);
1616
1617
  ```
1617
1618
 
1619
+ ## BRM202: $gate takes a value and a function, as in $gate(value, (prev, next) => next > prev)
1620
+
1621
+ `$gate` does not have a value and a guard function as its two arguments. The guard is an arrow function or a function expression.
1622
+
1623
+ https://brumate-docs.vercel.app/docs/errors#brm202
1624
+
1625
+ ```brm fragment
1626
+ const query = $gate(input, (_, next) => next.length >= 2);
1627
+ ```
1628
+
1629
+ ## BRM203: $gate takes a value that is ready. Use $memo for a promise or a server function
1630
+
1631
+ The value of `$gate` awaits or calls a server function. The compiler catches these two; any other promise or stream, such as a call of a local async function, throws when the value is computed. A gate holds a value that the guard can compare, so it does not wait for one.
1632
+
1633
+ https://brumate-docs.vercel.app/docs/errors#brm203
1634
+
1635
+ ```brm fragment
1636
+ const query = $gate(input, (_, next) => next.length >= 2);
1637
+ ```
1638
+
1639
+ ## BRM204: Declare a gated value with "const name = $gate(value, (prev, next) => boolean)" at the top level of a script
1640
+
1641
+ `$gate` is inside a function or a block of code.
1642
+
1643
+ https://brumate-docs.vercel.app/docs/errors#brm204
1644
+
1645
+ ```brm fragment
1646
+ const query = $gate(input, (_, next) => next.length >= 2);
1647
+ ```
1648
+
1618
1649
  ## BRM133: Declare a debounced value with "const name = $debounced(value, ms)" at the top level of a script
1619
1650
 
1620
1651
  `$debounced` is inside a function or a block of code.
@@ -1727,14 +1758,101 @@ $effect(() => {
1727
1758
  });
1728
1759
  ```
1729
1760
 
1730
- ## BRM143: $store is not supported yet
1761
+ ## BRM191: Read a store with "const name = $store(source)" or "let name = $store(source)" at the top level of a script
1762
+
1763
+ `$store` is not the whole value of a `const` or `let` at the top level of a script.
1764
+
1765
+ https://brumate-docs.vercel.app/docs/errors#brm191
1766
+
1767
+ ```brm fragment
1768
+ import { theme } from './theme';
1769
+ const value = $store(theme);
1770
+ ```
1771
+
1772
+ ## BRM192: $store takes one argument, a store
1773
+
1774
+ `$store` has no argument, or more than one.
1775
+
1776
+ https://brumate-docs.vercel.app/docs/errors#brm192
1777
+
1778
+ ```brm fragment
1779
+ const field = $getContext(Field);
1780
+ const state = $store(field.state);
1781
+ ```
1782
+
1783
+ ## BRM193: $toStore takes one argument, a memo, a signal or a prop, as a value of $setContext
1784
+
1785
+ `$toStore` has no argument or more than one, or it is not a value of `$setContext`, alone or as a key of an object or an item of a list.
1786
+
1787
+ https://brumate-docs.vercel.app/docs/errors#brm193
1788
+
1789
+ ```brm fragment
1790
+ $setContext(Field, { state: $toStore(state), pick });
1791
+ ```
1792
+
1793
+ ## BRM195: $store gives one name, not a pattern such as { a }
1794
+
1795
+ `$store` initializes a pattern.
1796
+
1797
+ https://brumate-docs.vercel.app/docs/errors#brm195
1798
+
1799
+ ```brm fragment
1800
+ const value = $store(source);
1801
+ ```
1802
+
1803
+ ## BRM196: The context value reads "{name}", which changes, but a context value is read once. Put it in a store with $toStore, or read it inside a function
1804
+
1805
+ The value of `$setContext` reads a signal, a memo, a prop or a `$bind` value that changes in the browser, and a context is read once, so readers would keep the first value. A call or an immediately invoked function in the value that reads such a name is not reported, and freezes the same way.
1806
+
1807
+ https://brumate-docs.vercel.app/docs/errors#brm196
1808
+
1809
+ ```brm fragment
1810
+ $setContext(Field, { state: $toStore(state), pick: () => value });
1811
+ ```
1812
+
1813
+ ## BRM197: Read a context that may have no provider with "const name = $getContext(Context, undefined)", and a store in it with "const state = name ? $store(name.key) : undefined", with const and no other test
1814
+
1815
+ A read of a context that may have no provider has another shape: `$hasContext`, which is removed, `$store(name?.key)`, a `let` or another test in the guard, an `else` that is not `undefined`, a guard on a name that is not the optional value of `$getContext(Context, undefined)`, or a key of that value read without the guard.
1816
+
1817
+ https://brumate-docs.vercel.app/docs/errors#brm197
1818
+
1819
+ ```brm fragment
1820
+ const field = $getContext(Field, undefined);
1821
+ const state = field ? $store(field.state) : undefined;
1822
+ ```
1823
+
1824
+ ## BRM198: The source of $store gives its store at once, so it cannot await or call a server function
1825
+
1826
+ The source of `$store` awaits, or calls a server function. The browser runs the source again when the page resumes, and links the store in the same pass, so the source must give the store, not a promise of it.
1827
+
1828
+ https://brumate-docs.vercel.app/docs/errors#brm198
1829
+
1830
+ ```brm fragment
1831
+ import { theme } from './theme';
1832
+ const value = $store(theme);
1833
+ ```
1834
+
1835
+ ## BRM199: $store takes a store. Read the store of a context with "$store($getContext(Context))", or give a store as the fallback, "$store($getContext(Context, store(value)))"
1836
+
1837
+ `$store` reads a context that the file names in `$getContext` or `$setContext`, `undefined`, `null`, or `$getContext` with a literal fallback, which is no store. A context that the file only imports is found when the page renders, as a `TypeError`.
1838
+
1839
+ https://brumate-docs.vercel.app/docs/errors#brm199
1840
+
1841
+ ```brm fragment
1842
+ const level = $store($getContext(LevelContext));
1843
+ const locale = $store($getContext(LocaleContext, store('en-US')));
1844
+ ```
1845
+
1846
+ ## BRM200: The value of bridge() must be a module-level export of a script file or of <script module>, as "export const name = bridge({ … })"
1731
1847
 
1732
- `$store` does not exist.
1848
+ A call of `bridge` from `brumate/store` is in the script of a component, or in a function, or is not the value of an exported `const`. The page state names a bridge by its module and its export, so the browser can load it.
1733
1849
 
1734
- https://brumate-docs.vercel.app/docs/errors#brm143
1850
+ https://brumate-docs.vercel.app/docs/errors#brm200
1735
1851
 
1736
1852
  ```brm fragment
1737
- let todos = $signal([]);
1853
+ // bears.ts, or the <script module> of a component
1854
+ import { bridge } from 'brumate/store';
1855
+ export const bearStore = bridge({ create, get, subscribe });
1738
1856
  ```
1739
1857
 
1740
1858
  ## BRM144: $get is not supported. Signals cannot leave the file
@@ -4,41 +4,45 @@ The compiler replaces each call and removes the import, so none of them ship. Im
4
4
 
5
5
  "Top level of a script" means a statement of the component's `<script>`, or of a `<script>` at the start of a block body. It is not inside a function or another statement.
6
6
 
7
- | Function | Returns | Where |
8
- | ------------------------------ | -------------------------- | ----------------------------------------------------- |
9
- | `$props<T>()` | `T` | `const` or `let` in the component script |
10
- | `$signal<T>(value?)` | `T` | `let` at the top level of a script |
11
- | `$memo<T>(value)` | the value, never a promise | `const` at the top level of a script |
12
- | `$debounced(value, ms)` | the value, as a memo | `const` at the top level of a script |
13
- | `$throttled(value, ms)` | the value, as a memo | `const` at the top level of a script |
14
- | `$isPending(memo)` | `boolean` | Any expression |
15
- | `$resolve(memo, options?)` | `Promise<T>` | A handler, an effect or a `$client` callback |
16
- | `$isPassed(prop)` | `boolean` | `const` in the component script |
17
- | `$isBound(prop)` | `boolean` | Any expression |
18
- | `$effect(callback)` | `void` | Statement at the top level of a script |
19
- | `$client(callback, trigger?)` | `void` | Statement at the top level of a script |
20
- | `$untrack(value)` | the value | Any expression |
21
- | `$produce(signal, recipe)` | `void` | Where a signal can be written |
22
- | `$setContext(context, value)` | `void` | Statement at the top level of a script |
23
- | `$getContext(context)` | the value | `const` at the top level of a script |
24
- | `$route(path?)` | `{ url, search, params }` | `const` at the top level of a script |
25
- | `$searchParam(name, options?)` | the value of the parameter | `let` at the top level of a script |
26
- | `$navigation()` | `{ pending, from, to }` | `const` at the top level of a script |
27
- | `$id()` | `string` | `const` at the top level of a script |
28
- | `$title(value)` | `void` | Statement at the top level of a script |
29
- | `$action(fn)` | the result, or `undefined` | `const` at the top level of a script |
30
- | `$optimistic(source)` | the value of `source` | `let` at the top level of a script |
31
- | `$handler(fn, ...args)` | a handler | Any expression of a script or a module function |
32
- | `$bind(value, set?)` | a binding | The whole value of a prop, or of `value` or `checked` |
7
+ | Function | Returns | Where |
8
+ | --------------------------------- | -------------------------- | ----------------------------------------------------- |
9
+ | `$props<T>()` | `T` | `const` or `let` in the component script |
10
+ | `$signal<T>(value?)` | `T` | `let` at the top level of a script |
11
+ | `$memo<T>(value)` | the value, never a promise | `const` at the top level of a script |
12
+ | `$gate(value, guard)` | the value, as a memo | `const` at the top level of a script |
13
+ | `$debounced(value, ms)` | the value, as a memo | `const` at the top level of a script |
14
+ | `$throttled(value, ms)` | the value, as a memo | `const` at the top level of a script |
15
+ | `$isPending(...memos)` | `boolean` | Any expression |
16
+ | `$resolve(memo, options?)` | `Promise<T>` | A handler, an effect or a `$client` callback |
17
+ | `$isPassed(prop)` | `boolean` | `const` in the component script |
18
+ | `$isBound(prop)` | `boolean` | Any expression |
19
+ | `$effect(callback)` | `void` | Statement at the top level of a script |
20
+ | `$client(callback, trigger?)` | `void` | Statement at the top level of a script |
21
+ | `$untrack(value)` | the value | Any expression |
22
+ | `$produce(signal, recipe)` | `void` | Where a signal can be written |
23
+ | `$setContext(context, value)` | `void` | Statement at the top level of a script |
24
+ | `$getContext(context, fallback?)` | the value, or the fallback | `const` at the top level of a script |
25
+ | `$store(source)` | the value of the store | `const` or `let` at the top level of a script |
26
+ | `$toStore(value)` | a store | A value of `$setContext` |
27
+ | `$route(path?)` | `{ url, search, params }` | `const` at the top level of a script |
28
+ | `$searchParam(name, options?)` | the value of the parameter | `let` at the top level of a script |
29
+ | `$navigation()` | `{ pending, from, to }` | `const` at the top level of a script |
30
+ | `$id()` | `string` | `const` at the top level of a script |
31
+ | `$title(value)` | `void` | Statement at the top level of a script |
32
+ | `$action(fn)` | the result, or `undefined` | `const` at the top level of a script |
33
+ | `$optimistic(source)` | the value of `source` | `let` at the top level of a script |
34
+ | `$handler(fn, ...args)` | a handler | Any expression of a script or a module function |
35
+ | `$bind(value, set?)` | a binding | The whole value of a prop, or of `value` or `checked` |
33
36
 
34
37
  ## What each one does
35
38
 
36
39
  - `$props<T>()`: the props of the component. Destructure it: `const { title, size = 'm', ...rest } = $props<Props>()`. With `let`, the component writes the props, and `Bindable<T>` in the type marks one that a parent can bind. Not in a block script.
37
40
  - `$signal(value)`: writable state. Read it and assign to it as a variable. The start value is computed once, on the server.
38
41
  - `$memo(expression)`: a read-only value that follows the values it reads. It can hold a promise, such as a server function call, and gives the settled value.
42
+ - `$gate(value, (prev, next) => boolean)`: a memo that takes the new value only when the guard returns true, and keeps its value otherwise. The first value passes. The value cannot be a promise: `await` and server calls are compile errors, other promises throw.
39
43
  - `$debounced(value, ms)`: a memo that takes the value once it stops changing for `ms` milliseconds. `ms` is a number literal.
40
44
  - `$throttled(value, ms)`: a memo that takes the value at most once every `ms` milliseconds.
41
- - `$isPending(memo)`: true while a new value of the memo loads in the browser. Always false on the server.
45
+ - `$isPending(...memos)`: true while a new value of any of the memos loads in the browser. Always false on the server.
42
46
  - `$resolve(memo, { timeout, signal })`: waits until the memo is no longer pending, and gives its value. A handler that reads a memo that can lack a value uses `await $resolve(memo)` or checks `$isPending(memo)` first (BRM182).
43
47
  - `$isPassed(prop)`: true when the caller passed the prop, also as `undefined` or `null`. A default value does not count. The argument is a prop that the component destructures.
44
48
  - `$isBound(prop)`: true when the caller passed the prop with `$bind`, so a write changes the caller's value. The argument is a prop destructured with `let`.
@@ -46,8 +50,11 @@ The compiler replaces each call and removes the import, so none of them ship. Im
46
50
  - `$client(callback, trigger?)`: runs the callback in the browser once. The trigger is `'visible'` (default), `'idle'` or `'load'`. It can return a cleanup function.
47
51
  - `$untrack(value)`: reads a value, or calls a function, without depending on it.
48
52
  - `$produce(signal, recipe)`: changes a draft of an object or array in a signal, and writes the result as a new value. The recipe can return a new value instead.
49
- - `$setContext(context, value)`: gives a value of a context to the components inside. Create the context with `createContext` from `brumate/context`.
50
- - `$getContext(context)`: reads a context from the nearest component above that provides it, or its default.
53
+ - `$setContext(context, value)`: gives a value of a context to the components inside. Create the context with `createContext` from `brumate/context`. The value is read once. A signal, memo, prop or `$bind` value that changes, read outside a function, is BRM196: put it in a store with `$toStore`.
54
+ - `$getContext(context, fallback?)`: reads a context from the nearest component above that provides it, and throws when none does. With a second argument it gives the fallback instead: `$getContext(Field, undefined)` is a read that may have no provider. A literal fallback is passed as it is. Other code runs only without a provider, once per read, and again in the browser when it renders the component.
55
+ - `$store(source)`: binds a store, any value with `get()`, `subscribe(listener)` (returns a function that stops it) and optionally `set(value)`, to a variable. The source gives a store: a key of the value of `$getContext` such as `$store(field.state)`, `$store($getContext(Level))` for a context that holds a store, an import, a prop, a value of `<script module>`, a call or `store(0)`. A context, `undefined` and `null` are no store (BRM199), and a source that gives no store when the page renders throws a `TypeError`. For a context that may have no provider, guard on the value: `const field = $getContext(Field, undefined); const state = field ? $store(field.state) : undefined;`, with `const` and no other test (BRM197). When a constant default exists, give a store as the fallback: `const locale = $store($getContext(LocaleContext, store('en-US')))`. The store is made for each reader, only without a provider. The variable follows the store. With `let`, a write such as `x = next` or `x++` calls `set`, and a store without `set` throws a `TypeError` when the variable registers. On resume the browser runs a source that is not a context once, with the values the server sent (a signal changed before the reader's code loads is not seen), and a store it makes starts from its first value. A store in a prop, a script value or a context must come from `brumate/store`, or be a store of another library wrapped with `bridge()`, or the server render throws. A source that awaits is an error (BRM198).
56
+ - `$toStore(value)`: a store for a value of `$setContext`, from a memo, a signal or a prop, alone or as a key of an object. A store of a signal has a `set` that writes the signal. Every context is static, and its readers update through the stores it holds. Make a store of your own with `store(value)` from `brumate/store`.
57
+ - `bridge({ create, get, subscribe, set? })` from `brumate/store`: wraps a store of another library (zustand and the like) so it can cross the page state in a context or a prop. Keep its value in a module-level export of a script file or of `<script module>`, as `export const bearStore = bridge({ … })`, or the compiler reports BRM200. `create(snapshot)` makes the library's store: once for each call on the server, once in the browser at the first subscriber, with the data that `get(source)` gave on the server. `get` must give plain data the page can send. A provider calls `$setContext(BearContext, bearStore({ bears: 0 }))`, a reader binds `$store($getContext(BearContext))` and calls the library through `.source`, which is `undefined` in the browser until the store has a subscriber and its module has loaded (write `bear.source?.…`). Resume never calls `set`.
51
58
  - `$route(path?)`: the current route, as `{ url, search, params }`. The path is the route, such as `'/posts/[id]'`, and takes at most one argument.
52
59
  - `$searchParam(name, options?)`: a signal bound to one parameter of the query string. The server reads the URL, and a write in the browser changes it with `replaceState` (`{ history: 'push' }` for `pushState`) and asks the server nothing. Back and forward write the signal. Options: `default` (left out of the URL), `parse`, `serialize`, `history`, `server` (a write navigates). The name is a string literal.
53
60
  - `$navigation()`: the state of a navigation, as `{ pending, from, to }`. It takes no arguments.
@@ -60,4 +67,4 @@ The compiler replaces each call and removes the import, so none of them ship. Im
60
67
 
61
68
  ## Not supported
62
69
 
63
- Calling one of these is a compile error that names the replacement: `$ref` (use a `$signal` with `ref=$bind(name)`), `$watch` (use `$effect`), `$settled` (use `$client`), `$cleanup` (return a function from `$effect` or `$client`), `$store` (assign a new value to a `$signal`, or use `$produce`), `$get` and `$set` (signals cannot leave the file).
70
+ Calling one of these is a compile error that names the replacement: `$ref` (use a `$signal` with `ref=$bind(name)`), `$watch` (use `$effect`), `$settled` (use `$client`), `$cleanup` (return a function from `$effect` or `$client`), `$get` and `$set` (signals cannot leave the file), `$hasContext` (read with `$getContext(context, undefined)`, BRM197).
@@ -78,8 +78,9 @@
78
78
 
79
79
  ## Other derived values
80
80
 
81
+ - `const query = $gate(input, (_, next) => next.length >= 2)` takes a new value only when the guard returns true, and the readers do not run otherwise. The first value passes.
81
82
  - `const settled = $debounced(query, 300)` takes the value once it stops changing for 300 ms. `$throttled(value, ms)` takes it at most once in `ms`. The wait is a number literal.
82
- - `$isPending(memo)` is true while an async memo loads.
83
+ - `$isPending(memo)` is true while an async memo loads. `$isPending(a, b)` is true while any of them loads.
83
84
  - `let shown = $optimistic(source)` follows a memo, an action or a prop, and takes a value in the meantime.
84
85
  - `const field = $id()` is an ID that is the same on the server and in the browser.
85
86
 
@@ -232,4 +233,4 @@ A memo can hold a promise, such as the result of a server function. Markup and o
232
233
  <p>{parent}</p>
233
234
  ```
234
235
 
235
- `createContext` comes from `brumate/context`: `export const Theme = createContext<'light' | 'dark'>('light');` in a `.ts` file. A component reads the context of the nearest component above it, children included.
236
+ `createContext` comes from `brumate/context`: `export const Theme = createContext<'light' | 'dark'>('theme');` in a `.ts` file. It takes only a key and has no default. A component reads the context of the nearest component above it, children included, and `$getContext` throws when none provides it. Give a fallback for a read that may have no provider: `$getContext(Theme, 'light')`.