delay-parser.js 1.1 KB

12345678910111213141516171819202122232425
  1. /** 语句:delay 延迟(仅 value;单位 ms/s/m/h/d/w/mon/月/y,可组合,纯数字按秒) */
  2. const { assertStrictKeys } = require('../action-schema.js')
  3. const types = ['delay']
  4. function parse (action, parseContext) {
  5. const path = parseContext.actionPath || 'delay'
  6. assertStrictKeys(action, ['type', 'value'], path)
  7. if (action.value === undefined || action.value === null || String(action.value).trim() === '') {
  8. throw new Error(`${path}: delay 须使用字段 value 指定时长`)
  9. }
  10. return { type: 'delay', value: action.value }
  11. }
  12. async function execute(action, ctx) {
  13. const raw = action.value != null ? action.value : '0s'
  14. const resolved = ctx.replaceVariablesInString && ctx.variableContext
  15. ? ctx.replaceVariablesInString(String(raw), ctx.variableContext)
  16. : raw
  17. const delayMs = ctx.parseDelayString ? ctx.parseDelayString(resolved) : 0
  18. const waitMs = typeof delayMs === 'number' && delayMs > 0 ? delayMs : 0
  19. if (waitMs > 0) await new Promise(r => setTimeout(r, waitMs))
  20. return { success: true }
  21. }
  22. module.exports = { types, parse, execute }