使用说明
Instance 持有 props、事件、方法、slot 和运行态引用。它只能在创建它的 JavaScript 进程与 Runtime 生命周期内使用。
需要等待 Variant 与普通绑定以一个事务完成时,使用 setProps();它返回 FlamoPropsCommitReceipt,失败或取消不会发布部分属性状态。
Instance 能否跨模块引用
可以。在同一个页面、同一个 JavaScript realm 和同一份模块图内,Instance 是普通活对象,可以由一个 TS/JS 模块导出引用,再由另一个模块导入使用。导出的不是副本或组件定义,导入方必须处理尚未创建、已经销毁和 owner teardown 等生命周期状态。
不能做的是把 Instance 保存为 JSON、写进 Artifact、通过 structured clone / Worker / iframe / 进程边界传递,或把它当作可再次实例化的 Component Release。跨环境恢复必须保存定义与输入,再创建新的 Instance。
需要持久化或传输时,请保存以下信息:
- 用于创建它的 Artifact;
- 或 Component 模块的稳定标识、版本和 props;
- 如涉及创作变更,再保存明确的 authoring transaction。
恢复时重新导入 Artifact 或 Component,并在目标 Runtime 中再次调用 create() / mount():
const instance = await runtime.create({ artifact: importedArtifact });
// 当前生命周期结束时只销毁句柄;需要恢复的定义已另行保存await runtime.destroy(instance);Instance 没有宿主 mount()
宿主 DOM/Canvas 挂载属于 FlamoRuntime 或 FlamoComponentApi:
const instance = await runtime.mount('#preview', artifact);// 或 runtime.create({ target: '#preview', artifact })FlamoInstance 没有 mount(target)。它名称中带 mount 的方法操作的是当前实例内部:mountChild() 挂 child graph,mountComposition() 挂组合 IR,mountSlot() 分配 slot 内容;这些方法都不会把当前 Instance 重新绑定到另一个 DOM 目标。
父子 Instance 图
组合组件运行后,父 Instance 可以通过 children、child(key) 与 publicChild(key) 读取层级,也可以用 mountChild()、swapChild()、reconcileChildren() 临时修改当前运行树。
这些方法不会修改原始 Artifact,也不会生成新的 Component Release。需要把当前结构长期保存并封装为新组件时,应使用稳定 child key、精确 Component Ref、parentKey、mount、props、slots 和 routes 重建 Composition IR,再编译为新 Artifact。参见 组合多个组件并封装为新组件。
| 需要 | API |
|---|---|
| 保留现有 children,只新增一个 key | mountChild() |
| 声明完整目标列表并批量复用、新增、移除 | reconcileChildren() |
| 保留 key,替换该位置的 child 定义 | swapChild() |