@nx-lang/previewer

中文文档
0.10.0发布于 2026/11/02MITTypeScriptES Module构建来源已验证

Unstable. Runs one NX component as a headless preview session on @nx-lang/ir-runtime: a timeline of ticks with branches, dispatch by handler, scenarios and hot reload. No UI and no framework.

原始使用文档
文档目录6 节
  1. What a session keeps
  2. Example
  3. Dispatching
  4. Hot reload
  5. Origins
  6. Options

@nx-lang/previewer

Unstable. The API and the scenario format may change incompatibly in any release, a patch release included. Pin an exact version.

Runs one NX component as a headless preview session on @nx-lang/ir-runtime. The viewer answers "does this say what I meant?"; the previewer answers "does it behave right?": click through a survey as a designer with a team of 24, step back, try "engineer", and keep both runs.

The package has no UI and no framework. A host draws the session's current output and its timeline however it likes, in a browser, a worker or Node.

What a session keeps

  • Ticks. Every render is a tick: what made it (the first props, a batch, new props or a reload), the props, the state, the rendered output, the effects the handlers returned, and where each record of the output came from. A tick never changes.
  • A tree. Going back to a tick runs nothing. Dispatching from a tick that already has children adds another child, so both continuations stay and the host can return to either.
  • Handlers by pointer. A host dispatches to the ActionHandler record it found in the output, by its JSON pointer, and the session finds the token, so the host never tracks token numbering.
  • Scenarios. The current path saves as a program.json lifecycle with a name and each entry's handler pointer, and replays in any session of the same component, after an edit too.
  • Hot reload. Given a new program, the session keeps the state when the new program accepts it, replays the path when it does not, and keeps the old program when the props no longer render.

Example

The session takes a prepared program, so compiling stays the host's. programFromImages links the images @nx-lang/sdk-wasm's generateNxIr emits, or images a host built ahead of time.

import type { NxHostRecord } from "@nx-lang/ir-runtime";
import { createPreviewSession, programFromImages, replayScenario, type PreviewImage } from "@nx-lang/previewer";

/** Runs the question flow as a designer, tries "engineer" instead, and replays the first run. */
export function previewFlow(images: readonly PreviewImage[]) {
  const program = programFromImages(images, "main.nx");
  const session = createPreviewSession(program, "Flow", { respondent: "friend" });

  // Answer through the handlers the output renders, by where they sit in it.
  const answer = (action: NxHostRecord & { $type: string }) =>
    session.dispatchHandler(`/children/1/on${action.$type.slice("Step.".length)}`, action);
  answer({ $type: "Step.TextAnswered", answer: { $type: "TextAnswer", questionId: "name", value: "Ada" } });
  answer({ $type: "Step.TextAnswered", answer: { $type: "TextAnswer", questionId: "email", value: "[email protected]" } });
  const beforeRole = session.current;
  answer({ $type: "Step.ChoiceAnswered", answer: { $type: "ChoiceAnswer", questionId: "role", selected: "designer" } });
  answer({ $type: "Step.ChoiceAnswered", answer: { $type: "ChoiceAnswer", questionId: "designTool", selected: "canvas" } });
  answer({ $type: "Step.IntegerAnswered", answer: { $type: "IntegerAnswer", questionId: "teamSize", value: 24 } });

  // Save the run before branching: a scenario is the current path, and plain JSON.
  const shared = JSON.stringify(session.scenario("designer, team of 24"));

  // Go back and try another role. Nothing is re-run, and the designer run stays in the tree.
  session.goTo(beforeRole);
  answer({ $type: "Step.ChoiceAnswered", answer: { $type: "ChoiceAnswer", questionId: "role", selected: "engineer" } });
  const branches = session.childrenOf(beforeRole).map((tick) => tick.state.role);

  // Anyone with the same program can replay the saved run.
  const { session: replayed, result } = replayScenario(program, JSON.parse(shared));
  return { branches, completed: result.completed, teamSize: replayed.current.state.teamSize };
}

previewFlow returns { branches: ["designer", "engineer"], completed: true, teamSize: 24 } for the question-flow conformance program in specs/ir-conformance/question-flow.

Dispatching

  • dispatch(batch) takes a batch as the runtime does: ActionHandlerInvocation entries naming tokens, or actions the component emits. An entry may also be { handler, action }, a handler's JSON pointer and the action for it.
  • dispatchHandler(pointer, action) dispatches one action by pointer. The action's $type must be the action the handler record names.
  • setProps(props) renders the current state with new props.

Each of these adds a child of the current tick and makes it current. A call that fails throws an NxIrRuntimeError and adds no tick. Its diagnostics are the runtime's (nx-ir-*), or the session's own:

  • nx-preview-handler: an entry names no handler in the current output, a handler for another action, or a token the output does not hold;
  • nx-preview-batch: a batch is not a list;
  • nx-preview-scenario: a scenario is malformed or for another component or module, or a path with a props change is exported;
  • nx-preview-tick: a tick is not one the session keeps;
  • nx-preview-options: maxTicks is not a positive integer;
  • nx-preview-program: programFromImages was given two images for one identity, or none for the entry.

setProps and a reload render again from the start, so the runtime numbers the new output's tokens afresh (h1-1, …). Read a token from the current tick only, or dispatch by pointer, which needs no token at all.

Hot reload

reload(program) returns what it did:

  • kept: the new program accepted the current props and state, and the output is re-rendered as a reload tick, a child of the current one.
  • replayed: the state no longer fits, so the current path ran again under the new program, from a new root. completed and stopped say how far it got.
  • failed: the props no longer render, or rendering reached one of the session's limits. The session keeps the old program and the current tick.

The reload itself drops no tick of the path it started from, whatever maxTicks says. After it, maxTicks applies as usual. The old path is off the current one, and the session drops a tick only once it has no children, so once the session is past the limit, later calls drop the old path from its last tick backward, starting with the tick the reload started from. A host that compares long runs side by side should raise maxTicks to fit both.

A tick keeps the program that rendered it: going back to a tick from before a reload and dispatching runs the old program.

Origins

Every call passes an origins report, so each tick's origins lists, for each record of its output, the module and UTF-8 byte span of the element expression that built it. originOf(tick, pointer) finds one record's. The images need their debug sections (generateNxIr({ debug: true })) for records to have origins. Pass { origins: false } to turn the reports off.

Options

createPreviewSession(program, component, props, options) takes the runtime's limits, maxOperations, maxInputSize, maxCallDepth and maxRangeLength, which apply to every call the session makes; a preview runs code a model may have written, so set them. maxInputSize bounds what the host passes, the props and the batches; the state that setProps and a reload pass back is the runtime's own and is not measured. maxTicks (1,000 by default) bounds the tree: past it, the oldest ticks off the current path that have no children are dropped, and a current path longer than the limit is kept whole.

版本与发布记录

@nx-lang/previewer 共发布 5 个版本,首次发布于 2026/10/18;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.10.0latest2026/11/021 天前
0.9.02026/10/268 天前
0.8.02026/10/1816 天前
0.0.02026/10/1816 天前
0.0.0-stage2026/10/1816 天前

关于 @nx-lang/previewer 的常见问题

@nx-lang/previewer 是什么?

不稳定的。在@nx-lang/ir-runtime上运行一个NX组件,作为一个无头预览会话:一个带有分支、处理程序调度、场景和热加载的时间轴。没有UI,没有框架。

如何安装 @nx-lang/previewer?

在项目目录运行 npm install @nx-lang/previewer,或使用 pnpm add / yarn add / bun add。

@nx-lang/previewer 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@nx-lang/previewer 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@nx-lang/previewer 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@nx-lang/previewer 可以商用吗?

@nx-lang/previewer 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@nx-lang/previewer 还在维护吗?

最新版本 0.10.0 发布于 2026/11/02(1 天前);至今共发布 5 个版本,其中过去 12 个月发布了 5 个;首次发布于 2026/10/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。