Runtime API / Function

create

通过模块内置的默认 Runtime 创建一个 Flamo Instance;它是 flamo.create(options) 的快捷入口,不会新建 Runtime。

使用说明

它实际做了什么

顶层 create(options) 等价于:

return flamo.create(options);

其中 flamo 是 @flamo/runtime 在模块加载时创建的默认 Runtime 单例。因此每次调用 create() 只会创建新的 Instance,不会创建新的 Runtime。

import { create, destroy } from '@flamo/runtime';
const instance = await create({
target: '#preview',
artifact,
});
await destroy(instance);

同一页面中所有顶层 create()、mount()、get()、destroy() 和 inspect() 都操作这个默认 Runtime。默认 Runtime 会复用底层 App 与资源生命周期,而每个 Instance 仍拥有自己的 id、props、节点映射和交互作用域。

target 与挂载时机

create({ artifact, target }) 会在创建 Instance 的同一步绑定目标,等价于调用默认 Runtime 的 mount(target, artifact)。如果没有 target、没有默认 canvas,Instance 不会自动附着到 DOM。

返回的 FlamoInstance 没有 mount(target) 方法,不能先无目标创建,再调用 instance.mount() 补挂载。需要 DOM/Canvas 宿主时,应在 create() 的选项中提供 target,或直接调用 mount()。

与 createFlamoRuntime() 的区别

API 创建什么 谁管理 Instance 适合场景
create(options) 一个 Instance 模块内置的默认 Runtime 单一预览、简单页面、无需自定义 Runtime 策略
createFlamoRuntime(options) 一个新的 Runtime 控制面 由你持有的 Runtime 编辑器、多画布、独立实例注册表、自定义资源池或宿主服务

如果只需要“把这个 Artifact 创建出来”,使用 create()。如果需要决定这些 Instance 与谁共享 App、由谁查询和销毁、使用哪个资源池,先调用 createFlamoRuntime(),再使用 runtime.create()。

const runtime = createFlamoRuntime({ canvas });
const instance = await runtime.create({ artifact });
runtime.get(instance.id); // 只查询这个 Runtime 的实例
await runtime.destroy(instance);

与 Card.create() 的区别

假设 Card 是 createFlamoComponentApi() 返回的组件 API:

const instance = await Card.create(props, options);

它会自动执行 Card(props) 生成 Artifact,并使用模块内置的默认 Runtime 创建 Instance。显式的 runtime.create() 不绑定某个组件 factory,调用方需要提供 Artifact 与运行态 props:

const artifact = Card(props);
const instance = await runtime.create({
...options,
artifact,
props,
});

因此,使用默认 Runtime 且希望少写样板代码时用 Card.create();需要指定 Runtime 的实例注册表、App、ResourcePool 或宿主服务时用 runtime.create()。

参数

optionsFlamoCreateOptions