Files
image-workbench-desktop/src/plugin-api-queue.js
T

49 lines
1.6 KiB
JavaScript

/* plugin-api-queue.js —— 宿主「任务队列」能力(2026-12-03)
*
* 插件只是一个视图:账本、下载、落地、定位、记录上限全部在宿主。
* 与 gallery 能力同构:setImpl 由 Workbench 每次渲染注入,插件拿到稳定代理。
*/
;(function (global) {
'use strict'
let impl = null
const listeners = []
function setImpl (next) { impl = (next && typeof next === 'object') ? next : null }
function call (name) {
return function () {
const fn = impl && impl[name]
if (typeof fn !== 'function') return Promise.reject(new Error('任务队列能力未就绪'))
try { return Promise.resolve(fn.apply(null, arguments)) } catch (e) { return Promise.reject(e) }
}
}
function emit (reason) {
for (const fn of listeners.slice()) {
try { fn(reason || 'changed') } catch (e) { /* 插件回调异常不影响宿主 */ }
}
}
const API = {
list: call('list'),
retry: call('retry'),
restart: call('restart'),
land: call('land'),
preview: call('preview'),
copyLink: call('copyLink'),
locate: call('locate'),
stats: call('stats'),
clear: call('clear'),
getLimit: call('getLimit'),
setLimit: call('setLimit'),
onChanged: function (fn) {
if (typeof fn !== 'function') return function () {}
listeners.push(fn)
return function () { const i = listeners.indexOf(fn); if (i >= 0) listeners.splice(i, 1) }
},
}
global.IWBPluginQueue = { setImpl, api: function () { return API }, emit }
})(typeof window !== 'undefined' ? window : globalThis)