meowjsx 0.3.0-alpha.0 → 0.3.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -16,6 +16,18 @@ Synchronous HTML output follows the same ancestor rule, but still does not suppo
16
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
+
20
+ `RuntimePlugin.prepareContainer({ runtime, container, target })` prepares a container before a target
21
+ renderer mounts into it, such as delivering a stylesheet to its document, and returns the cleanup.
22
+ The core types the container as `unknown`; the plugin narrows it by `target` (`"web"` passes the
23
+ container Element). `runtime.targetHooks.prepareContainer(container, target)` runs the hooks in
24
+ `pre`, normal, `post` order and returns one cleanup that releases them in reverse; a target calls it,
25
+ hosts do not. `meowjsx/web` `render` prepares the container before the tree mounts and releases it
26
+ after `unmount()` removes the tree. If a hook or the synchronous first render throws, what was
27
+ acquired is released and the error propagates; asynchronous render failures go to `onError` and
28
+ release nothing early. Release continues past a failing cleanup and rethrows the first error.
29
+ `renderToHtml` runs no hook. Uninstalling a plugin releases the containers it prepared, and a plugin
30
+ installed after a render reaches only later renders.
19
31
  It does not intercept module side effects or direct function calls, and does not change the meaning of the `create` / `render` hooks.
20
32
 
21
33
  ## Development metadata and instance observation
@@ -1,4 +1,4 @@
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
+ import type { AnyShadowDefinition, Child, Cleanup, Component, ComponentMeta, ComponentRegistry, ComponentRenderContext, CreateContext, CreateHook, LifecycleScope, Props, RegisteredComponent, RenderHook, ResolveComponentHook, PrepareContainerHook, Runtime, RuntimePluginEnforce, ShadowRegistry, VNode } from "./types.js";
2
2
  import { type ContextScope } from "./context.js";
3
3
  export interface HookEntry<Hook> {
4
4
  readonly handler: Hook;
@@ -8,6 +8,7 @@ export interface RuntimeState {
8
8
  createHooks: HookEntry<CreateHook>[];
9
9
  renderHooks: HookEntry<RenderHook>[];
10
10
  resolveComponentHooks: HookEntry<ResolveComponentHook>[];
11
+ containerHooks: HookEntry<PrepareContainerHook>[];
11
12
  shadowRegistry: ShadowRegistry;
12
13
  overrides: Map<string, Component<any>>;
13
14
  }
@@ -3,7 +3,7 @@ export { keyed, type KeyedChild } from "./keyed.js";
3
3
  export { createPartDescriptor, isPartDescriptor } from "./parts.js";
4
4
  export type { PartDescriptor, RefBinding, RefCallback, RefCleanup } from "./parts.js";
5
5
  import type { Child, Cleanup, ComponentMeta, Component, HFactory, ShadowDefinition, ShadowSetup, FragmentComponent, Props, Runtime, RuntimeOptions, RuntimePluginOption, VNode } from "./types.js";
6
- export type { Child, ClassValue, Cleanup, Component, ComponentMeta, ComponentMetadata, ComponentPartRef, ComponentRegistry, ComponentRegistryMap, ComponentRenderContext, Context, CreateContext, CreateHook, Effect, ElementType, FragmentComponent, Key, LifecycleScope, MaybeSignal, MountResult, Props, ReadableSignal, RegisteredComponent, RegisteredComponentName, RegisteredComponentPart, RenderContext, RenderHook, ResolveComponentContext, ResolveComponentHook, Runtime, RuntimeOptions, RuntimePlugin, RuntimePluginContext, RuntimePluginEnforce, RuntimePluginOption, RuntimePluginSetup, RuntimeRendererOptions, RuntimeTargetHooks, ShadowContext, ShadowDefinition, ShadowRegistry, ShadowSetup, Signal, StyleObject, StyleScalar, StyleValue, Theme, ThemeContext, ThemeRule, ThemeRules, ThemeStyle, VNode, WritableSignal, } from "./types.js";
6
+ export type { Child, ClassValue, Cleanup, Component, ComponentMeta, ComponentMetadata, ComponentPartRef, ComponentRegistry, ComponentRegistryMap, ComponentRenderContext, Context, CreateContext, CreateHook, Effect, ElementType, FragmentComponent, Key, LifecycleScope, MaybeSignal, MountResult, PrepareContainerHook, Props, ReadableSignal, RegisteredComponent, RegisteredComponentName, RegisteredComponentPart, RenderContext, RenderHook, ResolveComponentContext, ResolveComponentHook, Runtime, ContainerContext, RuntimeOptions, RuntimePlugin, RuntimePluginContext, RuntimePluginEnforce, RuntimePluginOption, RuntimePluginSetup, RuntimeRendererOptions, RuntimeTargetHooks, ShadowContext, ShadowDefinition, ShadowRegistry, ShadowSetup, Signal, StyleObject, StyleScalar, StyleValue, Theme, ThemeContext, ThemeRule, ThemeRules, ThemeStyle, VNode, WritableSignal, } from "./types.js";
7
7
  export { batch, isAsyncIterable, isPromiseLike, derived, isSignal, mapSignal, signal, unwrap, } from "./reactive.js";
8
8
  export { createLifecycleScope } from "./lifecycle.js";
9
9
  export { createThemePlugin, styleText } from "./style.js";
@@ -33,11 +33,14 @@ export function createRuntime(options = {}) {
33
33
  createHooks: [],
34
34
  renderHooks: [],
35
35
  resolveComponentHooks: [],
36
+ containerHooks: [],
36
37
  shadowRegistry: createShadowRegistry(options.shadows),
37
38
  overrides: new Map(Object.entries(options.overrides ?? {})),
38
39
  };
39
40
  const registry = createComponentRegistry(options.components ?? []);
40
41
  const runtime = {};
42
+ // The cleanups each mount hook still owes, so uninstalling its plugin can release them.
43
+ const activeMounts = new Map();
41
44
  const installPlugin = (plugin) => {
42
45
  let createEntry;
43
46
  let renderEntry;
@@ -87,6 +90,17 @@ export function createRuntime(options = {}) {
87
90
  renderEntry = addHook(state.renderHooks, plugin.render, plugin.enforce);
88
91
  contributionCleanups.push(() => removeHook(state.renderHooks, renderEntry));
89
92
  }
93
+ if (plugin.prepareContainer) {
94
+ const entry = addHook(state.containerHooks, plugin.prepareContainer, plugin.enforce);
95
+ const owed = new Set();
96
+ activeMounts.set(entry, owed);
97
+ contributionCleanups.push(() => {
98
+ removeHook(state.containerHooks, entry);
99
+ activeMounts.delete(entry);
100
+ });
101
+ // Runs before the hook is removed: a mount the plugin prepared does not outlive it.
102
+ contributionCleanups.push(() => disposeCleanups([...owed]));
103
+ }
90
104
  if (plugin.resolveComponent) {
91
105
  const entry = addHook(state.resolveComponentHooks, plugin.resolveComponent, plugin.enforce);
92
106
  contributionCleanups.push(() => removeHook(state.resolveComponentHooks, entry));
@@ -195,6 +209,43 @@ export function createRuntime(options = {}) {
195
209
  },
196
210
  transformVNode: (vnode) => runRenderHooks(state, runtime, vnode),
197
211
  replacementFor: (vnode) => replacementFor(state, vnode),
212
+ prepareContainer(container, target) {
213
+ const acquired = [];
214
+ try {
215
+ for (const entry of state.containerHooks.slice()) {
216
+ const cleanup = entry.handler({ runtime, container, target });
217
+ if (typeof cleanup !== "function")
218
+ continue;
219
+ const owed = activeMounts.get(entry);
220
+ let released = false;
221
+ const once = () => {
222
+ if (released)
223
+ return;
224
+ released = true;
225
+ owed?.delete(once);
226
+ cleanup();
227
+ };
228
+ owed?.add(once);
229
+ acquired.push(once);
230
+ }
231
+ }
232
+ catch (error) {
233
+ try {
234
+ disposeCleanups(acquired);
235
+ }
236
+ catch {
237
+ // Preserve the mount hook's error; rollback is best-effort.
238
+ }
239
+ throw error;
240
+ }
241
+ let active = true;
242
+ return () => {
243
+ if (!active)
244
+ return;
245
+ active = false;
246
+ disposeCleanups(acquired);
247
+ };
248
+ },
198
249
  };
199
250
  const createRenderer = (strategy, rendererOptions = {}) => {
200
251
  const defaultOnMount = (vnode) => {
@@ -150,6 +150,19 @@ export interface RuntimePluginContext {
150
150
  onCleanup(cleanup: Cleanup): void;
151
151
  }
152
152
  export type RuntimePluginSetup = (context: RuntimePluginContext) => void | Cleanup;
153
+ /** What a target renderer hands a plugin when it prepares a container for a mount. */
154
+ export interface ContainerContext {
155
+ readonly runtime: Runtime;
156
+ /** The node the target renders into; meowjsx/web passes the container Element. */
157
+ readonly container: unknown;
158
+ /** The target that mounts, such as "web"; a plugin narrows `container` by it. */
159
+ readonly target: string;
160
+ }
161
+ /**
162
+ * Prepares a container before a target renders into it, such as delivering a stylesheet to its
163
+ * document. The returned cleanup runs after that mount's tree is removed.
164
+ */
165
+ export type PrepareContainerHook = (context: ContainerContext) => void | Cleanup;
153
166
  export type RuntimePlugin = {
154
167
  name?: string;
155
168
  enforce?: RuntimePluginEnforce;
@@ -157,6 +170,8 @@ export type RuntimePlugin = {
157
170
  create?: CreateHook;
158
171
  render?: RenderHook;
159
172
  resolveComponent?: ResolveComponentHook;
173
+ /** Runs when a target renderer mounts into a container; not for static output. */
174
+ prepareContainer?: PrepareContainerHook;
160
175
  shadows?: readonly AnyShadowDefinition[];
161
176
  overrides?: Record<string, Component<any>>;
162
177
  components?: readonly RegisteredComponent[];
@@ -182,6 +197,12 @@ export interface RuntimeTargetHooks {
182
197
  };
183
198
  transformVNode(vnode: VNode): VNode;
184
199
  replacementFor(vnode: VNode): Component<any, any> | undefined;
200
+ /**
201
+ * Runs the installed plugins' `prepareContainer` hooks for one container, in plugin order, and returns
202
+ * the cleanup that releases them in reverse. A target renderer calls it before rendering into
203
+ * the container and calls the cleanup after the tree is removed.
204
+ */
205
+ prepareContainer(container: unknown, target: string): Cleanup;
185
206
  }
186
207
  export type FragmentComponent = (props: {
187
208
  children?: Child;
@@ -25,8 +25,32 @@ export function createWebRenderer(runtime = defaultRuntime, options = {}) {
25
25
  const child = options.observation
26
26
  ? h(WebObservationContext.Provider, { value: options.observation }, node)
27
27
  : node;
28
- const mounted = renderer.render(child, container, null);
29
- return { unmount: () => mounted.unmount() };
28
+ // Plugins prepare the container first, so a stylesheet or theme root exists when component
29
+ // setup and refs run; they are released after the tree is gone.
30
+ const release = runtime.targetHooks.prepareContainer(container, "web");
31
+ let mounted;
32
+ try {
33
+ mounted = renderer.render(child, container, null);
34
+ }
35
+ catch (error) {
36
+ try {
37
+ release();
38
+ }
39
+ catch {
40
+ // Preserve the render error; the release is best-effort.
41
+ }
42
+ throw error;
43
+ }
44
+ return {
45
+ unmount: () => {
46
+ try {
47
+ mounted.unmount();
48
+ }
49
+ finally {
50
+ release();
51
+ }
52
+ },
53
+ };
30
54
  };
31
55
  return {
32
56
  runtime,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "meowjsx",
3
- "version": "0.3.0-alpha.0",
3
+ "version": "0.3.0-alpha.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "sideEffects": false,