上下文节点
概述
在Rivet中的"Context"是一组共享输入,可以在任何图中访问,无论它们是入口点图还是由另一个图调用的子图。Context节点用于访问这些输入。
上下文节点允许您在图中访问这些值。它适用于每个图都可以访问的"全局值",而无需将这些值作为图输入进行管道传递。使用上下文节点和contextValues
为您的项目传递"全局上下文",例如当前日期。
上下文目前只能在Rivet嵌入到宿主应用程序中时设置。它通过contextValues
参数传递给runGraphInFile
或createProcessor
:
import * as Rivet from '@ironclad/rivet-node';
const contextValues = {
stringContext: 'str',
numberContext: 1,
booleanContext: true,
};
const processor = Rivet.createProcessor({
...etc,
contextValues,
});
- 输入
- 输出
- 编辑器设置
上下文节点除了编辑器设置页面的可选输入外,没有其他输入。
输出
标题 | 数据类型 | 描述 | 备注 |
---|---|---|---|
(上下文ID) | (在编辑器中配置) | 上下文变量的值。 | ID和数据类型在上下文节点的编辑器设置页面中配置。如果未设置上下文值,则将使用在编辑器设置中或通过输入端口配置的默认值。 |
编辑器设置
设置 | 描述 | 默认值 | 使用输入切换 | 输入数据类型 |
---|---|---|---|---|
ID | The ID of the context that you are pulling in. Must match exactly to the ID passed in to contextValues in the parent application. | (required) | No | N/A |
Data Type | The data type of the value passed in to contextValues . The value will be coerced to this data type if it does not match. | String | No | N/A |
Default Value | The default value, if the context value is not set. | (empty) | Yes | Same data type as the configured Data Type above. Will be coerced into the data type configured. |
示例1:访问字符串上下文值
在您的父应用程序中,将一个字符串值传递到contextValues
中:
import * as Rivet from '@ironclad/rivet-node';
const contextValues = {
stringContext: 'str',
};
const processor = Rivet.createProcessor({
...etc,
contextValues,
});
在您的图表中,添加一个Context节点。将ID
设置为stringContext
,并将Data Type
设置为String
。保持Default Value
为空。
在Remote Debugger连接时运行您的图形。Context节点的输出应为str
。
错误处理
在正常情况下,上下文节点不会出错。如果未通过contextValues
设置上下文,则将使用默认值。
常见问题
问:我可以使用Context节点传入当前日期和时间吗?
A: 是的,你可以将当前日期作为上下文值传入:
const processor = Rivet.createProcessor({
...etc,
contextValues: {
currentDate: new Date(),
},
});
然后,在您的图表中添加一个Context节点。将ID
设置为currentDate
,并将Data Type
设置为Date
。保持Default Value
为空。
问:我可以使用Context节点传入一个函数吗?
A: 不,你不能将函数作为上下文值传递。你只能传入 Data Values
问:我可以使用Context节点传入自定义数据类型吗?
A: 不,你不能传入自定义数据类型作为上下文值。你只能传入 Data Values