@mindees/renderer 0.5.0 → 0.7.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/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/native-app.d.ts +23 -2
- package/dist/native-app.d.ts.map +1 -1
- package/dist/native-app.js +82 -1
- package/dist/native-app.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ import { Maturity, NotImplementedError, PackageInfo, notImplemented } from "@min
|
|
|
16
16
|
/** The npm package name. */
|
|
17
17
|
declare const name = "@mindees/renderer";
|
|
18
18
|
/** The package version. All `@mindees/*` packages share one locked version line. */
|
|
19
|
-
declare const VERSION = "0.
|
|
19
|
+
declare const VERSION = "0.7.0";
|
|
20
20
|
/**
|
|
21
21
|
* Current maturity. The Helix **web/DOM** renderer (reconciler, DOM backend,
|
|
22
22
|
* headless backend, SSR + hydration) is implemented and tested. Native
|
package/dist/index.js
CHANGED
|
@@ -15,7 +15,7 @@ import { NotImplementedError, notImplemented } from "@mindees/core";
|
|
|
15
15
|
/** The npm package name. */
|
|
16
16
|
const name = "@mindees/renderer";
|
|
17
17
|
/** The package version. All `@mindees/*` packages share one locked version line. */
|
|
18
|
-
const VERSION = "0.
|
|
18
|
+
const VERSION = "0.7.0";
|
|
19
19
|
/**
|
|
20
20
|
* Current maturity. The Helix **web/DOM** renderer (reconciler, DOM backend,
|
|
21
21
|
* headless backend, SSR + hydration) is implemented and tested. Native
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { Maturity, PackageInfo } from '@mindees/core'\nimport { NotImplementedError, notImplemented } from '@mindees/core'\n\n/** Host-backend contract + capability detection. */\nexport {\n type HostBackend,\n isSerializable,\n type SerializableBackend,\n} from './backend'\n/** DOM (web) backend. */\nexport {\n createDomBackend,\n type DomDocument,\n type DomElement,\n type DomNode,\n type DomText,\n domTagFor,\n} from './dom'\n/** Keyed list reconciliation (the renderer side of core's KeyedRegion). */\nexport { bindKeyedChild } from './for'\n/** Headless (in-memory) backend — the reference/test target. */\nexport {\n createHeadlessBackend,\n createHeadlessRoot,\n type HeadlessNode,\n isEventProp,\n} from './headless'\n/**\n * Native backends. `createNativeCommandBackend` is implemented (emits a native\n * command stream); `createNativeBackend`/`createCanvasBackend` are research\n * tracks that throw `NotImplementedError`.\n */\nexport {\n type CanvasBackend,\n createCanvasBackend,\n createNativeBackend,\n createNativeCommandBackend,\n type NativeBackend,\n type NativeCommandBackend,\n type NativeCommandBackendOptions,\n type NativeCommandNode,\n} from './native'\n/** One-call native app entry — wires the command backend + host contract. */\nexport {\n type CreateNativeAppOptions,\n createNativeApp,\n type NativeApp,\n} from './native-app'\n/**\n * The strict reference native host — applies a command stream to a model tree and\n * validates it (the executable conformance contract real native hosts implement).\n */\nexport {\n createReferenceHost,\n NativeHostError,\n type ReferenceHost,\n type ReferenceHostNode,\n} from './native-host'\n/** The native command protocol: command types + serialization-safe helpers. */\nexport {\n type CreateNodeCommand,\n type CreateTextCommand,\n createNativeNodeIdFactory,\n type DisposeNodeCommand,\n type InsertChildCommand,\n isNativeCommand,\n isNativePropValue,\n type NativeCommand,\n type NativeNodeId,\n type NativePropValue,\n normalizeNativeProp,\n type RegisterEventCommand,\n type RemoveChildCommand,\n type RemovePropCommand,\n type SetPropCommand,\n type UnregisterEventCommand,\n type UpdateTextCommand,\n} from './native-protocol'\n/** Portal reconciliation (the renderer side of core's PortalRegion). */\nexport { bindPortalChild } from './portal'\n/** The fine-grained reactive reconciler. */\nexport { type Mounted, mountNode, render } from './render'\n/** Server-side rendering + hydration (web). */\nexport { hydrate, renderToString } from './ssr'\n\n/** The npm package name. */\nexport const name = '@mindees/renderer'\n\n/** The package version. All `@mindees/*` packages share one locked version line. */\nexport const VERSION = '0.
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { Maturity, PackageInfo } from '@mindees/core'\nimport { NotImplementedError, notImplemented } from '@mindees/core'\n\n/** Host-backend contract + capability detection. */\nexport {\n type HostBackend,\n isSerializable,\n type SerializableBackend,\n} from './backend'\n/** DOM (web) backend. */\nexport {\n createDomBackend,\n type DomDocument,\n type DomElement,\n type DomNode,\n type DomText,\n domTagFor,\n} from './dom'\n/** Keyed list reconciliation (the renderer side of core's KeyedRegion). */\nexport { bindKeyedChild } from './for'\n/** Headless (in-memory) backend — the reference/test target. */\nexport {\n createHeadlessBackend,\n createHeadlessRoot,\n type HeadlessNode,\n isEventProp,\n} from './headless'\n/**\n * Native backends. `createNativeCommandBackend` is implemented (emits a native\n * command stream); `createNativeBackend`/`createCanvasBackend` are research\n * tracks that throw `NotImplementedError`.\n */\nexport {\n type CanvasBackend,\n createCanvasBackend,\n createNativeBackend,\n createNativeCommandBackend,\n type NativeBackend,\n type NativeCommandBackend,\n type NativeCommandBackendOptions,\n type NativeCommandNode,\n} from './native'\n/** One-call native app entry — wires the command backend + host contract. */\nexport {\n type CreateNativeAppOptions,\n createNativeApp,\n type NativeApp,\n} from './native-app'\n/**\n * The strict reference native host — applies a command stream to a model tree and\n * validates it (the executable conformance contract real native hosts implement).\n */\nexport {\n createReferenceHost,\n NativeHostError,\n type ReferenceHost,\n type ReferenceHostNode,\n} from './native-host'\n/** The native command protocol: command types + serialization-safe helpers. */\nexport {\n type CreateNodeCommand,\n type CreateTextCommand,\n createNativeNodeIdFactory,\n type DisposeNodeCommand,\n type InsertChildCommand,\n isNativeCommand,\n isNativePropValue,\n type NativeCommand,\n type NativeNodeId,\n type NativePropValue,\n normalizeNativeProp,\n type RegisterEventCommand,\n type RemoveChildCommand,\n type RemovePropCommand,\n type SetPropCommand,\n type UnregisterEventCommand,\n type UpdateTextCommand,\n} from './native-protocol'\n/** Portal reconciliation (the renderer side of core's PortalRegion). */\nexport { bindPortalChild } from './portal'\n/** The fine-grained reactive reconciler. */\nexport { type Mounted, mountNode, render } from './render'\n/** Server-side rendering + hydration (web). */\nexport { hydrate, renderToString } from './ssr'\n\n/** The npm package name. */\nexport const name = '@mindees/renderer'\n\n/** The package version. All `@mindees/*` packages share one locked version line. */\nexport const VERSION = '0.7.0'\n\n/**\n * Current maturity. The Helix **web/DOM** renderer (reconciler, DOM backend,\n * headless backend, SSR + hydration) is implemented and tested. Native\n * (iOS/Android) and the GPU canvas are research tracks (throw\n * `NotImplementedError`). See the repository `STATUS.md`.\n */\nexport const maturity: Maturity = 'experimental'\n\n/**\n * Static identity + maturity metadata for this package. Frozen so the\n * self-reported identity tooling introspects cannot be mutated at runtime,\n * matching the `readonly` fields of {@link PackageInfo}.\n */\nexport const info: PackageInfo = Object.freeze({ name, version: VERSION, maturity })\n\nexport type { Maturity, PackageInfo }\nexport { NotImplementedError, notImplemented }\n"],"mappings":";;;;;;;;;;;;;;;AAsFA,MAAa,OAAO;;AAGpB,MAAa,UAAU;;;;;;;AAQvB,MAAa,WAAqB;;;;;;AAOlC,MAAa,OAAoB,OAAO,OAAO;CAAE;CAAM,SAAS;CAAS;AAAS,CAAC"}
|
package/dist/native-app.d.ts
CHANGED
|
@@ -2,12 +2,18 @@ import { NativeNodeId } from "./native-protocol.js";
|
|
|
2
2
|
import { MindeesNode } from "@mindees/core";
|
|
3
3
|
|
|
4
4
|
//#region src/native-app.d.ts
|
|
5
|
-
/** The contract a native host drives: render once,
|
|
5
|
+
/** The contract a native host drives: render once, dispatch events, and tick frames. */
|
|
6
6
|
interface NativeApp {
|
|
7
7
|
/** Mount the app and flush the initial command batch to the host. */
|
|
8
8
|
start(): void;
|
|
9
9
|
/** Invoke a registered handler (a native event fired), then flush the resulting batch. */
|
|
10
10
|
dispatchEvent(handlerId: string, event?: unknown): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Advance animations by one frame: forward the host's vsync timestamp (ms) to the animation
|
|
13
|
+
* engine, then flush the resulting command batch. The host calls this each vsync while the frame
|
|
14
|
+
* loop is active (see {@link CreateNativeAppOptions}). A no-op when no frame source is installed.
|
|
15
|
+
*/
|
|
16
|
+
frameTick(nowMs: number): void;
|
|
11
17
|
}
|
|
12
18
|
/** Options for {@link createNativeApp}. */
|
|
13
19
|
interface CreateNativeAppOptions {
|
|
@@ -22,11 +28,26 @@ interface CreateNativeAppOptions {
|
|
|
22
28
|
*/
|
|
23
29
|
readonly emit?: (json: string) => void;
|
|
24
30
|
/**
|
|
25
|
-
* Expose the app on a global so the host can call `start()`/`dispatchEvent()`.
|
|
31
|
+
* Expose the app on a global so the host can call `start()`/`dispatchEvent()`/`frameTick()`.
|
|
26
32
|
* `true` (default) → `globalThis.MindeesApp`; a string → that global name; `false` →
|
|
27
33
|
* don't expose (use the returned handle directly, e.g. in Node tests).
|
|
28
34
|
*/
|
|
29
35
|
readonly expose?: boolean | string;
|
|
36
|
+
/**
|
|
37
|
+
* Install the reactive scheduler that powers `startTransition`/`deferred`/normal-lane effects.
|
|
38
|
+
* `createNativeApp` owns this scheduler so it can flush the command batch after each microtask
|
|
39
|
+
* drain (otherwise deferred mutations would recompute but never reach the host). Pass `false` to
|
|
40
|
+
* run the pure synchronous lane; to use a fully custom scheduler, pass `false` and call
|
|
41
|
+
* `setReactiveScheduler` yourself (you then own flushing).
|
|
42
|
+
*/
|
|
43
|
+
readonly scheduler?: false;
|
|
44
|
+
/**
|
|
45
|
+
* Install the reactive scheduler + the vsync frame source. Default: `true` when the app is
|
|
46
|
+
* exposed AND a host (`globalThis.MindeesHost`) is present — so SSR/Node/tests install nothing
|
|
47
|
+
* and keep the synchronous jump-to-final behavior. Pass `true` to force-wire (e.g. tests driving
|
|
48
|
+
* `frameTick` directly).
|
|
49
|
+
*/
|
|
50
|
+
readonly wireEngines?: boolean;
|
|
30
51
|
}
|
|
31
52
|
/**
|
|
32
53
|
* Wire a root node to a native command host. Returns the {@link NativeApp} handle and
|
package/dist/native-app.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"native-app.d.ts","names":[],"sources":["../src/native-app.ts"],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"native-app.d.ts","names":[],"sources":["../src/native-app.ts"],"mappings":";;;;;UAmDiB,SAAA;EA2Ed;EAzED,KAAA;EAyEU;EAvEV,aAAA,CAAc,SAAA,UAAmB,KAAA;EAqEjC;;;;;EA/DA,SAAA,CAAU,KAAA;AAAA;;UASK,sBAAA;;;;;WAKN,MAAA,GAAS,YAAY;;;;;WAKrB,IAAA,IAAQ,IAAA;;;;;;WAMR,MAAA;;;;;;;;WAQA,SAAA;;;;;;;WAOA,WAAA;AAAA;;;;;iBAsBK,eAAA,CACd,IAAA,EAAM,WAAA,EACN,OAAA,GAAS,sBAAA,GACR,SAAA"}
|
package/dist/native-app.js
CHANGED
|
@@ -1,11 +1,44 @@
|
|
|
1
1
|
import { render } from "./render.js";
|
|
2
2
|
import { createNativeCommandBackend } from "./native-command-backend.js";
|
|
3
|
+
import { createScheduler, setFrameSource, setReactiveScheduler } from "@mindees/core";
|
|
3
4
|
//#region src/native-app.ts
|
|
5
|
+
/**
|
|
6
|
+
* `createNativeApp` — the one-call entry point for a MindeesNative app on an embedded
|
|
7
|
+
* native host. It hides the wiring an app author should never have to write: creating
|
|
8
|
+
* the {@link createNativeCommandBackend}, rendering the root, flushing the command
|
|
9
|
+
* batch to the host, and exposing the start/dispatch/frame contract the host calls.
|
|
10
|
+
*
|
|
11
|
+
* ```tsx
|
|
12
|
+
* import { createNativeApp } from '@mindees/renderer'
|
|
13
|
+
* import { App } from './App'
|
|
14
|
+
*
|
|
15
|
+
* createNativeApp(<App />)
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* That's the whole entry file. The native host (see `examples/native-hosts/`) injects a
|
|
19
|
+
* `MindeesHost.emit(json)` global and calls `MindeesApp.start()` once, then
|
|
20
|
+
* `MindeesApp.dispatchEvent(handlerId)` per native event — both of which this wires.
|
|
21
|
+
*
|
|
22
|
+
* On a host it also makes animations + concurrency **work by default**: it installs a
|
|
23
|
+
* reactive {@link Scheduler} (so `startTransition`/`deferred`/normal-lane effects run) and a
|
|
24
|
+
* vsync-driven {@link FrameSource} (so `timing`/`spring`/gesture animations advance). The host
|
|
25
|
+
* drives frames by calling `MindeesApp.frameTick(nowMs)` each vsync, and the engine signals when
|
|
26
|
+
* to start/stop that loop through a `MindeesHostFrame.setFrameLoopActive(boolean)` global — so the
|
|
27
|
+
* vsync loop runs **only while something is animating** (battery-friendly), tied to the animation
|
|
28
|
+
* engine's own arm/sleep. With no host (SSR / Node / tests) nothing is installed and animations
|
|
29
|
+
* jump straight to their final value, exactly as before.
|
|
30
|
+
*
|
|
31
|
+
* @module
|
|
32
|
+
*/
|
|
33
|
+
let enginesWired = false;
|
|
4
34
|
function defaultEmit(json) {
|
|
5
35
|
const host = globalThis.MindeesHost;
|
|
6
36
|
if (!host || typeof host.emit !== "function") throw new Error("createNativeApp: no `emit` was provided and globalThis.MindeesHost.emit is unavailable");
|
|
7
37
|
host.emit(json);
|
|
8
38
|
}
|
|
39
|
+
function hostIsPresent() {
|
|
40
|
+
return typeof globalThis.MindeesHost?.emit === "function";
|
|
41
|
+
}
|
|
9
42
|
/**
|
|
10
43
|
* Wire a root node to a native command host. Returns the {@link NativeApp} handle and
|
|
11
44
|
* (unless `expose: false`) publishes it as `globalThis.MindeesApp` for the host to call.
|
|
@@ -17,6 +50,51 @@ function createNativeApp(root, options = {}) {
|
|
|
17
50
|
const batch = backend.flushCommands();
|
|
18
51
|
if (batch.length > 0) emit(JSON.stringify(batch));
|
|
19
52
|
};
|
|
53
|
+
const expose = options.expose ?? true;
|
|
54
|
+
const wireEngines = options.wireEngines ?? (expose !== false && hostIsPresent());
|
|
55
|
+
let storedTick = null;
|
|
56
|
+
if (wireEngines) {
|
|
57
|
+
if (enginesWired) throw new Error("createNativeApp: the reactive engines are already wired by another app instance. Create one app per runtime, or pass `wireEngines: false` (and wire the scheduler/frame source yourself).");
|
|
58
|
+
enginesWired = true;
|
|
59
|
+
if (options.scheduler !== false) {
|
|
60
|
+
let flushQueued = false;
|
|
61
|
+
const trailingFlush = () => {
|
|
62
|
+
if (flushQueued) return;
|
|
63
|
+
flushQueued = true;
|
|
64
|
+
queueMicrotask(() => {
|
|
65
|
+
flushQueued = false;
|
|
66
|
+
flush();
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
setReactiveScheduler(createScheduler({
|
|
70
|
+
scheduleMicrotask: (cb) => queueMicrotask(() => {
|
|
71
|
+
cb();
|
|
72
|
+
trailingFlush();
|
|
73
|
+
}),
|
|
74
|
+
onError: (error) => {
|
|
75
|
+
queueMicrotask(() => {
|
|
76
|
+
throw error;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}));
|
|
80
|
+
}
|
|
81
|
+
const hostFrame = globalThis.MindeesHostFrame;
|
|
82
|
+
if (hostFrame) {
|
|
83
|
+
const frameSource = (tick) => {
|
|
84
|
+
storedTick = tick;
|
|
85
|
+
try {
|
|
86
|
+
hostFrame.setFrameLoopActive?.(true);
|
|
87
|
+
} catch {}
|
|
88
|
+
return () => {
|
|
89
|
+
storedTick = null;
|
|
90
|
+
try {
|
|
91
|
+
hostFrame.setFrameLoopActive?.(false);
|
|
92
|
+
} catch {}
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
setFrameSource(frameSource);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
20
98
|
const app = {
|
|
21
99
|
start() {
|
|
22
100
|
render(root, backend, backend.root);
|
|
@@ -26,9 +104,12 @@ function createNativeApp(root, options = {}) {
|
|
|
26
104
|
const handled = backend.dispatchEvent(handlerId, event);
|
|
27
105
|
flush();
|
|
28
106
|
return handled;
|
|
107
|
+
},
|
|
108
|
+
frameTick(nowMs) {
|
|
109
|
+
if (storedTick) storedTick(nowMs);
|
|
110
|
+
flush();
|
|
29
111
|
}
|
|
30
112
|
};
|
|
31
|
-
const expose = options.expose ?? true;
|
|
32
113
|
if (expose !== false) {
|
|
33
114
|
const name = typeof expose === "string" ? expose : "MindeesApp";
|
|
34
115
|
globalThis[name] = app;
|
package/dist/native-app.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"native-app.js","names":[],"sources":["../src/native-app.ts"],"sourcesContent":["/**\n * `createNativeApp` — the one-call entry point for a MindeesNative app on an embedded\n * native host. It hides the wiring an app author should never have to write: creating\n * the {@link createNativeCommandBackend}, rendering the root, flushing the command\n * batch to the host, and exposing the start/dispatch contract the host calls.\n *\n * ```tsx\n * import { createNativeApp } from '@mindees/renderer'\n * import { App } from './App'\n *\n * createNativeApp(<App />)\n * ```\n *\n * That's the whole entry file. The native host (see `examples/native-hosts/`) injects\n * a `MindeesHost.emit(json)` global and calls `MindeesApp.start()` once, then\n * `MindeesApp.dispatchEvent(handlerId)` per native event — both of which this wires.\n *\n * @module\n */\n\nimport type { MindeesNode } from '@mindees/core'\nimport { createNativeCommandBackend } from './native-command-backend'\nimport type { NativeNodeId } from './native-protocol'\nimport { render } from './render'\n\n/** The contract a native host drives: render once, then dispatch events by id. */\nexport interface NativeApp {\n /** Mount the app and flush the initial command batch to the host. */\n start(): void\n /** Invoke a registered handler (a native event fired), then flush the resulting batch. */\n dispatchEvent(handlerId: string, event?: unknown): boolean\n}\n\n/** Options for {@link createNativeApp}. */\nexport interface CreateNativeAppOptions {\n /**\n * Id of the host's pre-existing root container. Defaults to `\"host-root\"` — the\n * convention the reference hosts register — so the common case needs no config.\n */\n readonly rootId?: NativeNodeId\n /**\n * How to deliver a command batch to the host. Defaults to `globalThis.MindeesHost.emit`\n * (what the embedded hosts inject). Override in tests or alternative transports.\n */\n readonly emit?: (json: string) => void\n /**\n * Expose the app on a global so the host can call `start()`/`dispatchEvent()`.\n * `true` (default) → `globalThis.MindeesApp`; a string → that global name; `false` →\n * don't expose (use the returned handle directly, e.g. in Node tests).\n */\n readonly expose?: boolean | string\n}\n\nfunction defaultEmit(json: string): void {\n const host = (globalThis as { MindeesHost?: { emit?: (json: string) => void } }).MindeesHost\n if (!host || typeof host.emit !== 'function') {\n throw new Error(\n 'createNativeApp: no `emit` was provided and globalThis.MindeesHost.emit is unavailable',\n )\n }\n host.emit(json)\n}\n\n/**\n * Wire a root node to a native command host. Returns the {@link NativeApp} handle and\n * (unless `expose: false`) publishes it as `globalThis.MindeesApp` for the host to call.\n */\nexport function createNativeApp(\n root: MindeesNode,\n options: CreateNativeAppOptions = {},\n): NativeApp {\n const backend = createNativeCommandBackend({ rootId: options.rootId ?? 'host-root' })\n const emit = options.emit ?? defaultEmit\n\n const flush = (): void => {\n const batch = backend.flushCommands()\n if (batch.length > 0) emit(JSON.stringify(batch))\n }\n\n const app: NativeApp = {\n start(): void {\n render(root, backend, backend.root)\n flush()\n },\n dispatchEvent(handlerId: string, event?: unknown): boolean {\n const handled = backend.dispatchEvent(handlerId, event)\n flush()\n return handled\n },\n }\n\n const expose = options.expose ?? true\n if (expose !== false) {\n const name = typeof expose === 'string' ? expose : 'MindeesApp'\n ;(globalThis as Record<string, unknown>)[name] = app\n }\n\n return app\n}\n"],"mappings":";;;AAqDA,SAAS,YAAY,MAAoB;CACvC,MAAM,OAAQ,WAAmE;CACjF,IAAI,CAAC,QAAQ,OAAO,KAAK,SAAS,YAChC,MAAM,IAAI,MACR,wFACF;CAEF,KAAK,KAAK,IAAI;AAChB;;;;;AAMA,SAAgB,gBACd,MACA,UAAkC,CAAC,GACxB;CACX,MAAM,UAAU,2BAA2B,EAAE,QAAQ,QAAQ,UAAU,YAAY,CAAC;CACpF,MAAM,OAAO,QAAQ,QAAQ;CAE7B,MAAM,cAAoB;EACxB,MAAM,QAAQ,QAAQ,cAAc;EACpC,IAAI,MAAM,SAAS,GAAG,KAAK,KAAK,UAAU,KAAK,CAAC;CAClD;CAEA,MAAM,MAAiB;EACrB,QAAc;GACZ,OAAO,MAAM,SAAS,QAAQ,IAAI;GAClC,MAAM;EACR;EACA,cAAc,WAAmB,OAA0B;GACzD,MAAM,UAAU,QAAQ,cAAc,WAAW,KAAK;GACtD,MAAM;GACN,OAAO;EACT;CACF;CAEA,MAAM,SAAS,QAAQ,UAAU;CACjC,IAAI,WAAW,OAAO;EACpB,MAAM,OAAO,OAAO,WAAW,WAAW,SAAS;EAClD,WAAwC,QAAQ;CACnD;CAEA,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"native-app.js","names":[],"sources":["../src/native-app.ts"],"sourcesContent":["/**\n * `createNativeApp` — the one-call entry point for a MindeesNative app on an embedded\n * native host. It hides the wiring an app author should never have to write: creating\n * the {@link createNativeCommandBackend}, rendering the root, flushing the command\n * batch to the host, and exposing the start/dispatch/frame contract the host calls.\n *\n * ```tsx\n * import { createNativeApp } from '@mindees/renderer'\n * import { App } from './App'\n *\n * createNativeApp(<App />)\n * ```\n *\n * That's the whole entry file. The native host (see `examples/native-hosts/`) injects a\n * `MindeesHost.emit(json)` global and calls `MindeesApp.start()` once, then\n * `MindeesApp.dispatchEvent(handlerId)` per native event — both of which this wires.\n *\n * On a host it also makes animations + concurrency **work by default**: it installs a\n * reactive {@link Scheduler} (so `startTransition`/`deferred`/normal-lane effects run) and a\n * vsync-driven {@link FrameSource} (so `timing`/`spring`/gesture animations advance). The host\n * drives frames by calling `MindeesApp.frameTick(nowMs)` each vsync, and the engine signals when\n * to start/stop that loop through a `MindeesHostFrame.setFrameLoopActive(boolean)` global — so the\n * vsync loop runs **only while something is animating** (battery-friendly), tied to the animation\n * engine's own arm/sleep. With no host (SSR / Node / tests) nothing is installed and animations\n * jump straight to their final value, exactly as before.\n *\n * @module\n */\n\nimport {\n createScheduler,\n type FrameSource,\n type MindeesNode,\n setFrameSource,\n setReactiveScheduler,\n} from '@mindees/core'\nimport { createNativeCommandBackend } from './native-command-backend'\nimport type { NativeNodeId } from './native-protocol'\nimport { render } from './render'\n\n// The reactive scheduler + animation frame source are PROCESS globals (one per runtime). This guard\n// makes a second `createNativeApp` that would re-wire them fail loudly instead of silently stealing\n// the first app's engines. Reset with `_resetNativeAppEngines()` between tests.\nlet enginesWired = false\n\n/** @internal Test-only: reset the process-global engine-wiring guard. */\nexport function _resetNativeAppEngines(): void {\n enginesWired = false\n}\n\n/** The contract a native host drives: render once, dispatch events, and tick frames. */\nexport interface NativeApp {\n /** Mount the app and flush the initial command batch to the host. */\n start(): void\n /** Invoke a registered handler (a native event fired), then flush the resulting batch. */\n dispatchEvent(handlerId: string, event?: unknown): boolean\n /**\n * Advance animations by one frame: forward the host's vsync timestamp (ms) to the animation\n * engine, then flush the resulting command batch. The host calls this each vsync while the frame\n * loop is active (see {@link CreateNativeAppOptions}). A no-op when no frame source is installed.\n */\n frameTick(nowMs: number): void\n}\n\n/** The JS→host battery signal: the engine asks the host to run / stop its vsync loop. */\ninterface HostFrameApi {\n setFrameLoopActive?: (active: boolean) => void\n}\n\n/** Options for {@link createNativeApp}. */\nexport interface CreateNativeAppOptions {\n /**\n * Id of the host's pre-existing root container. Defaults to `\"host-root\"` — the\n * convention the reference hosts register — so the common case needs no config.\n */\n readonly rootId?: NativeNodeId\n /**\n * How to deliver a command batch to the host. Defaults to `globalThis.MindeesHost.emit`\n * (what the embedded hosts inject). Override in tests or alternative transports.\n */\n readonly emit?: (json: string) => void\n /**\n * Expose the app on a global so the host can call `start()`/`dispatchEvent()`/`frameTick()`.\n * `true` (default) → `globalThis.MindeesApp`; a string → that global name; `false` →\n * don't expose (use the returned handle directly, e.g. in Node tests).\n */\n readonly expose?: boolean | string\n /**\n * Install the reactive scheduler that powers `startTransition`/`deferred`/normal-lane effects.\n * `createNativeApp` owns this scheduler so it can flush the command batch after each microtask\n * drain (otherwise deferred mutations would recompute but never reach the host). Pass `false` to\n * run the pure synchronous lane; to use a fully custom scheduler, pass `false` and call\n * `setReactiveScheduler` yourself (you then own flushing).\n */\n readonly scheduler?: false\n /**\n * Install the reactive scheduler + the vsync frame source. Default: `true` when the app is\n * exposed AND a host (`globalThis.MindeesHost`) is present — so SSR/Node/tests install nothing\n * and keep the synchronous jump-to-final behavior. Pass `true` to force-wire (e.g. tests driving\n * `frameTick` directly).\n */\n readonly wireEngines?: boolean\n}\n\nfunction defaultEmit(json: string): void {\n const host = (globalThis as { MindeesHost?: { emit?: (json: string) => void } }).MindeesHost\n if (!host || typeof host.emit !== 'function') {\n throw new Error(\n 'createNativeApp: no `emit` was provided and globalThis.MindeesHost.emit is unavailable',\n )\n }\n host.emit(json)\n}\n\nfunction hostIsPresent(): boolean {\n const host = (globalThis as { MindeesHost?: { emit?: unknown } }).MindeesHost\n return typeof host?.emit === 'function'\n}\n\n/**\n * Wire a root node to a native command host. Returns the {@link NativeApp} handle and\n * (unless `expose: false`) publishes it as `globalThis.MindeesApp` for the host to call.\n */\nexport function createNativeApp(\n root: MindeesNode,\n options: CreateNativeAppOptions = {},\n): NativeApp {\n const backend = createNativeCommandBackend({ rootId: options.rootId ?? 'host-root' })\n const emit = options.emit ?? defaultEmit\n\n const flush = (): void => {\n const batch = backend.flushCommands()\n if (batch.length > 0) emit(JSON.stringify(batch))\n }\n\n const expose = options.expose ?? true\n const wireEngines = options.wireEngines ?? (expose !== false && hostIsPresent())\n\n // The engine's onFrame, captured when the animation loop subscribes; `frameTick` drives it.\n let storedTick: ((nowMs: number) => void) | null = null\n\n if (wireEngines) {\n if (enginesWired) {\n // The reactive scheduler + frame source are process globals; a second wiring would silently\n // steal the first app's engines. Fail loudly instead. (One app per runtime; tests reset via\n // _resetNativeAppEngines.)\n throw new Error(\n 'createNativeApp: the reactive engines are already wired by another app instance. Create one ' +\n 'app per runtime, or pass `wireEngines: false` (and wire the scheduler/frame source yourself).',\n )\n }\n enginesWired = true\n\n // A microtask-drained scheduler. After each drained task we run ONE coalesced trailing flush so\n // deferred/startTransition/normal-lane tree mutations (which land on a microtask, outside the\n // frameTick + dispatchEvent windows) still reach the host — one frame late, never dropped. We\n // OWN this scheduler so the trailing flush is always wired (a custom scheduler couldn't be).\n if (options.scheduler !== false) {\n let flushQueued = false\n const trailingFlush = (): void => {\n if (flushQueued) return\n flushQueued = true\n queueMicrotask(() => {\n flushQueued = false\n flush()\n })\n }\n setReactiveScheduler(\n createScheduler({\n scheduleMicrotask: (cb) =>\n queueMicrotask(() => {\n cb()\n trailingFlush()\n }),\n onError: (error) => {\n // Surface a scheduled-task error instead of swallowing it (uncaught → host log).\n queueMicrotask(() => {\n throw error\n })\n },\n }),\n )\n }\n\n // The vsync frame source: capture the engine's tick + signal the host to run/stop its loop. The\n // subscribe (START) fires the instant the first animation driver arms the loop; the unsubscribe\n // (STOP) fires the instant the last driver settles — so the host's vsync loop runs ONLY while\n // something animates (the battery win), with no separate heuristic to keep in sync.\n //\n // CRITICAL: only install the source when a host can actually DRIVE it (MindeesHostFrame present,\n // i.e. there's a `frameTick` caller + a `setFrameLoopActive` listener). Arming a loop that\n // nothing ticks would FREEZE animations at their start value — strictly worse than jumping to\n // the final value. With no driver, leave the source null → animations jump-to-final (safe).\n const hostFrame = (globalThis as { MindeesHostFrame?: HostFrameApi }).MindeesHostFrame\n if (hostFrame) {\n const frameSource: FrameSource = (tick) => {\n storedTick = tick\n try {\n hostFrame.setFrameLoopActive?.(true)\n } catch {\n // a throwing host signal must not break arming the loop\n }\n return () => {\n storedTick = null\n try {\n hostFrame.setFrameLoopActive?.(false)\n } catch {\n // ditto: always stop the loop even if the host signal throws\n }\n }\n }\n setFrameSource(frameSource)\n }\n }\n\n const app: NativeApp = {\n start(): void {\n render(root, backend, backend.root)\n flush()\n },\n dispatchEvent(handlerId: string, event?: unknown): boolean {\n const handled = backend.dispatchEvent(handlerId, event)\n flush()\n return handled\n },\n frameTick(nowMs: number): void {\n if (storedTick) storedTick(nowMs)\n flush()\n },\n }\n\n if (expose !== false) {\n const name = typeof expose === 'string' ? expose : 'MindeesApp'\n ;(globalThis as Record<string, unknown>)[name] = app\n }\n\n return app\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,IAAI,eAAe;AA6DnB,SAAS,YAAY,MAAoB;CACvC,MAAM,OAAQ,WAAmE;CACjF,IAAI,CAAC,QAAQ,OAAO,KAAK,SAAS,YAChC,MAAM,IAAI,MACR,wFACF;CAEF,KAAK,KAAK,IAAI;AAChB;AAEA,SAAS,gBAAyB;CAEhC,OAAO,OADO,WAAoD,aAC9C,SAAS;AAC/B;;;;;AAMA,SAAgB,gBACd,MACA,UAAkC,CAAC,GACxB;CACX,MAAM,UAAU,2BAA2B,EAAE,QAAQ,QAAQ,UAAU,YAAY,CAAC;CACpF,MAAM,OAAO,QAAQ,QAAQ;CAE7B,MAAM,cAAoB;EACxB,MAAM,QAAQ,QAAQ,cAAc;EACpC,IAAI,MAAM,SAAS,GAAG,KAAK,KAAK,UAAU,KAAK,CAAC;CAClD;CAEA,MAAM,SAAS,QAAQ,UAAU;CACjC,MAAM,cAAc,QAAQ,gBAAgB,WAAW,SAAS,cAAc;CAG9E,IAAI,aAA+C;CAEnD,IAAI,aAAa;EACf,IAAI,cAIF,MAAM,IAAI,MACR,2LAEF;EAEF,eAAe;EAMf,IAAI,QAAQ,cAAc,OAAO;GAC/B,IAAI,cAAc;GAClB,MAAM,sBAA4B;IAChC,IAAI,aAAa;IACjB,cAAc;IACd,qBAAqB;KACnB,cAAc;KACd,MAAM;IACR,CAAC;GACH;GACA,qBACE,gBAAgB;IACd,oBAAoB,OAClB,qBAAqB;KACnB,GAAG;KACH,cAAc;IAChB,CAAC;IACH,UAAU,UAAU;KAElB,qBAAqB;MACnB,MAAM;KACR,CAAC;IACH;GACF,CAAC,CACH;EACF;EAWA,MAAM,YAAa,WAAmD;EACtE,IAAI,WAAW;GACb,MAAM,eAA4B,SAAS;IACzC,aAAa;IACb,IAAI;KACF,UAAU,qBAAqB,IAAI;IACrC,QAAQ,CAER;IACA,aAAa;KACX,aAAa;KACb,IAAI;MACF,UAAU,qBAAqB,KAAK;KACtC,QAAQ,CAER;IACF;GACF;GACA,eAAe,WAAW;EAC5B;CACF;CAEA,MAAM,MAAiB;EACrB,QAAc;GACZ,OAAO,MAAM,SAAS,QAAQ,IAAI;GAClC,MAAM;EACR;EACA,cAAc,WAAmB,OAA0B;GACzD,MAAM,UAAU,QAAQ,cAAc,WAAW,KAAK;GACtD,MAAM;GACN,OAAO;EACT;EACA,UAAU,OAAqB;GAC7B,IAAI,YAAY,WAAW,KAAK;GAChC,MAAM;EACR;CACF;CAEA,IAAI,WAAW,OAAO;EACpB,MAAM,OAAO,OAAO,WAAW,WAAW,SAAS;EAClD,WAAwC,QAAQ;CACnD;CAEA,OAAO;AACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mindees/renderer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "MindeesNative Helix — fine-grained reactive renderer with a web/DOM backend, SSR + hydration, and a headless test backend. Native and GPU-canvas backends are research tracks.",
|
|
5
5
|
"license": "MIT OR Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"directory": "packages/renderer"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@mindees/core": "0.
|
|
26
|
+
"@mindees/core": "0.7.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"happy-dom": "20.9.0"
|