82 lines
4.9 KiB
JavaScript
82 lines
4.9 KiB
JavaScript
;(function (global) {
|
||
'use strict'
|
||
|
||
function text(value, max) { return value === undefined || value === null ? '' : String(value).slice(0, max || 120) }
|
||
// disposer 抛错**不是**无害的清理失败:disposer 负责 abort 控制器 / 清定时器 / 释放锁,
|
||
// 它抛错可能意味着**资源没被释放**(定时器没停、请求没取消)⇒ 必须留痕,不能静默。
|
||
// CrashLog 是 src/runtime-crash-log.js 导出的模块化接口(非 app.js 的 window 全局),
|
||
// 运行期才取 ⇒ 与模块加载顺序无关。
|
||
function reportDisposerError(where, e) {
|
||
try {
|
||
const cl = global.IWBCrashLog
|
||
if (cl && typeof cl.record === 'function') {
|
||
cl.record('disposer-error', { scope: 'runtime-execution-context', message: where + ':' + String((e && e.message) || e) })
|
||
}
|
||
} catch (err) { /* ignore */ }
|
||
}
|
||
function makeId(prefix) { return text(prefix || 'run', 24) + '-' + Date.now().toString(36) + '-' + Math.random().toString(36).slice(2, 8) }
|
||
function create(input) {
|
||
const source = input || {}
|
||
const now = Date.now()
|
||
const disposers = []
|
||
const jobs = new Map()
|
||
let state = text(source.state || 'queued', 40)
|
||
let retryCount = Math.max(0, Number(source.retryCount) || 0)
|
||
let finishedAt = 0
|
||
let disposed = false
|
||
const abortController = source.controller || (typeof global.AbortController === 'function' ? new global.AbortController() : null)
|
||
const context = {
|
||
runId: text(source.runId || makeId('run'), 120),
|
||
nodeId: text(source.nodeId, 120),
|
||
taskId: text(source.taskId, 120),
|
||
startedAt: Number(source.startedAt) || now,
|
||
get state() { return state },
|
||
get retryCount() { return retryCount },
|
||
get finishedAt() { return finishedAt },
|
||
get disposed() { return disposed },
|
||
get signal() { return abortController && abortController.signal },
|
||
get controller() { return abortController },
|
||
addDisposer(disposer) {
|
||
if (typeof disposer !== 'function') return function () {}
|
||
if (disposed) { try { disposer() } catch (e) { reportDisposerError('已释放后补调 disposer', e) } ; return function () {} }
|
||
let active = true
|
||
const remove = () => {
|
||
if (!active) return
|
||
active = false
|
||
const index = disposers.indexOf(remove)
|
||
if (index >= 0) disposers.splice(index, 1)
|
||
try { disposer() } catch (e) { reportDisposerError('disposer 执行失败', e) }
|
||
}
|
||
disposers.push(remove)
|
||
return remove
|
||
},
|
||
addAbortController(controller) {
|
||
return context.addDisposer(() => { if (controller && typeof controller.abort === 'function' && (!controller.signal || !controller.signal.aborted)) controller.abort() })
|
||
},
|
||
addTimer(timer, clear) {
|
||
return context.addDisposer(() => { if (timer !== undefined && timer !== null && typeof clear === 'function') clear(timer) })
|
||
},
|
||
addJob(jobId, value) { jobs.set(text(jobId, 160), Object.assign({}, value || {}, { id: text(jobId, 160) })); return context.getJob(jobId) },
|
||
updateJob(jobId, patch) { const key = text(jobId, 160); const current = jobs.get(key) || { id: key }; const next = Object.assign({}, current, patch || {}); jobs.set(key, next); return Object.assign({}, next) },
|
||
getJob(jobId) { const value = jobs.get(text(jobId, 160)); return value ? Object.assign({}, value) : null },
|
||
jobs() { return Array.from(jobs.values()).map((value) => Object.assign({}, value)) },
|
||
retry(amount) { retryCount += Math.max(0, Number(amount) || 1); return retryCount },
|
||
transition(next) { state = text(next, 40); if (['completed', 'failed', 'cancelled', 'interrupted'].indexOf(state) >= 0) finishedAt = finishedAt || Date.now(); return state },
|
||
complete() { return context.transition('completed') },
|
||
cancel(reason) { context.cancelReason = text(reason, 160); return context.transition('cancelled') },
|
||
fail(error) { context.error = text(error && (error.message || error.code) || error, 240); return context.transition('failed') },
|
||
diagnostic(input) { return Object.assign({}, input || {}, { runId: context.runId, retryCount }) },
|
||
dispose() {
|
||
if (disposed) return
|
||
disposed = true
|
||
while (disposers.length) { const dispose = disposers.pop(); try { dispose() } catch (e) { reportDisposerError('批量释放 disposer 失败', e) } }
|
||
if (!finishedAt && ['completed', 'failed', 'cancelled', 'interrupted'].indexOf(state) >= 0) finishedAt = Date.now()
|
||
},
|
||
snapshot() { return { runId: context.runId, nodeId: context.nodeId, taskId: context.taskId, state, retryCount, startedAt: context.startedAt, finishedAt, jobs: context.jobs(), error: context.error, cancelReason: context.cancelReason } }
|
||
}
|
||
if (abortController) context.addAbortController(abortController)
|
||
return context
|
||
}
|
||
global.IWBRuntimeExecutionContext = { create }
|
||
})(typeof window !== 'undefined' ? window : globalThis)
|