轮询触发器
定期调用端点以检查变更
轮询触发器的工作方式通常如下:
启用时: 使用上下文存储属性存储最后的时间戳或最近的项目 ID。
运行时: 此方法每 5 分钟运行一次,获取某个时间戳范围内的端点数据,或遍历直到找到最后一个项目 ID,然后以数组形式返回新项目。
测试: 你可以实现一个 test 函数,返回一些最近的项目。建议限制为五个。
示例:
轮询库
实现轮询触发器有多种策略,我们创建了一个库来帮助你完成这件事。
策略
基于时间:
此策略使用时间戳获取新项目。你需要实现 items 方法,该方法应返回最近的项目。 库将根据时间戳检测新项目。
轮询对象的泛型类型包含 props 值和对象类型。
const polling: Polling<AppConnectionValueForAuthProperty<typeof auth>, Record<string, never>> = {
strategy: DedupeStrategy.TIMEBASED,
items: async ({ propsValue, lastFetchEpochMS }) => {
// Todo 实现获取项目的逻辑
const items = [ {id: 1, created_date: '2021-01-01T00:00:00Z'}, {id: 2, created_date: '2021-01-01T00:00:00Z'}];
return items.map((item) => ({
epochMilliSeconds: dayjs(item.created_date).valueOf(),
data: item,
}));
}
}
最后 ID 策略:
此策略基于最后项目 ID 获取新项目。要使用此策略,你需要实现 items 方法,该方法应返回最近的项目。 库将检测最后项目 ID 之后的新项目。
轮询对象的泛型类型包含 props 值和对象类型。
const polling: Polling<AppConnectionValueForAuthProperty<typeof auth>, Record<string,any>> = {
strategy: DedupeStrategy.LAST_ITEM,
items: async ({ propsValue }) => {
// 实现获取项目的逻辑
const items = [{ id: 1 }, { id: 2 }];
return items.map((item) => ({
id: item.id,
data: item,
}));
}
}
触发器实现
实现轮询对象后,你可以使用轮询辅助工具来实现触发器。
export const newTicketInView = createTrigger({
name: 'new_ticket_in_view',
displayName: '视图中的新工单',
description: '当视图中创建新工单时触发',
type: TriggerStrategy.POLLING,
props: {
authentication: Property.SecretText({
displayName: '认证',
description: markdownProperty,
required: true,
}),
},
sampleData: {},
onEnable: async (context) => {
await pollingHelper.onEnable(polling, {
store: context.store,
propsValue: context.propsValue,
auth: context.auth
})
},
onDisable: async (context) => {
await pollingHelper.onDisable(polling, {
store: context.store,
propsValue: context.propsValue,
auth: context.auth
})
},
run: async (context) => {
return await pollingHelper.poll(polling, context);
},
test: async (context) => {
return await pollingHelper.test(polling, context);
}
});