@hyperfixi/reactivity 2.4.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/LICENSE +20 -0
- package/README.md +137 -0
- package/dist/index.cjs +772 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +282 -0
- package/dist/index.d.ts +282 -0
- package/dist/index.js +744 -0
- package/dist/index.js.map +1 -0
- package/package.json +62 -0
- package/src/bind.ts +355 -0
- package/src/caret-var.test.ts +137 -0
- package/src/caret-var.ts +125 -0
- package/src/index.ts +132 -0
- package/src/integration.test.ts +585 -0
- package/src/live.ts +68 -0
- package/src/signals.test.ts +369 -0
- package/src/signals.ts +444 -0
- package/src/types.ts +46 -0
- package/src/when.ts +72 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 LokaScript Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# @hyperfixi/reactivity
|
|
2
|
+
|
|
3
|
+
Signal-based reactive features for [hyperfixi](https://github.com/codetalcott/hyperfixi). Adds four constructs from upstream `_hyperscript 0.9.90`:
|
|
4
|
+
|
|
5
|
+
| Construct | Purpose |
|
|
6
|
+
| ---------------------------------------------------- | -------------------------------------------------------------------------------------------------------- |
|
|
7
|
+
| `live [commandList] end` | Reactive block. Body re-runs whenever any tracked read changes. |
|
|
8
|
+
| `when <expr> [or <expr>]* changes [commandList] end` | Observer. Body fires when any watched expression's value changes (`Object.is` semantics). |
|
|
9
|
+
| `bind <var> [to\|and\|with] <element>` | Two-way DOM ⇄ var binding with auto-detected property by element type. |
|
|
10
|
+
| `^name [on <target>]` | DOM-scoped inherited variable. Reads walk up the parent chain; writes hit the nearest defining ancestor. |
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { createRuntime, installPlugin } from '@hyperfixi/core';
|
|
16
|
+
import { reactivityPlugin } from '@hyperfixi/reactivity';
|
|
17
|
+
|
|
18
|
+
const runtime = createRuntime();
|
|
19
|
+
installPlugin(runtime, reactivityPlugin);
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Re-installing is a no-op (the plugin guards on `parserExtensions.hasFeature('live')`), so it is safe to call from HMR boundaries.
|
|
23
|
+
|
|
24
|
+
## `live`
|
|
25
|
+
|
|
26
|
+
```hyperscript
|
|
27
|
+
live
|
|
28
|
+
set $total to $price * $quantity
|
|
29
|
+
end
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The body is one effect; its dependency set is the union of every read performed during execution. Writes to any tracked dependency schedule a re-run on the next microtask flush.
|
|
33
|
+
|
|
34
|
+
### `hx-live` bridge
|
|
35
|
+
|
|
36
|
+
When this plugin is installed, `@hyperfixi/core`'s htmx-compat layer also recognizes the htmx v4 `hx-live` attribute and translates it to a `live ... end` block. The attribute value is hyperscript syntax — not JavaScript like upstream htmx v4 — so it reads naturally and inherits hyperscript's multilingual support:
|
|
37
|
+
|
|
38
|
+
```html
|
|
39
|
+
<!-- English -->
|
|
40
|
+
<div hx-live="put $count into me"></div>
|
|
41
|
+
|
|
42
|
+
<!-- Japanese (within <section lang="ja"> once Phase 8 lands) -->
|
|
43
|
+
<div hx-live="$count を me に 入れる"></div>
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
If reactivity isn't installed at the moment an `hx-live` element is processed, a clear console error is logged and the element is skipped — other htmx attributes on the same element still wire up normally. The `hyperfixi-hx-v4.js` bundle (in `@hyperfixi/core/dist/`) auto-installs reactivity so this gate is invisible to users of that bundle.
|
|
47
|
+
|
|
48
|
+
## `when ... changes`
|
|
49
|
+
|
|
50
|
+
```hyperscript
|
|
51
|
+
when $message changes
|
|
52
|
+
put it into me
|
|
53
|
+
end
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Each watched expression becomes its own effect. The body runs with `it` and `result` bound to the new value. Multiple expressions can be observed in one feature using `or`:
|
|
57
|
+
|
|
58
|
+
```hyperscript
|
|
59
|
+
when $a or $b changes
|
|
60
|
+
log 'a or b changed'
|
|
61
|
+
end
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
**Init behavior**: the body fires once on initial evaluation if the watched expression has a non-null/non-undefined value, then on every subsequent change (`Object.is` semantics). If you need pure change-only semantics, gate the body on a sentinel:
|
|
65
|
+
|
|
66
|
+
```hyperscript
|
|
67
|
+
when $x changes
|
|
68
|
+
if not :primed
|
|
69
|
+
set :primed to true
|
|
70
|
+
exit
|
|
71
|
+
end
|
|
72
|
+
log 'x actually changed'
|
|
73
|
+
end
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## `bind`
|
|
77
|
+
|
|
78
|
+
```hyperscript
|
|
79
|
+
bind $greeting to me
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Two effects are created (registration order is load-bearing):
|
|
83
|
+
|
|
84
|
+
1. **DOM → var** — reads the auto-detected property, writes the var. DOM "wins" on init.
|
|
85
|
+
2. **var → DOM** — reads the var, writes the DOM property. Fires on programmatic var writes after init.
|
|
86
|
+
|
|
87
|
+
Auto-detected property by element type:
|
|
88
|
+
|
|
89
|
+
| Element | Property |
|
|
90
|
+
| ----------------------------------- | --------------- |
|
|
91
|
+
| `<input type="checkbox\|radio">` | `checked` |
|
|
92
|
+
| `<input type="number\|range">` | `valueAsNumber` |
|
|
93
|
+
| `<input>`, `<textarea>`, `<select>` | `value` |
|
|
94
|
+
| `[contenteditable="true"]` | `textContent` |
|
|
95
|
+
| Custom elements with own `value` | `value` |
|
|
96
|
+
|
|
97
|
+
### Explicit property
|
|
98
|
+
|
|
99
|
+
Bind to a specific property using either possessive or member-access syntax:
|
|
100
|
+
|
|
101
|
+
```hyperscript
|
|
102
|
+
bind $color to #picker's value -- possessive (preferred — reads in any language)
|
|
103
|
+
bind $color to #picker.value -- dot syntax (JS-style alternative)
|
|
104
|
+
bind $text to #div's textContent -- non-form properties: var→DOM only
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
For form-like elements (`<input>`, `<textarea>`, `<select>`, `[contenteditable=true]`) both directions work. For non-form elements (e.g., binding a `<div>`'s `textContent`), only var→DOM fires — there are no `input`/`change` events to drive DOM→var, so user mutations of the property via devtools won't propagate back. Enable [debug logging](#debug-logging) to see a one-time warning when this happens.
|
|
108
|
+
|
|
109
|
+
Chained property access (`bind $x to #el's dataset.value`) is not supported in v1 — only one level deep.
|
|
110
|
+
|
|
111
|
+
Both `$globals` and `:locals` are accepted on the var side. Local writes propagate through the localWriteHook keyed off `context.me`, so a `set :foo to ...` from any handler running on the same `me` element will update the bound DOM property.
|
|
112
|
+
|
|
113
|
+
## `^name` — DOM-scoped variables
|
|
114
|
+
|
|
115
|
+
```hyperscript
|
|
116
|
+
-- on a parent element
|
|
117
|
+
set ^count to 0
|
|
118
|
+
|
|
119
|
+
-- on a descendant button
|
|
120
|
+
on click increment ^count
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Reads walk up from `me` (or the element passed via `on <target>`), tracking each element visited so writes at any ancestor notify dependent effects. The walk stops at any `[dom-scope="isolated"]` boundary that doesn't itself define the var, allowing nested components to hold independent state.
|
|
124
|
+
|
|
125
|
+
## Debug logging
|
|
126
|
+
|
|
127
|
+
Set `localStorage.setItem('hyperfixi:debug', '*')` in the browser console to surface effect errors that would otherwise be swallowed. The reactive scheduler is intentionally tolerant of expression/handler exceptions to avoid breaking the microtask flush; debug mode logs them via `console.warn`.
|
|
128
|
+
|
|
129
|
+
## API exports
|
|
130
|
+
|
|
131
|
+
- `reactivityPlugin` (default export): the `HyperfixiPlugin` to pass to `installPlugin`.
|
|
132
|
+
- `reactive`: the singleton `Reactive` instance. Useful for explicit teardown via `reactive.stopElementEffects(el)`, or for direct caret-var read/write outside hyperscript code.
|
|
133
|
+
- Types: `CaretVarNode`, `LiveFeatureNode`, `WhenFeatureNode`, `BindFeatureNode`.
|
|
134
|
+
|
|
135
|
+
## License
|
|
136
|
+
|
|
137
|
+
MIT
|