chore: initial commit
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import type {bot_instance_context} from '../../instance/bot_instance_init.ts';
|
||||
import type {Json} from '../../../types/bot/workflow/workflow.ts';
|
||||
import type {WorkflowStartTemplate} from '../../../types/bot/workflow/metadata.ts';
|
||||
import {describe, output} from '../metadata.ts';
|
||||
|
||||
export const config_template: WorkflowStartTemplate = {
|
||||
name: 'common', version: '1.0.0',
|
||||
i18n: describe('普通启动', '手动启动工作流,也可在机器人出生后自动启动。'),
|
||||
config: [{
|
||||
name: 'auto_start', type: 'select', required: true, default: 'never',
|
||||
i18n: describe('自动启动', '选择是否在机器人出生后自动启动工作流。'),
|
||||
options: [
|
||||
{value: 'never', i18n: describe('不自动启动', '仅通过手动请求启动。')},
|
||||
{value: 'spawn', i18n: describe('出生时启动', '机器人出生并就绪后自动启动。')},
|
||||
],
|
||||
}],
|
||||
output: {
|
||||
...output('object', '启动数据', '手动启动时为调用方提供的 JSON 对象;自动启动时为 {event: "spawn"}。'),
|
||||
additional_properties: output('json', '触发字段', '手动启动时传入的 JSON 字段值。'),
|
||||
},
|
||||
};
|
||||
|
||||
export function register_common_start(context: Pick<bot_instance_context<any>, 'on' | 'off'>,
|
||||
start: (trigger: Record<string, Json>) => void, initially_ready = false): () => void {
|
||||
let disposed = false;
|
||||
let spawned = false;
|
||||
const on_spawn = () => {
|
||||
if (disposed || spawned) return;
|
||||
spawned = true;
|
||||
start({event: 'spawn'});
|
||||
};
|
||||
try { context.on('spawn', on_spawn); }
|
||||
catch (error) { context.off('spawn', on_spawn); throw error; }
|
||||
if (initially_ready) queueMicrotask(() => { if (!disposed && !spawned) on_spawn(); });
|
||||
return () => {
|
||||
if (disposed) return;
|
||||
disposed = true;
|
||||
context.off('spawn', on_spawn);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user