meowjsx 0.1.1-alpha.1 → 0.3.0-alpha.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/JSON.md +2 -1
- package/README.md +124 -7
- package/dist/src/context.d.ts +3 -6
- package/dist/src/context.js +1 -3
- package/dist/src/json/view.d.ts +2 -2
- package/dist/src/jsx-runtime.js +13 -6
- package/dist/src/lifecycle.d.ts +2 -2
- package/dist/src/lifecycle.js +3 -4
- package/dist/src/parts.d.ts +29 -0
- package/dist/src/parts.js +102 -0
- package/dist/src/registry.d.ts +6 -5
- package/dist/src/registry.js +76 -48
- package/dist/src/renderer.d.ts +2 -3
- package/dist/src/renderer.js +11 -22
- package/dist/src/runtime.d.ts +10 -9
- package/dist/src/runtime.js +32 -44
- package/dist/src/signal.d.ts +2 -0
- package/dist/src/signal.js +1 -0
- package/dist/src/style.d.ts +5 -1
- package/dist/src/style.js +92 -6
- package/dist/src/types.d.ts +49 -73
- package/dist/web/html.js +5 -0
- package/dist/web/index.d.ts +2 -1
- package/dist/web/index.js +1 -1
- package/dist/web/observation.d.ts +1 -1
- package/dist/web/render.d.ts +0 -2
- package/dist/web/render.js +9 -6
- package/dist/web/shadows.d.ts +4 -2
- package/dist/web/shadows.js +1 -1
- package/dist/web/strategy.js +2 -1
- package/dist/web/types.d.ts +22 -0
- package/dist/web/types.js +1 -0
- package/package.json +6 -1
package/JSON.md
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
```ts
|
|
6
6
|
import { createJsonView, defineJsonComponent, jsonSchema, parseJsonView } from "meowjsx/json";
|
|
7
|
-
import { h
|
|
7
|
+
import { h } from "meowjsx";
|
|
8
|
+
import { signal, type MaybeSignal } from "meowjsx/signal";
|
|
8
9
|
|
|
9
10
|
const Label = (props: { text: MaybeSignal<string> }) => h("span", null, props.text);
|
|
10
11
|
const catalog = new Map([
|
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ Synchronous HTML output follows the same ancestor rule, but still does not suppo
|
|
|
13
13
|
`useContext` reads environment only during the component's synchronous execution. Event handlers and code after `await` must use the value captured in render beforehand; do not treat it as asynchronous local storage. Provider does not add whole-tree re-render or dependency tracking; to share a dynamic value, pass a Signal object as the value and let consumers use the existing Signal binding. Replacing a Provider's ordinary value does not automatically re-run already mounted descendants. Context's internal scope is passed by the renderer and users must not modify it.
|
|
14
14
|
|
|
15
15
|
`RuntimePlugin.resolveComponent({ component, props, runtime })` can select a replacement function before the component is called.
|
|
16
|
-
It is called in the order `pre`, normal, `post`, and within each group's registration order; the first non-`undefined` result wins, and returning the original function also stops resolution; the result is not resolved recursively. Selection happens before the lifecycle scope is created, and the selected component uses its own
|
|
16
|
+
It is called in the order `pre`, normal, `post`, and within each group's registration order; the first non-`undefined` result wins, and returning the original function also stops resolution; the result is not resolved recursively. Selection happens before the lifecycle scope is created, and the selected component uses its own metadata, parts and cleanup logic while the original component does not execute. `props` is a read-only typed view and does not copy or freeze the object.
|
|
17
17
|
|
|
18
18
|
This hook belongs to the execution runtime: pre-created VNodes, Signal/Promise deferred mounts and synchronous HTML all apply. The cleanup returned by `runtime.use()` unmounts rules and does not rewrite already mounted instances; other runtimes are unaffected.
|
|
19
19
|
It does not intercept module side effects or direct function calls, and does not change the meaning of the `create` / `render` hooks.
|
|
@@ -21,7 +21,7 @@ It does not intercept module side effects or direct function calls, and does not
|
|
|
21
21
|
## Development metadata and instance observation
|
|
22
22
|
|
|
23
23
|
Component functions may carry `metadata?: ComponentMetadata`. `ComponentMetadata` is an open type-extension interface; development tools register their own namespaces through declaration merging, and core does not interpret or read their tool fields.
|
|
24
|
-
|
|
24
|
+
`Component<Props>` preserves this constraint, and ordinary functions can use
|
|
25
25
|
`satisfies ComponentMetadata` to check assignment. Metadata is not part of props or component runtime options.
|
|
26
26
|
|
|
27
27
|
`runtime.createRenderer(strategy, { observer })` enables target-neutral instance observation:
|
|
@@ -66,7 +66,7 @@ if that outlet is missing or throws again, the error is reported asynchronously
|
|
|
66
66
|
from an existing renderer are ignored. Usually one observation center is attached to one renderer; the implementation stores by instance identity and does not overwrite because different renderers have the same numeric id. `roots` may use a getter, but the scope is read and fixed when the renderer is created, suitable for tools that set the real child before creation; it is not a runtime dynamic-switch entry. Purely host or text roots
|
|
67
67
|
have no component instances, and external DOM/styles, font loading and browser size changes also do not belong to this observation contract.
|
|
68
68
|
|
|
69
|
-
MeowJSX core: the JSX/VNode factory, Signal/batch/effect, component and part
|
|
69
|
+
MeowJSX core: the JSX/VNode factory, Signal/batch/effect, component metadata and part bindings, shadow/plugin/theme
|
|
70
70
|
contracts, lifecycle and context, and the target-neutral renderer seam (`RenderStrategy<TNode>` /
|
|
71
71
|
`createRenderer()`). Core does not know the target platform.
|
|
72
72
|
|
|
@@ -79,9 +79,12 @@ meowjsx (this package: core + JSX contract)
|
|
|
79
79
|
|
|
80
80
|
## Exports
|
|
81
81
|
|
|
82
|
-
- `.`, `./runtime`: `h`, `Fragment`, `raw`, `
|
|
83
|
-
`
|
|
84
|
-
|
|
82
|
+
- `.`, `./runtime`: `h`, `Fragment`, `raw`, `createRuntime`, `defaultRuntime`, `defineShadow`,
|
|
83
|
+
`createThemePlugin`, part descriptor helpers, plus VNode/metadata, `Context` and generic ref types.
|
|
84
|
+
Existing reactive exports remain available and share the implementation below.
|
|
85
|
+
- `./signal`: `signal`, `batch`, `mapSignal`, `derived`, `isSignal`, `unwrap`, and the
|
|
86
|
+
`ReadableSignal`, `WritableSignal`, `Signal` and `MaybeSignal` types. This DOM-free entry loads
|
|
87
|
+
the reactive implementation without creating a runtime or renderer.
|
|
85
88
|
- `./renderer`: `RenderStrategy<TNode>`, `createRenderer()`, `Renderer`/`Rendered`/
|
|
86
89
|
`RendererOptions`, `ComponentObserver`/`ObservedComponent` and other target-seam types. `createRenderer()` injects component lifecycle,
|
|
87
90
|
render hooks, overrides and `key`/`componentPart` identity; the target only implements node and attribute placement.
|
|
@@ -95,6 +98,16 @@ meowjsx (this package: core + JSX contract)
|
|
|
95
98
|
The core root has no `render`/`renderToHtml`: `render(node, Element)` and HTML serialization are imported from
|
|
96
99
|
`meowjsx/web`. The root does not import Web back; both use the same core module, and multiple explicit Runtimes can still be created independently.
|
|
97
100
|
|
|
101
|
+
### API migration
|
|
102
|
+
|
|
103
|
+
- Use `defineShadow(component, part, setup)` in place of the removed `shadow` alias. It creates a
|
|
104
|
+
definition; register it through `createRuntime({ shadows: [...] })` or a runtime plugin. Setup runs
|
|
105
|
+
when the target mounts the matching part. Core keeps the host handle generic; `meowjsx/web` types
|
|
106
|
+
it as `Element`.
|
|
107
|
+
- Use `renderer.render(node, parent, before?)` on the target-neutral renderer and
|
|
108
|
+
`web.render(node, container)` on the Web renderer. Both return an `unmount()` handle. The `mount`
|
|
109
|
+
methods and top-level Web `mount` export are removed; static output remains `renderToHtml`.
|
|
110
|
+
|
|
98
111
|
## Invariant: core contains no platform
|
|
99
112
|
|
|
100
113
|
The core source (including types) does not contain `document`, `window`, DOM `Element`/`HTMLElement`,
|
|
@@ -111,9 +124,80 @@ The command above runs from the repository root and only checks the DOM-free cor
|
|
|
111
124
|
(defaults to `unknown`); the Web module narrows it to a real element. Theme contracts (`Theme`/`ThemeRule`/
|
|
112
125
|
`ThemeStyle` and their merge rules) stay in core as planned; they carry values only and touch no style API.
|
|
113
126
|
|
|
127
|
+
## Class lists
|
|
128
|
+
|
|
129
|
+
`className` accepts strings, stringifiable class values, nested readonly arrays, and Signals of
|
|
130
|
+
those values. Arrays keep their order, join with spaces, and omit `false`, `null`, `undefined` and
|
|
131
|
+
empty strings. They do not merge conflicting classes or interpret objects as class dictionaries.
|
|
132
|
+
The native `class` attribute follows the same runtime normalization.
|
|
133
|
+
|
|
134
|
+
```tsx
|
|
135
|
+
import { h } from "meowjsx";
|
|
136
|
+
import { signal } from "meowjsx/signal";
|
|
137
|
+
|
|
138
|
+
const stateClass = signal("is-idle");
|
|
139
|
+
<div className={["panel", [false, stateClass], "host-panel"]} />;
|
|
140
|
+
stateClass.value = "is-active";
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Static HTML reads current values without subscribing. Mounted nodes subscribe to Signals at any
|
|
144
|
+
list depth, update their class attribute in place, and release replaced sources and unmount
|
|
145
|
+
subscriptions. Theme-provided class lists compose with the caller's classes in the same way. The
|
|
146
|
+
public `ClassValue` type is available to components that forward `className`.
|
|
147
|
+
|
|
148
|
+
## Components and refs
|
|
149
|
+
|
|
150
|
+
Components are ordinary functions. A static `meta` declares their stable name and finite parts;
|
|
151
|
+
`Context<typeof Component>` supplies the lifecycle and a typed `parts` map. Use
|
|
152
|
+
`Context<typeof Component, ComponentProps>` when setup also needs the declared types of
|
|
153
|
+
`context.props`. Setup runs once per mount, and Signals carry later changes. Import `signal`
|
|
154
|
+
directly from `meowjsx/signal` and create local state inside setup; each mount creates its own Signals.
|
|
155
|
+
`context.effect` and `context.onCleanup` manage effects and cleanup for that mount. Signals have no
|
|
156
|
+
dispose operation; the renderer releases its binding subscriptions on unmount. Register any manual
|
|
157
|
+
subscription's unsubscribe function with `context.onCleanup`.
|
|
158
|
+
|
|
159
|
+
```tsx
|
|
160
|
+
import { h, type Context, type Props } from "meowjsx";
|
|
161
|
+
import type { WebRef } from "meowjsx/web";
|
|
162
|
+
|
|
163
|
+
interface PanelProps extends Props {
|
|
164
|
+
title: string;
|
|
165
|
+
ref?: WebRef<HTMLElement>;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function Panel({ title, children, ref, ...props }: PanelProps, ctx: Context<typeof Panel>) {
|
|
169
|
+
return (
|
|
170
|
+
<section {...props} ref={[ctx.parts.root, ref]}>
|
|
171
|
+
<h2 ref={ctx.parts.heading}>{title}</h2>
|
|
172
|
+
<div ref={ctx.parts.body}>{children}</div>
|
|
173
|
+
</section>
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
Panel.meta = { name: "panel", parts: ["root", "heading", "body"] } as const;
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
`ref` accepts nested readonly arrays of part descriptors, node callbacks and `false`/nullish
|
|
180
|
+
omissions. Callbacks run in order at the existing host-property mount point; returned synchronous
|
|
181
|
+
cleanups run in reverse order. A failure still releases acquired cleanups. Static HTML extracts part
|
|
182
|
+
identity without running callbacks. A host node accepts one distinct part identity, and a descriptor
|
|
183
|
+
can mark several nodes. Component refs remain props and are forwarded explicitly to the intended
|
|
184
|
+
host node.
|
|
185
|
+
|
|
186
|
+
Generic ref types stay in the DOM-free root. Importing the explicit `meowjsx/web` type surface adds
|
|
187
|
+
HTML/SVG node inference for native JSX refs. Register component functions with `components: [Panel]`;
|
|
188
|
+
metadata does not register itself. The [parts and ref-binding design](../../docs/designs/2026-10-24-component-parts-ref-bindings.md)
|
|
189
|
+
records the breaking migration and theme adapter.
|
|
190
|
+
|
|
191
|
+
## Bundling
|
|
192
|
+
|
|
193
|
+
The package declares `sideEffects: false` and ships one ESM module per source file. The default
|
|
194
|
+
runtime, JSX runtime and Web renderer are created by `/* @__PURE__ */` calls, and their exported
|
|
195
|
+
members (`h`, `Fragment`, `jsx`, `render` and the rest) are pure reads, so a bundle that imports only
|
|
196
|
+
Signals, Context or types leaves the runtime and renderer out.
|
|
197
|
+
|
|
114
198
|
## Verification
|
|
115
199
|
|
|
116
|
-
- `bun test packages/meowjsx/tests/unit`: Signal, VNode, plugin,
|
|
200
|
+
- `bun test packages/meowjsx/tests/unit`: Signal, VNode, plugin, metadata, part/ref bindings, fake target seam and Web host tests.
|
|
117
201
|
- `tests/types/` checks core and Web type cases separately; the core type environment does not include the DOM.
|
|
118
202
|
|
|
119
203
|
## Web module
|
|
@@ -136,6 +220,36 @@ its lifecycle is managed by the consumer, and no global observation tree is crea
|
|
|
136
220
|
Public JS artifacts preserve module relationships: the root, runtime, automatic JSX and Web entries reference the same core.
|
|
137
221
|
Release builds must not copy a defaultRuntime, Context or reactive scheduler per entry.
|
|
138
222
|
|
|
223
|
+
## Signal interoperability
|
|
224
|
+
|
|
225
|
+
Import reactive functions and types from `meowjsx/signal`:
|
|
226
|
+
|
|
227
|
+
```ts
|
|
228
|
+
import { signal, mapSignal, derived, batch, type ReadableSignal } from "meowjsx/signal";
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
The renderer's `SignalLike<T>` type from `meowjsx/renderer` describes its structural input protocol.
|
|
232
|
+
MeowJSX's `ReadableSignal<T>` and compatible external implementations satisfy that protocol. JSX
|
|
233
|
+
bindings accept any object with this shape; no MeowJSX constructor or brand is required:
|
|
234
|
+
|
|
235
|
+
```ts
|
|
236
|
+
interface SignalLike<T> {
|
|
237
|
+
readonly value: T;
|
|
238
|
+
subscribe(listener: (value: T) => void): () => void;
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
`value` returns the current value. `subscribe` reports changed values and returns an unsubscribe
|
|
243
|
+
function. The renderer reads the initial value, subscribes for updates and unsubscribes on unmount.
|
|
244
|
+
`mapSignal(source, map)` also subscribes explicitly, so it works with external implementations.
|
|
245
|
+
A third-party API with a different shape needs an adapter to this contract.
|
|
246
|
+
|
|
247
|
+
Automatic dependency tracking in `context.effect` and `derived` requires getters from the shared
|
|
248
|
+
MeowJSX reactive implementation. Reading an external Signal's `.value` alone does not register it as
|
|
249
|
+
a dependency; this also applies to component logic that uses those computations. Use `mapSignal`
|
|
250
|
+
for an explicit source, or subscribe directly and register the unsubscribe function with
|
|
251
|
+
`context.onCleanup`. The root, runtime and signal entries share one reactive module and scheduler.
|
|
252
|
+
|
|
139
253
|
## Derived signals
|
|
140
254
|
|
|
141
255
|
`derived(read)` is a read-only signal. `read` runs again when any signal it read changes, and subscribers hear the value when `Object.is` says it changed. `read` must not write signals. One source can also use `mapSignal`. A computation is not stored by writing another signal from `effect`.
|
|
@@ -146,6 +260,9 @@ Release builds must not copy a defaultRuntime, Context or reactive scheduler per
|
|
|
146
260
|
a deletion releases that row's lifecycle, and reordering moves the existing range. Whole-replacement semantics for plain arrays and Signals are unchanged.
|
|
147
261
|
|
|
148
262
|
```ts
|
|
263
|
+
import { h, keyed } from "meowjsx";
|
|
264
|
+
import { mapSignal, signal } from "meowjsx/signal";
|
|
265
|
+
|
|
149
266
|
const items = signal([{ id: "one", label: "First" }]);
|
|
150
267
|
const rows = keyed(
|
|
151
268
|
items,
|
package/dist/src/context.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Child, Component } from "./runtime.js";
|
|
2
2
|
/** A render-tree environment. Targets carry it through deferred child regions. */
|
|
3
3
|
export type ContextScope = ReadonlyMap<symbol, unknown>;
|
|
4
|
-
export interface
|
|
4
|
+
export interface ContextValue<T> {
|
|
5
5
|
readonly key: symbol;
|
|
6
6
|
readonly defaultValue: T;
|
|
7
7
|
readonly Provider: Component<{
|
|
@@ -9,11 +9,8 @@ export interface Context<T> {
|
|
|
9
9
|
children?: Child;
|
|
10
10
|
}>;
|
|
11
11
|
}
|
|
12
|
-
/** Semantic identity for runtime observation, independent of function names. */
|
|
13
12
|
export declare function isContextProvider(component: Function): boolean;
|
|
14
|
-
export declare function createContext<T>(defaultValue: T):
|
|
15
|
-
|
|
16
|
-
export declare function useContext<T>(context: Context<T>): T;
|
|
17
|
-
/** Runtime/target seam, not a process-wide persistent provider stack. */
|
|
13
|
+
export declare function createContext<T>(defaultValue: T): ContextValue<T>;
|
|
14
|
+
export declare function useContext<T>(context: ContextValue<T>): T;
|
|
18
15
|
export declare function withContextScope<T>(scope: ContextScope | undefined, run: () => T): T;
|
|
19
16
|
export declare function contextScopeForProvider(component: Function, value: unknown, parent: ContextScope | undefined): ContextScope | undefined;
|
package/dist/src/context.js
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
const providers = new WeakMap();
|
|
2
2
|
let activeScope;
|
|
3
|
-
/** Semantic identity for runtime observation, independent of function names. */
|
|
4
3
|
export function isContextProvider(component) {
|
|
5
4
|
return providers.has(component);
|
|
6
5
|
}
|
|
6
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
7
7
|
export function createContext(defaultValue) {
|
|
8
8
|
const key = Symbol("meowjsx.context");
|
|
9
9
|
const Provider = ({ children }) => children ?? null;
|
|
10
10
|
providers.set(Provider, key);
|
|
11
11
|
return Object.freeze({ key, defaultValue, Provider });
|
|
12
12
|
}
|
|
13
|
-
/** Read synchronously while rendering; capture the value before an await/callback. */
|
|
14
13
|
export function useContext(context) {
|
|
15
14
|
return activeScope?.has(context.key) ? activeScope.get(context.key) : context.defaultValue;
|
|
16
15
|
}
|
|
17
|
-
/** Runtime/target seam, not a process-wide persistent provider stack. */
|
|
18
16
|
export function withContextScope(scope, run) {
|
|
19
17
|
const previous = activeScope;
|
|
20
18
|
activeScope = scope;
|
package/dist/src/json/view.d.ts
CHANGED
|
@@ -30,11 +30,11 @@ export type JsonPropsDefinition<P> = {
|
|
|
30
30
|
readonly [K in Exclude<KnownKeys<P>, "children">]?: NonNullable<P[K]> extends Callback ? JsonActionProperty<Extract<NonNullable<P[K]>, Callback>> : JsonValueProperty<P[K]>;
|
|
31
31
|
};
|
|
32
32
|
export interface JsonComponentDefinition<P extends object = any> {
|
|
33
|
-
readonly component: Component<P, any
|
|
33
|
+
readonly component: Component<P, any>;
|
|
34
34
|
readonly props: JsonPropsDefinition<P>;
|
|
35
35
|
}
|
|
36
36
|
export type JsonCatalog = ReadonlyMap<string, JsonComponentDefinition<any>>;
|
|
37
|
-
export declare function defineJsonComponent<P extends object>(component: Component<P, any
|
|
37
|
+
export declare function defineJsonComponent<P extends object>(component: Component<P, any>, props: JsonPropsDefinition<NoInfer<P>>): JsonComponentDefinition<P>;
|
|
38
38
|
export type JsonNode = string | number | boolean | null | readonly JsonNode[] | {
|
|
39
39
|
readonly component: string;
|
|
40
40
|
readonly props?: Readonly<Record<string, JsonValue>>;
|
package/dist/src/jsx-runtime.js
CHANGED
|
@@ -3,7 +3,9 @@ function createJSX(runtime, type, props, key) {
|
|
|
3
3
|
const { children, ...rest } = props ?? {};
|
|
4
4
|
if (key !== undefined)
|
|
5
5
|
rest.key = key;
|
|
6
|
-
return children === undefined
|
|
6
|
+
return children === undefined
|
|
7
|
+
? runtime.h(type, rest)
|
|
8
|
+
: runtime.h(type, rest, children);
|
|
7
9
|
}
|
|
8
10
|
export function createJSXRuntime(runtime) {
|
|
9
11
|
const jsx = (type, props, key) => createJSX(runtime, type, props, key);
|
|
@@ -15,8 +17,13 @@ export function createJSXRuntime(runtime) {
|
|
|
15
17
|
jsxDEV,
|
|
16
18
|
};
|
|
17
19
|
}
|
|
18
|
-
const defaultJSXRuntime = createJSXRuntime(defaultRuntime);
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
const defaultJSXRuntime = /* @__PURE__ */ createJSXRuntime(defaultRuntime);
|
|
21
|
+
// A pure read lets bundlers drop the default JSX runtime when none of these members is used.
|
|
22
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
23
|
+
function defaultMember(key) {
|
|
24
|
+
return defaultJSXRuntime[key];
|
|
25
|
+
}
|
|
26
|
+
export const Fragment = /* @__PURE__ */ defaultMember("Fragment");
|
|
27
|
+
export const jsx = /* @__PURE__ */ defaultMember("jsx");
|
|
28
|
+
export const jsxs = /* @__PURE__ */ defaultMember("jsxs");
|
|
29
|
+
export const jsxDEV = /* @__PURE__ */ defaultMember("jsxDEV");
|
package/dist/src/lifecycle.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { Cleanup, LifecycleScope, Runtime } from "./types.js";
|
|
2
2
|
/**
|
|
3
3
|
* Create a lifecycle scope. Components receive one per invocation; a target
|
|
4
|
-
* that mounts a shadow uses the same primitive so
|
|
5
|
-
*
|
|
4
|
+
* that mounts a shadow uses the same primitive so effects and cleanups follow
|
|
5
|
+
* one ownership rule.
|
|
6
6
|
*/
|
|
7
7
|
export declare function createLifecycleScope(runtime: Runtime): LifecycleScope & {
|
|
8
8
|
dispose(): void;
|
package/dist/src/lifecycle.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { createReactiveEffect
|
|
1
|
+
import { createReactiveEffect } from "./reactive.js";
|
|
2
2
|
/**
|
|
3
3
|
* Create a lifecycle scope. Components receive one per invocation; a target
|
|
4
|
-
* that mounts a shadow uses the same primitive so
|
|
5
|
-
*
|
|
4
|
+
* that mounts a shadow uses the same primitive so effects and cleanups follow
|
|
5
|
+
* one ownership rule.
|
|
6
6
|
*/
|
|
7
7
|
export function createLifecycleScope(runtime) {
|
|
8
8
|
let active = true;
|
|
@@ -23,7 +23,6 @@ export function createLifecycleScope(runtime) {
|
|
|
23
23
|
};
|
|
24
24
|
return {
|
|
25
25
|
runtime,
|
|
26
|
-
signal,
|
|
27
26
|
effect(run) {
|
|
28
27
|
const stop = createReactiveEffect(run);
|
|
29
28
|
onCleanup(stop);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Cleanup } from "./types.js";
|
|
2
|
+
/** An opaque component and part identity carried by a host VNode. */
|
|
3
|
+
export interface PartDescriptor<Name extends string = string, Part extends string = string> {
|
|
4
|
+
readonly component: Name;
|
|
5
|
+
readonly part: Part;
|
|
6
|
+
readonly state?: Readonly<Record<string, unknown>>;
|
|
7
|
+
}
|
|
8
|
+
/** Create a descriptor by stable component and part names. */
|
|
9
|
+
export declare function createPartDescriptor<Name extends string, Part extends string>(component: Name, part: Part, state?: Readonly<Record<string, unknown>>): PartDescriptor<Name, Part>;
|
|
10
|
+
export declare function isPartDescriptor(value: unknown): value is PartDescriptor;
|
|
11
|
+
export type RefCleanup = () => void | {
|
|
12
|
+
readonly then?: never;
|
|
13
|
+
};
|
|
14
|
+
export type RefCallback<TNode> = (node: TNode) => void | RefCleanup;
|
|
15
|
+
export type RefBinding<TNode> = RefCallback<TNode> | PartDescriptor | false | null | undefined | readonly RefBinding<TNode>[];
|
|
16
|
+
declare const normalizedRef: unique symbol;
|
|
17
|
+
export interface NormalizedRef<TNode = unknown> {
|
|
18
|
+
readonly [normalizedRef]: true;
|
|
19
|
+
readonly callbacks: readonly RefCallback<TNode>[];
|
|
20
|
+
}
|
|
21
|
+
/** Flatten composed refs and resolve the single host part identity before hooks. */
|
|
22
|
+
export declare function normalizeRefBinding<TNode>(value: unknown): {
|
|
23
|
+
readonly ref?: NormalizedRef<TNode>;
|
|
24
|
+
readonly part?: PartDescriptor;
|
|
25
|
+
};
|
|
26
|
+
export declare function isNormalizedRef(value: unknown): value is NormalizedRef;
|
|
27
|
+
/** Invoke callbacks in order and release their successful cleanups in reverse order. */
|
|
28
|
+
export declare function mountRefBindings<TNode>(ref: NormalizedRef<TNode>, node: TNode): Cleanup;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
const descriptors = new WeakSet();
|
|
2
|
+
/** Create a descriptor by stable component and part names. */
|
|
3
|
+
export function createPartDescriptor(component, part, state) {
|
|
4
|
+
const descriptor = Object.freeze({
|
|
5
|
+
component,
|
|
6
|
+
part,
|
|
7
|
+
...(state === undefined ? {} : { state }),
|
|
8
|
+
});
|
|
9
|
+
descriptors.add(descriptor);
|
|
10
|
+
return descriptor;
|
|
11
|
+
}
|
|
12
|
+
export function isPartDescriptor(value) {
|
|
13
|
+
return typeof value === "object" && value !== null && descriptors.has(value);
|
|
14
|
+
}
|
|
15
|
+
const normalizedRef = Symbol("meowjsx.normalized-ref");
|
|
16
|
+
/** Flatten composed refs and resolve the single host part identity before hooks. */
|
|
17
|
+
export function normalizeRefBinding(value) {
|
|
18
|
+
const callbacks = [];
|
|
19
|
+
let part;
|
|
20
|
+
const visit = (entry) => {
|
|
21
|
+
if (entry === undefined || entry === null || entry === false)
|
|
22
|
+
return;
|
|
23
|
+
if (Array.isArray(entry)) {
|
|
24
|
+
for (const nested of entry)
|
|
25
|
+
visit(nested);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (isPartDescriptor(entry)) {
|
|
29
|
+
if (part && (part.component !== entry.component || part.part !== entry.part))
|
|
30
|
+
throw new Error("meowjsx host refs cannot bind multiple distinct parts");
|
|
31
|
+
if (!part || (part.state === undefined && entry.state !== undefined))
|
|
32
|
+
part = entry;
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (typeof entry === "function") {
|
|
36
|
+
callbacks.push(entry);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
throw new Error("meowjsx ref bindings must be callbacks, part descriptors, or nested arrays");
|
|
40
|
+
};
|
|
41
|
+
visit(value);
|
|
42
|
+
return {
|
|
43
|
+
...(callbacks.length > 0
|
|
44
|
+
? {
|
|
45
|
+
ref: Object.freeze({
|
|
46
|
+
[normalizedRef]: true,
|
|
47
|
+
callbacks: Object.freeze(callbacks),
|
|
48
|
+
}),
|
|
49
|
+
}
|
|
50
|
+
: {}),
|
|
51
|
+
...(part ? { part } : {}),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export function isNormalizedRef(value) {
|
|
55
|
+
return (typeof value === "object" &&
|
|
56
|
+
value !== null &&
|
|
57
|
+
Object.hasOwn(value, normalizedRef) &&
|
|
58
|
+
value[normalizedRef] === true);
|
|
59
|
+
}
|
|
60
|
+
/** Invoke callbacks in order and release their successful cleanups in reverse order. */
|
|
61
|
+
export function mountRefBindings(ref, node) {
|
|
62
|
+
const cleanups = [];
|
|
63
|
+
let active = true;
|
|
64
|
+
const release = () => {
|
|
65
|
+
if (!active)
|
|
66
|
+
return;
|
|
67
|
+
active = false;
|
|
68
|
+
let firstError;
|
|
69
|
+
let failed = false;
|
|
70
|
+
for (let index = cleanups.length - 1; index >= 0; index -= 1) {
|
|
71
|
+
try {
|
|
72
|
+
cleanups[index]();
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
if (!failed) {
|
|
76
|
+
failed = true;
|
|
77
|
+
firstError = error;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
cleanups.length = 0;
|
|
82
|
+
if (failed)
|
|
83
|
+
throw firstError;
|
|
84
|
+
};
|
|
85
|
+
try {
|
|
86
|
+
for (const callback of ref.callbacks) {
|
|
87
|
+
const cleanup = callback(node);
|
|
88
|
+
if (typeof cleanup === "function")
|
|
89
|
+
cleanups.push(cleanup);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
try {
|
|
94
|
+
release();
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
// Keep the callback failure primary while attempting every rollback cleanup.
|
|
98
|
+
}
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
return release;
|
|
102
|
+
}
|
package/dist/src/registry.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import type { AnyShadowDefinition, Child, Cleanup, Component, ComponentMeta, ComponentRegistry, ComponentRenderContext, CreateContext, CreateHook, LifecycleScope, Props, RegisteredComponent, RenderHook, ResolveComponentHook, Runtime, RuntimePluginEnforce, ShadowRegistry, VNode } from "./types.js";
|
|
1
2
|
import { type ContextScope } from "./context.js";
|
|
2
|
-
import type { AnyShadowDefinition, Child, Cleanup, Component, ComponentContract, ComponentRegistry, ComponentRenderContext, CreateContext, CreateHook, LifecycleScope, Props, RenderHook, ResolveComponentHook, Runtime, RuntimePluginEnforce, ShadowRegistry, VNode } from "./types.js";
|
|
3
3
|
export interface HookEntry<Hook> {
|
|
4
4
|
readonly handler: Hook;
|
|
5
5
|
readonly enforce?: RuntimePluginEnforce;
|
|
@@ -13,16 +13,17 @@ export interface RuntimeState {
|
|
|
13
13
|
}
|
|
14
14
|
export declare function addHook<Hook>(hooks: HookEntry<Hook>[], handler: Hook, enforce: RuntimePluginEnforce | undefined): HookEntry<Hook>;
|
|
15
15
|
export declare function removeHook<Hook>(hooks: HookEntry<Hook>[], entry: HookEntry<Hook>): void;
|
|
16
|
-
export declare function createComponentRegistry(initial: readonly
|
|
16
|
+
export declare function createComponentRegistry(initial: readonly RegisteredComponent[]): ComponentRegistry;
|
|
17
17
|
export declare function createShadowRegistry(initial?: readonly AnyShadowDefinition[]): ShadowRegistry;
|
|
18
|
-
export declare function createComponentContext<
|
|
18
|
+
export declare function createComponentContext<Meta extends ComponentMeta, PropsType extends object>(runtime: Runtime, component: Component<PropsType, Meta>, props: PropsType, scope: LifecycleScope): ComponentRenderContext<Meta, PropsType>;
|
|
19
19
|
interface ComponentInvocation {
|
|
20
20
|
child: Child;
|
|
21
21
|
dispose: Cleanup;
|
|
22
22
|
contextScope?: ContextScope;
|
|
23
23
|
}
|
|
24
|
-
export declare function invokeComponent(component: Component<any, any
|
|
24
|
+
export declare function invokeComponent(component: Component<any, any>, props: Props, runtime: Runtime, contextScope?: ContextScope): ComponentInvocation;
|
|
25
25
|
export declare function createBaseVNode(context: CreateContext): VNode;
|
|
26
26
|
export declare function runCreateHooks(state: RuntimeState, context: CreateContext): VNode;
|
|
27
27
|
export declare function runRenderHooks(state: RuntimeState, runtime: Runtime, vnode: VNode): VNode;
|
|
28
|
-
export
|
|
28
|
+
export declare function normalizeHostVNode(vnode: VNode): VNode;
|
|
29
|
+
export { isNormalizedRef, isPartDescriptor, mountRefBindings } from "./parts.js";
|