@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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/signals.ts","../src/caret-var.ts","../src/live.ts","../src/when.ts","../src/bind.ts","../src/index.ts"],"sourcesContent":["/**\n * Signal/effect reactive core.\n *\n * Models three dependency kinds for v1:\n * - `global` — `$name` variables in `context.globals`\n * - `element` — `^name` DOM-scoped variables (per-element storage)\n * - `dom` — DOM properties read via `input`/`change` listeners\n *\n * Inspired by upstream _hyperscript 0.9.91's `src/core/runtime/reactivity.js`\n * but reimplemented in TypeScript against hyperfixi's types. Batched via\n * `queueMicrotask` so synchronous writes coalesce into a single flush. Cycle\n * detection halts at >100 consecutive triggers (matching upstream).\n *\n * External packages never see `Effect` or `Reactive` directly — they hold\n * the disposer returned by `createEffect()` and call it when the owning\n * element is cleaned up.\n */\n\nexport type DepKind = 'global' | 'element' | 'dom';\n\n/**\n * Whether reactive debug logging is enabled. Mirrors the core convention:\n * `localStorage.setItem('hyperfixi:debug', '*')` enables, anything else (or no\n * `localStorage`) disables. Cheap call — no caching, since users toggle this\n * interactively in the console.\n */\nfunction debugEnabled(): boolean {\n try {\n return typeof localStorage !== 'undefined' && localStorage.getItem('hyperfixi:debug') !== null;\n } catch {\n return false;\n }\n}\n\nfunction debugWarn(message: string, err: unknown): void {\n if (!debugEnabled()) return;\n if (typeof console === 'undefined') return;\n console.warn(`[@hyperfixi/reactivity] ${message}`, err);\n}\n\nexport interface Dep {\n readonly key: string;\n readonly kind: DepKind;\n readonly name: string;\n readonly element: Element | null;\n}\n\nconst globalDepKey = (name: string): string => `global:${name}`;\nconst elementDepKey = (el: Element, name: string): string => `element:${reactiveIdFor(el)}:${name}`;\nconst domDepKey = (el: Element, prop: string): string => `dom:${reactiveIdFor(el)}:${prop}`;\n\n// WeakMap-based id assignment so we don't monkey-patch DOM nodes. Ids are\n// per-process and only used to construct stable string keys for an effect's\n// per-effect dependency dedup map.\nconst _reactiveIds = new WeakMap<Element, number>();\nlet _idCounter = 0;\nfunction reactiveIdFor(el: Element): number {\n const existing = _reactiveIds.get(el);\n if (existing !== undefined) return existing;\n const id = ++_idCounter;\n _reactiveIds.set(el, id);\n return id;\n}\n\nexport class Effect {\n readonly dependencies = new Map<string, Dep>();\n private lastValue: unknown = undefined;\n private _stopped = false;\n private _consecutiveTriggers = 0;\n\n constructor(\n private readonly r: Reactive,\n readonly expression: () => unknown | Promise<unknown>,\n readonly handler: (value: unknown) => void | Promise<void>,\n readonly element: Element | null\n ) {}\n\n get stopped(): boolean {\n return this._stopped;\n }\n\n /**\n * Run the effect for the first time: collect dependencies, subscribe, call\n * handler with the initial value. Errors in expression evaluation are caught\n * and silently swallowed (matches upstream's tolerance).\n */\n async initialize(): Promise<void> {\n if (this._stopped) return;\n try {\n const value = await this.r._runWithEffect(this, this.expression);\n this.lastValue = value;\n if (value !== undefined && value !== null) {\n await this.handler(value);\n }\n } catch (err) {\n // Swallow — expression evaluation failed during initial read. Surface\n // via debug log so users diagnosing \"my effect never fires\" can see why.\n debugWarn('effect.initialize failed', err);\n }\n }\n\n /**\n * Re-run the effect after a notify. Cycle-detects (halts at 101 consecutive\n * triggers). If the new value is Object.is-equal to the last, the handler\n * is skipped. If the owning element has disconnected, the effect stops\n * itself and returns.\n */\n async run(): Promise<void> {\n if (this._stopped) return;\n if (this.element && !this.element.isConnected) {\n this.stop();\n return;\n }\n this._consecutiveTriggers++;\n if (this._consecutiveTriggers > 100) {\n if (typeof console !== 'undefined') {\n console.error(\n '[@hyperfixi/reactivity] Effect halted: > 100 consecutive triggers (cycle detected).'\n );\n }\n this.stop();\n return;\n }\n try {\n const value = await this.r._runWithEffect(this, this.expression);\n if (Object.is(value, this.lastValue)) return;\n this.lastValue = value;\n await this.handler(value);\n } catch (err) {\n // Swallow — expression/handler errors don't break the microtask flush.\n debugWarn('effect.run failed', err);\n }\n }\n\n resetTriggerCount(): void {\n this._consecutiveTriggers = 0;\n }\n\n /**\n * Stop the effect and remove it from all dependency subscriptions. Safe to\n * call multiple times.\n */\n stop(): void {\n if (this._stopped) return;\n this._stopped = true;\n this.r._unsubscribeEffect(this);\n this.dependencies.clear();\n }\n}\n\ninterface ElementState {\n symbolSubs: Map<string, Set<Effect>>;\n caretVars: Map<string, unknown>;\n domHandlers: Map<string, DomHandler>;\n effects: Set<Effect>;\n}\n\ninterface DomHandler {\n subs: Set<Effect>;\n detach: () => void;\n}\n\nexport class Reactive {\n private currentEffect: Effect | null = null;\n private pending = new Set<Effect>();\n private scheduled = false;\n\n private globalSubs = new Map<string, Set<Effect>>();\n private elementState = new WeakMap<Element, ElementState>();\n\n private getElementState(el: Element): ElementState {\n let s = this.elementState.get(el);\n if (!s) {\n s = {\n symbolSubs: new Map(),\n caretVars: new Map(),\n domHandlers: new Map(),\n effects: new Set(),\n };\n this.elementState.set(el, s);\n }\n return s;\n }\n\n /**\n * Internal helper invoked by Effect — installs `this` as the current effect,\n * runs the expression, then restores the previous current-effect pointer.\n * Track* methods (called from read-paths) consult `currentEffect` to know\n * which effect to subscribe.\n */\n async _runWithEffect(e: Effect, fn: () => unknown | Promise<unknown>): Promise<unknown> {\n const prev = this.currentEffect;\n this.currentEffect = e;\n // Unsubscribe from previous deps before re-running. The expression will\n // re-record whatever it actually reads this time, avoiding stale subs.\n this._unsubscribeEffect(e);\n e.dependencies.clear();\n try {\n return await fn();\n } finally {\n this.currentEffect = prev;\n }\n }\n\n // ---------------------------------------------------------------------------\n // Track (read-path) — invoked from interceptors / evaluators.\n // ---------------------------------------------------------------------------\n\n trackGlobal(name: string): void {\n const e = this.currentEffect;\n if (!e) return;\n const key = globalDepKey(name);\n if (e.dependencies.has(key)) return;\n e.dependencies.set(key, { key, kind: 'global', name, element: null });\n let subs = this.globalSubs.get(name);\n if (!subs) {\n subs = new Set();\n this.globalSubs.set(name, subs);\n }\n subs.add(e);\n }\n\n trackElement(el: Element, name: string): void {\n const e = this.currentEffect;\n if (!e) return;\n const key = elementDepKey(el, name);\n if (e.dependencies.has(key)) return;\n e.dependencies.set(key, { key, kind: 'element', name, element: el });\n const state = this.getElementState(el);\n let subs = state.symbolSubs.get(name);\n if (!subs) {\n subs = new Set();\n state.symbolSubs.set(name, subs);\n }\n subs.add(e);\n state.effects.add(e);\n }\n\n trackDomProperty(el: Element, prop: string): void {\n const e = this.currentEffect;\n if (!e) return;\n const key = domDepKey(el, prop);\n if (e.dependencies.has(key)) return;\n e.dependencies.set(key, { key, kind: 'dom', name: prop, element: el });\n const state = this.getElementState(el);\n let handler = state.domHandlers.get(prop);\n if (!handler) {\n const subs = new Set<Effect>();\n const listener = (): void => {\n for (const effect of subs) this.schedule(effect);\n };\n // Use both input and change to cover radios/selects which don't fire input.\n el.addEventListener('input', listener);\n el.addEventListener('change', listener);\n handler = {\n subs,\n detach: () => {\n el.removeEventListener('input', listener);\n el.removeEventListener('change', listener);\n },\n };\n state.domHandlers.set(prop, handler);\n }\n handler.subs.add(e);\n state.effects.add(e);\n }\n\n // ---------------------------------------------------------------------------\n // Notify (write-path) — schedules dependent effects for re-run.\n // ---------------------------------------------------------------------------\n\n notifyGlobal(name: string): void {\n const subs = this.globalSubs.get(name);\n if (!subs) return;\n for (const e of subs) this.schedule(e);\n }\n\n notifyElement(el: Element, name: string): void {\n const state = this.elementState.get(el);\n if (!state) return;\n const subs = state.symbolSubs.get(name);\n if (!subs) return;\n for (const e of subs) this.schedule(e);\n }\n\n // ---------------------------------------------------------------------------\n // Effect lifecycle.\n // ---------------------------------------------------------------------------\n\n /**\n * Create + initialize an effect. Returns a disposer that stops the effect.\n * Callers are expected to register the disposer with the core runtime's\n * cleanup registry so it fires on element removal.\n */\n createEffect(\n expression: () => unknown | Promise<unknown>,\n handler: (value: unknown) => void | Promise<void>,\n owner: Element | null\n ): () => void {\n const e = new Effect(this, expression, handler, owner);\n if (owner) this.getElementState(owner).effects.add(e);\n // Fire initialize asynchronously so the caller can set up cleanup registration\n // before the first run touches the DOM.\n queueMicrotask(() => {\n void e.initialize();\n });\n return () => e.stop();\n }\n\n /**\n * Stop all effects owned by an element. Called by the reactivity plugin's\n * cleanup hook registered via `runtime.getCleanupRegistry()`.\n */\n stopElementEffects(el: Element): void {\n const state = this.elementState.get(el);\n if (!state) return;\n for (const e of Array.from(state.effects)) e.stop();\n state.effects.clear();\n }\n\n /**\n * Internal: remove an effect's entries from all subscriber sets. Called by\n * `Effect.stop()`.\n */\n _unsubscribeEffect(e: Effect): void {\n for (const dep of e.dependencies.values()) {\n if (dep.kind === 'global') {\n const subs = this.globalSubs.get(dep.name);\n subs?.delete(e);\n } else if (dep.kind === 'element' && dep.element) {\n const state = this.elementState.get(dep.element);\n const subs = state?.symbolSubs.get(dep.name);\n subs?.delete(e);\n state?.effects.delete(e);\n } else if (dep.kind === 'dom' && dep.element) {\n const state = this.elementState.get(dep.element);\n const handler = state?.domHandlers.get(dep.name);\n handler?.subs.delete(e);\n if (handler && handler.subs.size === 0) {\n handler.detach();\n state?.domHandlers.delete(dep.name);\n }\n state?.effects.delete(e);\n }\n }\n }\n\n // ---------------------------------------------------------------------------\n // Caret-variable storage — `^name` reads/writes.\n // ---------------------------------------------------------------------------\n\n /**\n * Whether `el` is a `dom-scope=\"isolated\"` boundary. Walks of `^var`\n * lookups stop at boundary elements that don't define the var, so nested\n * components don't accidentally read or write each other's state.\n */\n private isIsolationBoundary(el: Element): boolean {\n return typeof el.getAttribute === 'function' && el.getAttribute('dom-scope') === 'isolated';\n }\n\n /**\n * Walk up the DOM tree from `lookupRoot`, returning the first element whose\n * state has `name` defined. Stops at any `dom-scope=\"isolated\"` boundary\n * that doesn't itself define the var. Returns `null` if no owner is found.\n */\n private findCaretOwner(lookupRoot: Element, name: string): Element | null {\n let el: Element | null = lookupRoot;\n while (el) {\n const state = this.elementState.get(el);\n if (state && state.caretVars.has(name)) return el;\n if (this.isIsolationBoundary(el)) return null;\n el = el.parentElement;\n }\n return null;\n }\n\n /**\n * Read a DOM-scoped variable. Walks up from `lookupRoot`, tracking each\n * element visited as an `element` dep (so writes at any ancestor notify\n * dependent effects). Stops at any `dom-scope=\"isolated\"` boundary that\n * doesn't itself define the var.\n */\n readCaret(lookupRoot: Element, name: string): unknown {\n let el: Element | null = lookupRoot;\n while (el) {\n this.trackElement(el, name);\n const state = this.elementState.get(el);\n if (state && state.caretVars.has(name)) {\n return state.caretVars.get(name);\n }\n if (this.isIsolationBoundary(el)) return undefined;\n el = el.parentElement;\n }\n return undefined;\n }\n\n /**\n * Write a DOM-scoped variable. If `target` is provided, writes there\n * directly; otherwise walks up from `lookupRoot` to find the existing owner,\n * falling back to `lookupRoot` itself if no owner exists.\n */\n writeCaret(lookupRoot: Element, name: string, value: unknown, target?: Element): void {\n const owner = target ?? this.findCaretOwner(lookupRoot, name) ?? lookupRoot;\n const state = this.getElementState(owner);\n state.caretVars.set(name, value);\n this.notifyElement(owner, name);\n }\n\n // ---------------------------------------------------------------------------\n // Scheduler — microtask-batched flush.\n // ---------------------------------------------------------------------------\n\n private schedule(e: Effect): void {\n if (e.stopped) return;\n this.pending.add(e);\n if (this.scheduled) return;\n this.scheduled = true;\n queueMicrotask(() => void this.flush());\n }\n\n private async flush(): Promise<void> {\n try {\n while (this.pending.size > 0) {\n const batch = Array.from(this.pending);\n this.pending.clear();\n for (const e of batch) {\n if (e.stopped) continue;\n await e.run();\n // If the effect's run did not synchronously re-schedule itself, its\n // trigger count is reset. Self-rescheduling (a real cycle) keeps the\n // counter climbing toward the 100 cap. Long-lived effects that fire\n // sporadically (e.g. user-input bindings) stay alive forever.\n if (!this.pending.has(e)) e.resetTriggerCount();\n }\n }\n } finally {\n this.scheduled = false;\n }\n }\n}\n\n// Module singleton. One reactive graph per process; mirrors upstream's\n// `runtime.reactivity` pattern.\nexport const reactive = new Reactive();\n","/**\n * `^name` — DOM-scoped inherited variable.\n *\n * Upstream syntax:\n * ^counter → read from nearest ancestor that has `counter` set\n * ^counter on #target → read from (or near) #target\n * set ^counter to 42 → write to owner (or lookupRoot if not yet defined)\n *\n * Parse side: register `^` as a Pratt prefix operator. The handler consumes\n * the identifier token and an optional `on <target>` clause, emitting a\n * `caretVar` AST node.\n *\n * Eval side: `caretVar` node evaluator calls `reactive.readCaret(anchor,\n * name)` which walks the DOM tree for the owner and records deps as it goes.\n */\n\nimport type { ASTNode, ExecutionContext } from './types';\nimport { reactive } from './signals';\n\nexport interface CaretVarNode extends ASTNode {\n type: 'caretVar';\n name: string;\n onTarget: ASTNode | null;\n}\n\ninterface CaretVarRuntime {\n execute(node: ASTNode, ctx: ExecutionContext): Promise<unknown>;\n}\n\n/**\n * Coerce a runtime-evaluated `on <target>` result to a single Element.\n * Selector expressions resolve to an array of matched elements; bare\n * `me`/`it`/`you`/identifier references resolve to single Elements. Take the\n * first element of an array, or the value itself if it's already an Element.\n */\nfunction coerceToElement(value: unknown): Element | null {\n if (value instanceof Element) return value;\n if (Array.isArray(value) && value[0] instanceof Element) return value[0];\n return null;\n}\n\n/**\n * Pratt prefix handler for `^`. Consumes the following identifier token and\n * an optional `on <expr>` clause.\n */\nexport function parseCaretPrefix(token: unknown, ctx: unknown): ASTNode {\n // `ctx` is hyperfixi's PrattContext: { peek, advance, parseExpr, isStopToken, atEnd }.\n const pctx = ctx as {\n peek(): { value?: string; kind?: string } | undefined;\n advance(): { value: string; kind?: string };\n parseExpr(minBp: number): ASTNode;\n };\n const ident = pctx.advance();\n if (!ident || !ident.value) {\n throw new Error(\"Expected identifier after '^'\");\n }\n let onTarget: ASTNode | null = null;\n const next = pctx.peek();\n if (next && next.value === 'on') {\n pctx.advance(); // consume 'on'\n // Binding power 86 — higher than standard comparisons so `^x on me` doesn't\n // over-consume into surrounding expressions.\n onTarget = pctx.parseExpr(86);\n }\n const startTok = token as { start?: number; end?: number; line?: number; column?: number };\n return {\n type: 'caretVar',\n name: ident.value,\n onTarget,\n start: startTok?.start ?? 0,\n end: startTok?.end ?? 0,\n line: startTok?.line,\n column: startTok?.column,\n } as CaretVarNode;\n}\n\n/**\n * Build a node evaluator for `caretVar` bound to a specific runtime. The\n * evaluator walks up from the resolved anchor element, tracking every element\n * visited so writes at any ancestor notify dependent effects.\n *\n * Capturing `runtime` via a factory closure (matching live/when/bind) keeps\n * the evaluator independent of any module-scope state.\n */\nexport function makeEvaluateCaretVar(\n runtime: CaretVarRuntime\n): (node: ASTNode, ctx: unknown) => Promise<unknown> {\n return async function evaluateCaretVar(node, ctx) {\n const n = node as CaretVarNode;\n const context = ctx as ExecutionContext;\n let anchor: Element | null = (context.me as Element | null) ?? null;\n\n if (n.onTarget) {\n const resolved = await runtime.execute(n.onTarget, context);\n const el = coerceToElement(resolved);\n if (el) anchor = el;\n }\n\n if (!anchor) return undefined;\n return reactive.readCaret(anchor, n.name);\n };\n}\n\n/**\n * Build a node writer for `caretVar` bound to a specific runtime. Used by the\n * core `set` command via `parserExtensions.registerNodeWriter`.\n */\nexport function makeWriteCaretVar(\n runtime: CaretVarRuntime\n): (node: ASTNode, value: unknown, ctx: unknown) => Promise<void> {\n return async function writeCaretVar(node, value, ctx) {\n const n = node as CaretVarNode;\n const context = ctx as ExecutionContext;\n const anchor: Element | null = (context.me as Element | null) ?? null;\n if (!anchor) return;\n\n let target: Element | undefined;\n if (n.onTarget) {\n const resolved = await runtime.execute(n.onTarget, context);\n const el = coerceToElement(resolved);\n if (el) target = el;\n }\n reactive.writeCaret(anchor, n.name, value, target);\n };\n}\n","/**\n * `live ... end` — reactive block. Body re-runs whenever any dependency read\n * during its execution changes.\n *\n * Upstream syntax:\n * live [commandList] end\n *\n * Each command in the body is re-executed as a single effect. The entire\n * body shares one effect instance; its dependency set is the union of every\n * read performed during body execution.\n */\n\nimport type { ASTNode, ExecutionContext, FeatureParserCtx } from './types';\nimport { reactive } from './signals';\n\nexport interface LiveFeatureNode extends ASTNode {\n type: 'liveFeature';\n body: ASTNode[];\n}\n\n/**\n * Parse `live ... end`. The `live` keyword has already been consumed by the\n * parser dispatcher; we parse the body and expect a trailing `end`.\n */\nexport function parseLiveFeature(ctx: unknown, token: unknown): ASTNode {\n const pctx = ctx as FeatureParserCtx;\n const body = pctx.parseCommandListUntilEnd();\n // parseCommandListUntilEnd stops when it sees `end` (but doesn't consume it).\n if (!pctx.isAtEnd() && pctx.check('end')) pctx.match('end');\n const tok = token as { start?: number; end?: number; line?: number; column?: number };\n return {\n type: 'liveFeature',\n body,\n start: tok?.start ?? 0,\n end: pctx.getPosition().end,\n line: tok?.line,\n column: tok?.column,\n } as LiveFeatureNode;\n}\n\n/**\n * Create an evaluator bound to a runtime reference. The plugin captures\n * `runtime` at install time and passes it in so effect re-runs can dispatch\n * the body commands without going through module-scope state.\n */\nexport function makeEvaluateLiveFeature(runtime: {\n execute(node: ASTNode, ctx: ExecutionContext): Promise<unknown>;\n}): (node: ASTNode, ctx: unknown) => unknown | Promise<unknown> {\n return async function evaluateLiveFeature(node, ctx) {\n const context = ctx as ExecutionContext;\n const owner = (context.me as Element) ?? document.body;\n const n = node as LiveFeatureNode;\n\n const stop = reactive.createEffect(\n async () => {\n for (const cmd of n.body) {\n await runtime.execute(cmd, context);\n }\n },\n () => {\n /* no-op — side effects happened inside the expression */\n },\n owner\n );\n context.registerCleanup?.(owner, stop, 'live-effect');\n return undefined;\n };\n}\n","/**\n * `when <expr> [or <expr>]* changes [commandList] end` — observer feature.\n *\n * Runs the body when any watched expression's value changes (Object.is\n * semantics). One effect is created per watched expression so writes to a\n * given dep only re-run that watcher.\n */\n\nimport type { ASTNode, ExecutionContext, FeatureParserCtx } from './types';\nimport { reactive } from './signals';\n\nexport interface WhenFeatureNode extends ASTNode {\n type: 'whenFeature';\n watched: ASTNode[];\n body: ASTNode[];\n}\n\nexport function parseWhenFeature(ctx: unknown, token: unknown): ASTNode {\n const pctx = ctx as FeatureParserCtx;\n const watched: ASTNode[] = [pctx.parseExpression()];\n while (pctx.match('or')) {\n watched.push(pctx.parseExpression());\n }\n pctx.consume('changes', \"Expected 'changes' after when expression list\");\n const body = pctx.parseCommandListUntilEnd();\n if (!pctx.isAtEnd() && pctx.check('end')) pctx.match('end');\n const tok = token as { start?: number; end?: number; line?: number; column?: number };\n return {\n type: 'whenFeature',\n watched,\n body,\n start: tok?.start ?? 0,\n end: pctx.getPosition().end,\n line: tok?.line,\n column: tok?.column,\n } as WhenFeatureNode;\n}\n\nexport function makeEvaluateWhenFeature(runtime: {\n execute(node: ASTNode, ctx: ExecutionContext): Promise<unknown>;\n evaluateExpressionWithResult?: (\n node: ASTNode,\n ctx: ExecutionContext\n ) => Promise<{ value: unknown }>;\n}): (node: ASTNode, ctx: unknown) => unknown | Promise<unknown> {\n return async function evaluateWhenFeature(node, ctx) {\n const context = ctx as ExecutionContext;\n const owner = (context.me as Element) ?? document.body;\n const n = node as WhenFeatureNode;\n\n for (const watchedExpr of n.watched) {\n const stop = reactive.createEffect(\n async () => {\n // Evaluate the watched expression as a regular expression through the\n // runtime so that trackGlobal/trackElement fire via the global-write\n // hook and caret-var reads.\n return await runtime.execute(watchedExpr, context);\n },\n async newValue => {\n // Fire the body with `it` / `result` bound to the new value.\n const subCtx: ExecutionContext = { ...context, it: newValue, result: newValue };\n for (const cmd of n.body) {\n await runtime.execute(cmd, subCtx);\n }\n },\n owner\n );\n context.registerCleanup?.(owner, stop, 'when-effect');\n }\n return undefined;\n };\n}\n","/**\n * `bind X [to|and|with] Y` — two-way binding.\n *\n * Creates two effects (registration order is load-bearing — both initialize\n * via `queueMicrotask` and run in registration order):\n * 1. DOM → var: read DOM, write var. DOM \"wins\" on init.\n * 2. var → DOM: read var, write DOM. Fires on programmatic var writes\n * after init. On its own initial run, var === DOM (Effect 1 just synced\n * them), so the write is a no-op.\n *\n * Two binding forms:\n *\n * 1. Auto-detected (DOM side is a bare element expression):\n *\n * bind $name to #input -- detects `value`\n * bind $checked to me -- detects `checked` on a checkbox\n *\n * Auto-detected property by element type:\n * - INPUT[type=checkbox|radio] → `checked`\n * - INPUT[type=number|range] → `valueAsNumber`\n * - INPUT|TEXTAREA|SELECT → `value`\n * - contenteditable=\"true\" → `textContent`\n * - Custom elements with own `value` → `value`\n *\n * 2. Explicit property (DOM side is a member or possessive expression):\n *\n * bind $color to #picker's value -- possessive (preferred — reads in any language)\n * bind $color to #picker.value -- dot (JS-style alternative)\n * bind $text to #div's textContent -- non-form properties: var→DOM only\n *\n * For form-like elements, both directions work. For non-form elements\n * (e.g., binding a div's `textContent`), only var→DOM fires — there are\n * no input/change events to drive DOM→var, so user mutations of the\n * property via devtools won't propagate back.\n */\n\nimport type { ASTNode, ExecutionContext, FeatureParserCtx } from './types';\nimport { reactive } from './signals';\n\nexport interface BindFeatureNode extends ASTNode {\n type: 'bindFeature';\n left: ASTNode;\n right: ASTNode;\n}\n\nexport function parseBindFeature(ctx: unknown, token: unknown): ASTNode {\n const pctx = ctx as FeatureParserCtx;\n const left = pctx.parseExpression();\n // Accept any of `to` / `and` / `with` as the separator.\n if (!(pctx.match('to') || pctx.match('and') || pctx.match('with'))) {\n throw new Error(\"bind requires 'to', 'and', or 'with' between the two sides\");\n }\n const right = pctx.parseExpression();\n // Optional `end` terminator (matches upstream which allows both forms).\n if (pctx.check('end')) pctx.match('end');\n const tok = token as { start?: number; end?: number; line?: number; column?: number };\n return {\n type: 'bindFeature',\n left,\n right,\n start: tok?.start ?? 0,\n end: pctx.getPosition().end,\n line: tok?.line,\n column: tok?.column,\n } as BindFeatureNode;\n}\n\n/**\n * Auto-detect the DOM property to bind by element type. Returns null if the\n * target isn't a recognized form/editable element.\n */\nfunction detectProperty(el: Element): string | null {\n const tag = el.tagName;\n if (tag === 'INPUT') {\n const type = (el as HTMLInputElement).type;\n if (type === 'checkbox' || type === 'radio') return 'checked';\n if (type === 'number' || type === 'range') return 'valueAsNumber';\n return 'value';\n }\n if (tag === 'TEXTAREA' || tag === 'SELECT') return 'value';\n const ce = (el as HTMLElement).contentEditable;\n if (ce === 'true') return 'textContent';\n // Custom elements with own `value` prop.\n if ('value' in el) return 'value';\n return null;\n}\n\n/**\n * If `node` is a `memberExpression` or `possessiveExpression` with a static\n * identifier property (`#el.value` or `#el's value`), return the inner element\n * expression and the property name. Returns null for anything else.\n *\n * Computed member access (`#el[prop]`) is intentionally rejected — we'd have to\n * evaluate the index dynamically and the binding direction would be unclear.\n * Chained property access (`#el.dataset.value`) is also not unpacked — we only\n * peel one level. Multi-level support is a future arc.\n */\nfunction unwrapExplicitProperty(node: ASTNode): { element: ASTNode; propertyName: string } | null {\n if (!node || (node.type !== 'memberExpression' && node.type !== 'possessiveExpression')) {\n return null;\n }\n if (node.type === 'memberExpression' && node.computed === true) return null;\n const property = node.property as ASTNode | undefined;\n const object = node.object as ASTNode | undefined;\n if (!property || !object || property.type !== 'identifier') return null;\n const name = (property.name as string) ?? '';\n if (!name) return null;\n return { element: object, propertyName: name };\n}\n\n/**\n * Detect a chained property access on the bind RHS — e.g.\n * `me.style.backgroundColor` or `#el's dataset's value`.\n *\n * After `unwrapExplicitProperty` peels the outermost member, the returned\n * `element` is the object of that member. If that object is itself a\n * member/possessive expression, we have a multi-level chain that v1 of\n * bind doesn't support. Used only for the error path; the parsed AST is\n * left intact.\n */\nfunction isChainedMember(node: ASTNode | undefined): boolean {\n if (!node) return false;\n return node.type === 'memberExpression' || node.type === 'possessiveExpression';\n}\n\n/**\n * Whether the input/change listener installed by `trackDomProperty` would\n * actually fire for this element. Used to decide whether to wire up the\n * DOM→var direction when an explicit property is given.\n */\nfunction isFormLikeElement(el: Element): boolean {\n const tag = el.tagName;\n if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') return true;\n if ((el as HTMLElement).contentEditable === 'true') return true;\n return false;\n}\n\n/**\n * Whether debug logging is enabled. Mirrors signals.ts — keeps a single\n * convention for the package.\n */\nfunction debugEnabled(): boolean {\n try {\n return typeof localStorage !== 'undefined' && localStorage.getItem('hyperfixi:debug') !== null;\n } catch {\n return false;\n }\n}\n\n/**\n * Create the bind evaluator. Because `bind` has unusual parse shape (AST\n * nodes for left/right that may be identifiers, `$name` references, or DOM\n * element lookups), we rely on the runtime to evaluate them.\n */\nexport function makeEvaluateBindFeature(runtime: {\n execute(node: ASTNode, ctx: ExecutionContext): Promise<unknown>;\n}): (node: ASTNode, ctx: unknown) => unknown | Promise<unknown> {\n return async function evaluateBindFeature(node, ctx) {\n const context = ctx as ExecutionContext;\n const owner = (context.me as Element) ?? document.body;\n const n = node as BindFeatureNode;\n\n // Resolve sides. One is a var reference (read/write a symbol), the other\n // a DOM target (read/write a property). Determine direction from node shape.\n const leftIsVar = isVarRef(n.left);\n const rightIsVar = isVarRef(n.right);\n\n // We need a DOM-side descriptor (element + property) and a var-side name.\n let varSide: { name: string; isGlobal: boolean } | null = null;\n let domSide: { exprNode: ASTNode } | null = null;\n\n if (leftIsVar && !rightIsVar) {\n varSide = varRefInfo(n.left);\n domSide = { exprNode: n.right };\n } else if (rightIsVar && !leftIsVar) {\n varSide = varRefInfo(n.right);\n domSide = { exprNode: n.left };\n } else if (leftIsVar && rightIsVar) {\n // var-to-var binding not supported in v1 (no DOM side).\n throw new Error('bind: cannot bind two symbols together (need a DOM side)');\n } else {\n throw new Error('bind: could not identify a symbol side');\n }\n\n // Unwrap explicit property syntax: `#el.value` or `#el's value` becomes\n // (element-expression, propertyName). Otherwise, evaluate the whole side\n // and rely on auto-detection.\n const explicit = unwrapExplicitProperty(domSide.exprNode);\n // Multi-level property access (`#el.style.background`, `me's dataset's id`)\n // is not supported in v1. Surface that diagnosis up front; otherwise the\n // user gets the generic \"did not resolve to an element\" message and no\n // hint that the chain is the problem.\n if (explicit && isChainedMember(explicit.element)) {\n throw new Error(\n 'bind: multi-level property access (e.g., `#el.a.b`) is not supported in v1 — restructure to a single property write or pass the element directly for auto-detection (e.g., `bind $x to #el`).'\n );\n }\n const elementExpr = explicit ? explicit.element : domSide.exprNode;\n const domValue = await runtime.execute(elementExpr, context);\n if (!(domValue instanceof Element)) {\n const valueType =\n domValue === null ? 'null' : domValue === undefined ? 'undefined' : typeof domValue;\n const snippet =\n domValue !== null && domValue !== undefined ? ` \"${String(domValue).slice(0, 40)}\"` : '';\n const suggestion = explicit\n ? ''\n : \" If you meant to write to a property, use the explicit form: `<selector>'s <property>`.\";\n throw new Error(\n `bind: right-hand side did not resolve to an element (got ${valueType}${snippet}).${suggestion}`\n );\n }\n const el = domValue;\n // Explicit property bypasses auto-detect; the user takes responsibility for\n // picking something readable from the element. For DOM→var to work without\n // explicit property, fall back to auto-detect.\n const prop = explicit ? explicit.propertyName : detectProperty(el);\n if (!prop) {\n throw new Error(\n `bind: could not auto-detect property for <${el.tagName.toLowerCase()}> — use explicit \\`<expr>'s <property>\\` form`\n );\n }\n // For explicit-property mode on non-form elements, DOM→var can't sync via\n // input/change events. Skip the listener install; only var→DOM runs.\n const installDomToVar = !explicit || isFormLikeElement(el);\n if (explicit && !installDomToVar && debugEnabled() && typeof console !== 'undefined') {\n console.warn(\n `[@hyperfixi/reactivity] bind: DOM→var skipped for <${el.tagName.toLowerCase()}>.${prop} — no input/change event source.`\n );\n }\n\n // Initial sync: DOM → var.\n const readDom = (): unknown => {\n if (prop === 'valueAsNumber') return (el as HTMLInputElement).valueAsNumber;\n if (prop === 'checked') return (el as HTMLInputElement).checked;\n if (prop === 'textContent') return el.textContent ?? '';\n return (el as any)[prop];\n };\n const writeDom = (value: unknown): void => {\n if (prop === 'valueAsNumber') {\n const n = Number(value);\n (el as HTMLInputElement).valueAsNumber = Number.isNaN(n) ? (null as never) : n;\n } else if (prop === 'checked') {\n (el as HTMLInputElement).checked = Boolean(value);\n } else if (prop === 'textContent') {\n el.textContent = value == null ? '' : String(value);\n } else {\n (el as any)[prop] = value;\n }\n };\n const readVar = (): unknown => {\n if (varSide!.isGlobal) return context.globals?.get(varSide!.name);\n return context.locals?.get(varSide!.name);\n };\n const writeVar = (value: unknown): void => {\n if (varSide!.isGlobal) {\n context.globals?.set(varSide!.name, value);\n // Bypass the global-write hook (we're touching the Map directly), so\n // notify the reactive graph manually for the var→DOM effect.\n reactive.notifyGlobal(varSide!.name);\n } else {\n context.locals?.set(varSide!.name, value);\n // Same rationale for locals — direct Map write skips the localWriteHook,\n // so we notify the element-scoped subscription set directly.\n reactive.notifyElement(owner, varSide!.name);\n }\n };\n\n // Effect 1: DOM → var (fires on user input). Skipped for explicit-property\n // bindings on non-form elements — no input/change events would drive it.\n let stopDomToVar: (() => void) | null = null;\n if (installDomToVar) {\n stopDomToVar = reactive.createEffect(\n () => {\n reactive.trackDomProperty(el, prop);\n return readDom();\n },\n newValue => {\n writeVar(newValue);\n },\n owner\n );\n }\n\n // Effect 2: var → DOM (fires on programmatic var writes — for globals\n // via the core's globalWriteHook, for locals via the localWriteHook the\n // reactivity plugin registers).\n const stopVarToDom = reactive.createEffect(\n () => {\n if (varSide!.isGlobal) reactive.trackGlobal(varSide!.name);\n else reactive.trackElement(owner, varSide!.name);\n return readVar();\n },\n newValue => {\n if (newValue === undefined) return;\n writeDom(newValue);\n },\n owner\n );\n\n if (stopDomToVar) context.registerCleanup?.(owner, stopDomToVar, 'bind-dom-to-var');\n context.registerCleanup?.(owner, stopVarToDom, 'bind-var-to-dom');\n return undefined;\n };\n}\n\n/**\n * Is this AST node a var reference we can bind to?\n *\n * The hyperfixi parser emits `identifier` nodes for both globals and locals,\n * but with different shapes:\n * - `$foo` → `{ type: 'identifier', name: '$foo' }` (no scope field)\n * - `:foo` → `{ type: 'identifier', name: 'foo', scope: 'local' }`\n * - `::foo` → `{ type: 'identifier', name: 'foo', scope: 'global' }`\n *\n * We accept all three. The legacy `$`-prefix sniff stays as a fallback because\n * `parseExpression` doesn't always set `scope`.\n */\nfunction isVarRef(node: ASTNode): boolean {\n if (!node) return false;\n if (node.type !== 'identifier') return false;\n const scope = node.scope as string | undefined;\n if (scope === 'local' || scope === 'global') return true;\n const name = (node.name as string) ?? '';\n return name.startsWith('$') || name.startsWith(':');\n}\n\nfunction varRefInfo(node: ASTNode): { name: string; isGlobal: boolean } {\n const rawName = (node.name as string) ?? '';\n const scope = node.scope as string | undefined;\n // Prefer the parser-emitted scope marker. Fall back to prefix sniffing for\n // shapes where scope isn't set (e.g. `$foo` arriving as bare identifier\n // with name '$foo').\n let isGlobal: boolean;\n let name: string;\n if (scope === 'global') {\n isGlobal = true;\n name = rawName;\n } else if (scope === 'local') {\n isGlobal = false;\n name = rawName;\n } else if (rawName.startsWith('$')) {\n isGlobal = true;\n name = rawName.slice(1);\n } else if (rawName.startsWith(':')) {\n isGlobal = false;\n name = rawName.slice(1);\n } else {\n isGlobal = false;\n name = rawName;\n }\n if (!name) {\n throw new Error('bind: variable reference has empty name');\n }\n return { name, isGlobal };\n}\n","/**\n * @hyperfixi/reactivity — signal-based reactive features for hyperfixi.\n *\n * Adds four constructs from upstream _hyperscript 0.9.90:\n *\n * live [commandList] end reactive block — body re-runs\n * when tracked reads change.\n *\n * when <expr> [or <expr>]* changes observer — body runs when any\n * [commandList] end watched expression changes.\n *\n * bind <var> to <element> two-way DOM ⇄ var binding.\n *\n * ^name [on <target>] DOM-scoped inherited variable\n * (read + write with `^`).\n *\n * Install:\n *\n * ```ts\n * import { createRuntime, installPlugin } from '@hyperfixi/core';\n * import { reactivityPlugin } from '@hyperfixi/reactivity';\n *\n * const runtime = createRuntime();\n * installPlugin(runtime, reactivityPlugin);\n * ```\n */\n\nimport type { HyperfixiPlugin, HyperfixiPluginContext } from '@hyperfixi/core';\nimport { reactive } from './signals';\nimport { parseCaretPrefix, makeEvaluateCaretVar, makeWriteCaretVar } from './caret-var';\nimport { parseLiveFeature, makeEvaluateLiveFeature } from './live';\nimport { parseWhenFeature, makeEvaluateWhenFeature } from './when';\nimport { parseBindFeature, makeEvaluateBindFeature } from './bind';\n\nexport { reactive } from './signals';\nexport type { CaretVarNode } from './caret-var';\nexport type { LiveFeatureNode } from './live';\nexport type { WhenFeatureNode } from './when';\nexport type { BindFeatureNode } from './bind';\n\n/**\n * The plugin object. Install once at app startup; re-installing is idempotent\n * (guarded via a `parserExtensions.hasFeature('live')` check).\n *\n * Registers:\n * - Features: `live`, `when`, `bind` (top-level features with `end` bodies)\n * - Prefix op: `^` (primary expression for DOM-scoped vars)\n * - Node evaluators: `liveFeature`, `whenFeature`, `bindFeature`, `caretVar`\n * - A node writer for `caretVar` so `set ^X to Y` flows through `reactive.writeCaret`\n * - Global read/write hooks so `$name` reads track and writes notify\n *\n * Effect cleanup: each effect-creating evaluator calls\n * `context.registerCleanup(owner, stop, ...)` so the core runtime tears effects\n * down when their owning element is cleaned up. There is no separate plugin-level\n * cleanup hook; `reactive.stopElementEffects(el)` is exposed for explicit teardown\n * by tests and consumers that manage element lifecycle outside the runtime.\n */\nexport const reactivityPlugin: HyperfixiPlugin & { version: string } = {\n name: '@hyperfixi/reactivity',\n version: '2.3.1',\n install(ctx: HyperfixiPluginContext) {\n const { parserExtensions, runtime } = ctx;\n\n // Idempotency: the parser-extension registry is process-singleton, so\n // re-installing into a fresh runtime would otherwise stack additional\n // global read/write hooks on every call. `snapshot()`/`restore()` clears\n // the feature registry — when tests roll back the registry, this guard\n // re-enables a fresh install.\n if (parserExtensions.hasFeature('live')) return;\n\n // Parser hooks — three block features plus a primary-expression caret.\n parserExtensions.registerFeature('live', parseLiveFeature as never);\n parserExtensions.registerFeature('when', parseWhenFeature as never);\n parserExtensions.registerFeature('bind', parseBindFeature as never);\n parserExtensions.registerPrefixOperator('^', 85, parseCaretPrefix as never);\n\n // Runtime evaluators. Features capture `runtime` so effect re-runs can\n // dispatch body commands without going through module-scope state.\n parserExtensions.registerNodeEvaluator(\n 'liveFeature',\n makeEvaluateLiveFeature(runtime as never) as never\n );\n parserExtensions.registerNodeEvaluator(\n 'whenFeature',\n makeEvaluateWhenFeature(runtime as never) as never\n );\n parserExtensions.registerNodeEvaluator(\n 'bindFeature',\n makeEvaluateBindFeature(runtime as never) as never\n );\n parserExtensions.registerNodeEvaluator(\n 'caretVar',\n makeEvaluateCaretVar(runtime as never) as never\n );\n\n // Caret-var write: lets the core `set` command dispatch `set ^X to Y`\n // through `reactive.writeCaret`. Resolves `on <target>` via the captured\n // runtime — no global-scope indirection.\n parserExtensions.registerNodeWriter('caretVar', makeWriteCaretVar(runtime as never) as never);\n\n // Global-write hook: notify the reactive graph whenever `$name` is set.\n // The returned disposer is intentionally discarded — the install-time\n // idempotency guard above is the sole gate against double-registration.\n parserExtensions.registerGlobalWriteHook((name: string, _value: unknown, _context: unknown) => {\n reactive.notifyGlobal(name);\n });\n\n // Global-read hook: track the read against the current effect (if any)\n // so effects re-run when the global changes.\n parserExtensions.registerGlobalReadHook((name: string, _context: unknown) => {\n reactive.trackGlobal(name);\n });\n\n // Local hooks — analogous to globals, but keyed by `context.me` so each\n // element holds independent state. A `set :foo to ...` running in a\n // handler with `me=button1` notifies effects subscribed to (button1, 'foo');\n // a `set :foo` in a different `me` won't reach them. This matches how\n // locals already work — they're never cross-context — and lets two\n // components hold independent `:foo` state without interference.\n parserExtensions.registerLocalWriteHook((name: string, _value: unknown, context) => {\n const owner = (context as { me?: Element | null }).me ?? null;\n if (owner) reactive.notifyElement(owner, name);\n });\n\n parserExtensions.registerLocalReadHook((name: string, context) => {\n const owner = (context as { me?: Element | null }).me ?? null;\n if (owner) reactive.trackElement(owner, name);\n });\n },\n};\n\nexport default reactivityPlugin;\n"],"mappings":";AA0BA,SAAS,eAAwB;AAC/B,MAAI;AACF,WAAO,OAAO,iBAAiB,eAAe,aAAa,QAAQ,iBAAiB,MAAM;AAAA,EAC5F,QAAQ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C3E,eAAS,YAAY,IAAI;AAAA,IAC3B,CAAC;AAQD,qBAAiB,uBAAuB,CAAC,MAAc,QAAiB,YAAY;AAClF,YAAM,QAAS,QAAoC,MAAM;AACzD,UAAI,MAAO,UAAS,cAAc,OAAO,IAAI;AAAA,IAC/C,CAAC;AAED,qBAAiB,sBAAsB,CAAC,MAAc,YAAY;AAChE,YAAM,QAAS,QAAoC,MAAM;AACzD,UAAI,MAAO,UAAS,aAAa,OAAO,IAAI;AAAA,IAC9C,CAAC;AAAA,EACH;AACF;AAEA,IAAO,gBAAQ;","names":["debugEnabled","n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hyperfixi/reactivity",
|
|
3
|
+
"version": "2.4.0",
|
|
4
|
+
"description": "Reactive features for hyperfixi — adds `live`, `bind`, `when X changes`, and `^name` DOM-scoped vars (upstream _hyperscript 0.9.90).",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "dist/index.cjs",
|
|
8
|
+
"module": "dist/index.js",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsup",
|
|
19
|
+
"test": "vitest",
|
|
20
|
+
"test:run": "vitest run",
|
|
21
|
+
"test:check": "vitest run --reporter=dot 2>&1 | tail -5",
|
|
22
|
+
"typecheck": "tsc --noEmit"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@hyperfixi/core": "*"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^20.0.0",
|
|
29
|
+
"happy-dom": "^20.9.0",
|
|
30
|
+
"tsup": "^8.0.0",
|
|
31
|
+
"typescript": "^5.0.0",
|
|
32
|
+
"vitest": "^4.1.5"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"src",
|
|
37
|
+
"LICENSE"
|
|
38
|
+
],
|
|
39
|
+
"keywords": [
|
|
40
|
+
"hyperfixi",
|
|
41
|
+
"hyperscript",
|
|
42
|
+
"reactivity",
|
|
43
|
+
"reactive",
|
|
44
|
+
"signals",
|
|
45
|
+
"plugin",
|
|
46
|
+
"_hyperscript",
|
|
47
|
+
"v0.9.90"
|
|
48
|
+
],
|
|
49
|
+
"author": "LokaScript Contributors",
|
|
50
|
+
"license": "MIT",
|
|
51
|
+
"repository": {
|
|
52
|
+
"type": "git",
|
|
53
|
+
"url": "git+https://github.com/codetalcott/hyperfixi.git",
|
|
54
|
+
"directory": "packages/reactivity"
|
|
55
|
+
},
|
|
56
|
+
"engines": {
|
|
57
|
+
"node": ">=18.0.0"
|
|
58
|
+
},
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"access": "public"
|
|
61
|
+
}
|
|
62
|
+
}
|
package/src/bind.ts
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `bind X [to|and|with] Y` — two-way binding.
|
|
3
|
+
*
|
|
4
|
+
* Creates two effects (registration order is load-bearing — both initialize
|
|
5
|
+
* via `queueMicrotask` and run in registration order):
|
|
6
|
+
* 1. DOM → var: read DOM, write var. DOM "wins" on init.
|
|
7
|
+
* 2. var → DOM: read var, write DOM. Fires on programmatic var writes
|
|
8
|
+
* after init. On its own initial run, var === DOM (Effect 1 just synced
|
|
9
|
+
* them), so the write is a no-op.
|
|
10
|
+
*
|
|
11
|
+
* Two binding forms:
|
|
12
|
+
*
|
|
13
|
+
* 1. Auto-detected (DOM side is a bare element expression):
|
|
14
|
+
*
|
|
15
|
+
* bind $name to #input -- detects `value`
|
|
16
|
+
* bind $checked to me -- detects `checked` on a checkbox
|
|
17
|
+
*
|
|
18
|
+
* Auto-detected property by element type:
|
|
19
|
+
* - INPUT[type=checkbox|radio] → `checked`
|
|
20
|
+
* - INPUT[type=number|range] → `valueAsNumber`
|
|
21
|
+
* - INPUT|TEXTAREA|SELECT → `value`
|
|
22
|
+
* - contenteditable="true" → `textContent`
|
|
23
|
+
* - Custom elements with own `value` → `value`
|
|
24
|
+
*
|
|
25
|
+
* 2. Explicit property (DOM side is a member or possessive expression):
|
|
26
|
+
*
|
|
27
|
+
* bind $color to #picker's value -- possessive (preferred — reads in any language)
|
|
28
|
+
* bind $color to #picker.value -- dot (JS-style alternative)
|
|
29
|
+
* bind $text to #div's textContent -- non-form properties: var→DOM only
|
|
30
|
+
*
|
|
31
|
+
* For form-like elements, both directions work. For non-form elements
|
|
32
|
+
* (e.g., binding a div's `textContent`), only var→DOM fires — there are
|
|
33
|
+
* no input/change events to drive DOM→var, so user mutations of the
|
|
34
|
+
* property via devtools won't propagate back.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
import type { ASTNode, ExecutionContext, FeatureParserCtx } from './types';
|
|
38
|
+
import { reactive } from './signals';
|
|
39
|
+
|
|
40
|
+
export interface BindFeatureNode extends ASTNode {
|
|
41
|
+
type: 'bindFeature';
|
|
42
|
+
left: ASTNode;
|
|
43
|
+
right: ASTNode;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function parseBindFeature(ctx: unknown, token: unknown): ASTNode {
|
|
47
|
+
const pctx = ctx as FeatureParserCtx;
|
|
48
|
+
const left = pctx.parseExpression();
|
|
49
|
+
// Accept any of `to` / `and` / `with` as the separator.
|
|
50
|
+
if (!(pctx.match('to') || pctx.match('and') || pctx.match('with'))) {
|
|
51
|
+
throw new Error("bind requires 'to', 'and', or 'with' between the two sides");
|
|
52
|
+
}
|
|
53
|
+
const right = pctx.parseExpression();
|
|
54
|
+
// Optional `end` terminator (matches upstream which allows both forms).
|
|
55
|
+
if (pctx.check('end')) pctx.match('end');
|
|
56
|
+
const tok = token as { start?: number; end?: number; line?: number; column?: number };
|
|
57
|
+
return {
|
|
58
|
+
type: 'bindFeature',
|
|
59
|
+
left,
|
|
60
|
+
right,
|
|
61
|
+
start: tok?.start ?? 0,
|
|
62
|
+
end: pctx.getPosition().end,
|
|
63
|
+
line: tok?.line,
|
|
64
|
+
column: tok?.column,
|
|
65
|
+
} as BindFeatureNode;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Auto-detect the DOM property to bind by element type. Returns null if the
|
|
70
|
+
* target isn't a recognized form/editable element.
|
|
71
|
+
*/
|
|
72
|
+
function detectProperty(el: Element): string | null {
|
|
73
|
+
const tag = el.tagName;
|
|
74
|
+
if (tag === 'INPUT') {
|
|
75
|
+
const type = (el as HTMLInputElement).type;
|
|
76
|
+
if (type === 'checkbox' || type === 'radio') return 'checked';
|
|
77
|
+
if (type === 'number' || type === 'range') return 'valueAsNumber';
|
|
78
|
+
return 'value';
|
|
79
|
+
}
|
|
80
|
+
if (tag === 'TEXTAREA' || tag === 'SELECT') return 'value';
|
|
81
|
+
const ce = (el as HTMLElement).contentEditable;
|
|
82
|
+
if (ce === 'true') return 'textContent';
|
|
83
|
+
// Custom elements with own `value` prop.
|
|
84
|
+
if ('value' in el) return 'value';
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* If `node` is a `memberExpression` or `possessiveExpression` with a static
|
|
90
|
+
* identifier property (`#el.value` or `#el's value`), return the inner element
|
|
91
|
+
* expression and the property name. Returns null for anything else.
|
|
92
|
+
*
|
|
93
|
+
* Computed member access (`#el[prop]`) is intentionally rejected — we'd have to
|
|
94
|
+
* evaluate the index dynamically and the binding direction would be unclear.
|
|
95
|
+
* Chained property access (`#el.dataset.value`) is also not unpacked — we only
|
|
96
|
+
* peel one level. Multi-level support is a future arc.
|
|
97
|
+
*/
|
|
98
|
+
function unwrapExplicitProperty(node: ASTNode): { element: ASTNode; propertyName: string } | null {
|
|
99
|
+
if (!node || (node.type !== 'memberExpression' && node.type !== 'possessiveExpression')) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
if (node.type === 'memberExpression' && node.computed === true) return null;
|
|
103
|
+
const property = node.property as ASTNode | undefined;
|
|
104
|
+
const object = node.object as ASTNode | undefined;
|
|
105
|
+
if (!property || !object || property.type !== 'identifier') return null;
|
|
106
|
+
const name = (property.name as string) ?? '';
|
|
107
|
+
if (!name) return null;
|
|
108
|
+
return { element: object, propertyName: name };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Detect a chained property access on the bind RHS — e.g.
|
|
113
|
+
* `me.style.backgroundColor` or `#el's dataset's value`.
|
|
114
|
+
*
|
|
115
|
+
* After `unwrapExplicitProperty` peels the outermost member, the returned
|
|
116
|
+
* `element` is the object of that member. If that object is itself a
|
|
117
|
+
* member/possessive expression, we have a multi-level chain that v1 of
|
|
118
|
+
* bind doesn't support. Used only for the error path; the parsed AST is
|
|
119
|
+
* left intact.
|
|
120
|
+
*/
|
|
121
|
+
function isChainedMember(node: ASTNode | undefined): boolean {
|
|
122
|
+
if (!node) return false;
|
|
123
|
+
return node.type === 'memberExpression' || node.type === 'possessiveExpression';
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Whether the input/change listener installed by `trackDomProperty` would
|
|
128
|
+
* actually fire for this element. Used to decide whether to wire up the
|
|
129
|
+
* DOM→var direction when an explicit property is given.
|
|
130
|
+
*/
|
|
131
|
+
function isFormLikeElement(el: Element): boolean {
|
|
132
|
+
const tag = el.tagName;
|
|
133
|
+
if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') return true;
|
|
134
|
+
if ((el as HTMLElement).contentEditable === 'true') return true;
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Whether debug logging is enabled. Mirrors signals.ts — keeps a single
|
|
140
|
+
* convention for the package.
|
|
141
|
+
*/
|
|
142
|
+
function debugEnabled(): boolean {
|
|
143
|
+
try {
|
|
144
|
+
return typeof localStorage !== 'undefined' && localStorage.getItem('hyperfixi:debug') !== null;
|
|
145
|
+
} catch {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Create the bind evaluator. Because `bind` has unusual parse shape (AST
|
|
152
|
+
* nodes for left/right that may be identifiers, `$name` references, or DOM
|
|
153
|
+
* element lookups), we rely on the runtime to evaluate them.
|
|
154
|
+
*/
|
|
155
|
+
export function makeEvaluateBindFeature(runtime: {
|
|
156
|
+
execute(node: ASTNode, ctx: ExecutionContext): Promise<unknown>;
|
|
157
|
+
}): (node: ASTNode, ctx: unknown) => unknown | Promise<unknown> {
|
|
158
|
+
return async function evaluateBindFeature(node, ctx) {
|
|
159
|
+
const context = ctx as ExecutionContext;
|
|
160
|
+
const owner = (context.me as Element) ?? document.body;
|
|
161
|
+
const n = node as BindFeatureNode;
|
|
162
|
+
|
|
163
|
+
// Resolve sides. One is a var reference (read/write a symbol), the other
|
|
164
|
+
// a DOM target (read/write a property). Determine direction from node shape.
|
|
165
|
+
const leftIsVar = isVarRef(n.left);
|
|
166
|
+
const rightIsVar = isVarRef(n.right);
|
|
167
|
+
|
|
168
|
+
// We need a DOM-side descriptor (element + property) and a var-side name.
|
|
169
|
+
let varSide: { name: string; isGlobal: boolean } | null = null;
|
|
170
|
+
let domSide: { exprNode: ASTNode } | null = null;
|
|
171
|
+
|
|
172
|
+
if (leftIsVar && !rightIsVar) {
|
|
173
|
+
varSide = varRefInfo(n.left);
|
|
174
|
+
domSide = { exprNode: n.right };
|
|
175
|
+
} else if (rightIsVar && !leftIsVar) {
|
|
176
|
+
varSide = varRefInfo(n.right);
|
|
177
|
+
domSide = { exprNode: n.left };
|
|
178
|
+
} else if (leftIsVar && rightIsVar) {
|
|
179
|
+
// var-to-var binding not supported in v1 (no DOM side).
|
|
180
|
+
throw new Error('bind: cannot bind two symbols together (need a DOM side)');
|
|
181
|
+
} else {
|
|
182
|
+
throw new Error('bind: could not identify a symbol side');
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Unwrap explicit property syntax: `#el.value` or `#el's value` becomes
|
|
186
|
+
// (element-expression, propertyName). Otherwise, evaluate the whole side
|
|
187
|
+
// and rely on auto-detection.
|
|
188
|
+
const explicit = unwrapExplicitProperty(domSide.exprNode);
|
|
189
|
+
// Multi-level property access (`#el.style.background`, `me's dataset's id`)
|
|
190
|
+
// is not supported in v1. Surface that diagnosis up front; otherwise the
|
|
191
|
+
// user gets the generic "did not resolve to an element" message and no
|
|
192
|
+
// hint that the chain is the problem.
|
|
193
|
+
if (explicit && isChainedMember(explicit.element)) {
|
|
194
|
+
throw new Error(
|
|
195
|
+
'bind: multi-level property access (e.g., `#el.a.b`) is not supported in v1 — restructure to a single property write or pass the element directly for auto-detection (e.g., `bind $x to #el`).'
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
const elementExpr = explicit ? explicit.element : domSide.exprNode;
|
|
199
|
+
const domValue = await runtime.execute(elementExpr, context);
|
|
200
|
+
if (!(domValue instanceof Element)) {
|
|
201
|
+
const valueType =
|
|
202
|
+
domValue === null ? 'null' : domValue === undefined ? 'undefined' : typeof domValue;
|
|
203
|
+
const snippet =
|
|
204
|
+
domValue !== null && domValue !== undefined ? ` "${String(domValue).slice(0, 40)}"` : '';
|
|
205
|
+
const suggestion = explicit
|
|
206
|
+
? ''
|
|
207
|
+
: " If you meant to write to a property, use the explicit form: `<selector>'s <property>`.";
|
|
208
|
+
throw new Error(
|
|
209
|
+
`bind: right-hand side did not resolve to an element (got ${valueType}${snippet}).${suggestion}`
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
const el = domValue;
|
|
213
|
+
// Explicit property bypasses auto-detect; the user takes responsibility for
|
|
214
|
+
// picking something readable from the element. For DOM→var to work without
|
|
215
|
+
// explicit property, fall back to auto-detect.
|
|
216
|
+
const prop = explicit ? explicit.propertyName : detectProperty(el);
|
|
217
|
+
if (!prop) {
|
|
218
|
+
throw new Error(
|
|
219
|
+
`bind: could not auto-detect property for <${el.tagName.toLowerCase()}> — use explicit \`<expr>'s <property>\` form`
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
// For explicit-property mode on non-form elements, DOM→var can't sync via
|
|
223
|
+
// input/change events. Skip the listener install; only var→DOM runs.
|
|
224
|
+
const installDomToVar = !explicit || isFormLikeElement(el);
|
|
225
|
+
if (explicit && !installDomToVar && debugEnabled() && typeof console !== 'undefined') {
|
|
226
|
+
console.warn(
|
|
227
|
+
`[@hyperfixi/reactivity] bind: DOM→var skipped for <${el.tagName.toLowerCase()}>.${prop} — no input/change event source.`
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Initial sync: DOM → var.
|
|
232
|
+
const readDom = (): unknown => {
|
|
233
|
+
if (prop === 'valueAsNumber') return (el as HTMLInputElement).valueAsNumber;
|
|
234
|
+
if (prop === 'checked') return (el as HTMLInputElement).checked;
|
|
235
|
+
if (prop === 'textContent') return el.textContent ?? '';
|
|
236
|
+
return (el as any)[prop];
|
|
237
|
+
};
|
|
238
|
+
const writeDom = (value: unknown): void => {
|
|
239
|
+
if (prop === 'valueAsNumber') {
|
|
240
|
+
const n = Number(value);
|
|
241
|
+
(el as HTMLInputElement).valueAsNumber = Number.isNaN(n) ? (null as never) : n;
|
|
242
|
+
} else if (prop === 'checked') {
|
|
243
|
+
(el as HTMLInputElement).checked = Boolean(value);
|
|
244
|
+
} else if (prop === 'textContent') {
|
|
245
|
+
el.textContent = value == null ? '' : String(value);
|
|
246
|
+
} else {
|
|
247
|
+
(el as any)[prop] = value;
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
const readVar = (): unknown => {
|
|
251
|
+
if (varSide!.isGlobal) return context.globals?.get(varSide!.name);
|
|
252
|
+
return context.locals?.get(varSide!.name);
|
|
253
|
+
};
|
|
254
|
+
const writeVar = (value: unknown): void => {
|
|
255
|
+
if (varSide!.isGlobal) {
|
|
256
|
+
context.globals?.set(varSide!.name, value);
|
|
257
|
+
// Bypass the global-write hook (we're touching the Map directly), so
|
|
258
|
+
// notify the reactive graph manually for the var→DOM effect.
|
|
259
|
+
reactive.notifyGlobal(varSide!.name);
|
|
260
|
+
} else {
|
|
261
|
+
context.locals?.set(varSide!.name, value);
|
|
262
|
+
// Same rationale for locals — direct Map write skips the localWriteHook,
|
|
263
|
+
// so we notify the element-scoped subscription set directly.
|
|
264
|
+
reactive.notifyElement(owner, varSide!.name);
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
// Effect 1: DOM → var (fires on user input). Skipped for explicit-property
|
|
269
|
+
// bindings on non-form elements — no input/change events would drive it.
|
|
270
|
+
let stopDomToVar: (() => void) | null = null;
|
|
271
|
+
if (installDomToVar) {
|
|
272
|
+
stopDomToVar = reactive.createEffect(
|
|
273
|
+
() => {
|
|
274
|
+
reactive.trackDomProperty(el, prop);
|
|
275
|
+
return readDom();
|
|
276
|
+
},
|
|
277
|
+
newValue => {
|
|
278
|
+
writeVar(newValue);
|
|
279
|
+
},
|
|
280
|
+
owner
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Effect 2: var → DOM (fires on programmatic var writes — for globals
|
|
285
|
+
// via the core's globalWriteHook, for locals via the localWriteHook the
|
|
286
|
+
// reactivity plugin registers).
|
|
287
|
+
const stopVarToDom = reactive.createEffect(
|
|
288
|
+
() => {
|
|
289
|
+
if (varSide!.isGlobal) reactive.trackGlobal(varSide!.name);
|
|
290
|
+
else reactive.trackElement(owner, varSide!.name);
|
|
291
|
+
return readVar();
|
|
292
|
+
},
|
|
293
|
+
newValue => {
|
|
294
|
+
if (newValue === undefined) return;
|
|
295
|
+
writeDom(newValue);
|
|
296
|
+
},
|
|
297
|
+
owner
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
if (stopDomToVar) context.registerCleanup?.(owner, stopDomToVar, 'bind-dom-to-var');
|
|
301
|
+
context.registerCleanup?.(owner, stopVarToDom, 'bind-var-to-dom');
|
|
302
|
+
return undefined;
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Is this AST node a var reference we can bind to?
|
|
308
|
+
*
|
|
309
|
+
* The hyperfixi parser emits `identifier` nodes for both globals and locals,
|
|
310
|
+
* but with different shapes:
|
|
311
|
+
* - `$foo` → `{ type: 'identifier', name: '$foo' }` (no scope field)
|
|
312
|
+
* - `:foo` → `{ type: 'identifier', name: 'foo', scope: 'local' }`
|
|
313
|
+
* - `::foo` → `{ type: 'identifier', name: 'foo', scope: 'global' }`
|
|
314
|
+
*
|
|
315
|
+
* We accept all three. The legacy `$`-prefix sniff stays as a fallback because
|
|
316
|
+
* `parseExpression` doesn't always set `scope`.
|
|
317
|
+
*/
|
|
318
|
+
function isVarRef(node: ASTNode): boolean {
|
|
319
|
+
if (!node) return false;
|
|
320
|
+
if (node.type !== 'identifier') return false;
|
|
321
|
+
const scope = node.scope as string | undefined;
|
|
322
|
+
if (scope === 'local' || scope === 'global') return true;
|
|
323
|
+
const name = (node.name as string) ?? '';
|
|
324
|
+
return name.startsWith('$') || name.startsWith(':');
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function varRefInfo(node: ASTNode): { name: string; isGlobal: boolean } {
|
|
328
|
+
const rawName = (node.name as string) ?? '';
|
|
329
|
+
const scope = node.scope as string | undefined;
|
|
330
|
+
// Prefer the parser-emitted scope marker. Fall back to prefix sniffing for
|
|
331
|
+
// shapes where scope isn't set (e.g. `$foo` arriving as bare identifier
|
|
332
|
+
// with name '$foo').
|
|
333
|
+
let isGlobal: boolean;
|
|
334
|
+
let name: string;
|
|
335
|
+
if (scope === 'global') {
|
|
336
|
+
isGlobal = true;
|
|
337
|
+
name = rawName;
|
|
338
|
+
} else if (scope === 'local') {
|
|
339
|
+
isGlobal = false;
|
|
340
|
+
name = rawName;
|
|
341
|
+
} else if (rawName.startsWith('$')) {
|
|
342
|
+
isGlobal = true;
|
|
343
|
+
name = rawName.slice(1);
|
|
344
|
+
} else if (rawName.startsWith(':')) {
|
|
345
|
+
isGlobal = false;
|
|
346
|
+
name = rawName.slice(1);
|
|
347
|
+
} else {
|
|
348
|
+
isGlobal = false;
|
|
349
|
+
name = rawName;
|
|
350
|
+
}
|
|
351
|
+
if (!name) {
|
|
352
|
+
throw new Error('bind: variable reference has empty name');
|
|
353
|
+
}
|
|
354
|
+
return { name, isGlobal };
|
|
355
|
+
}
|