fix: import dispatch version guard for successful PDA actions

This commit is contained in:
2026-10-23 01:50:54 +08:00
parent 54e49e2426
commit a06f54489c
2 changed files with 7 additions and 2 deletions
+6 -1
View File
@@ -30,7 +30,12 @@ function page(dependencies = {}) {
const original = deps[api]; if (!original) continue
deps[api] = (...args) => { lastCommand = { dispatchNo: args[0], action, body: args.at(-1) }; return original(...args) }
}
const definition = new Function(...Object.keys(deps), source.replace(/^\s*import .*$/gm, '').replace('export default', 'return'))(...Object.values(deps))
// Only bind dependencies actually imported by the page; injecting every mock
// would hide missing production imports behind successful workflow tests.
const importedNames = new Set([...source.matchAll(/^\s*import\s+(?:\{([^}]+)\}|(\w+))\s+from\s+/gm)]
.flatMap(match => (match[1] || match[2]).split(',').map(name => name.trim().split(/\s+as\s+/).at(-1))))
const bindings = Object.entries(deps).filter(([name]) => importedNames.has(name))
const definition = new Function(...bindings.map(([name]) => name), source.replace(/^\s*import .*$/gm, '').replace('export default', 'return'))(...bindings.map(([, value]) => value))
const state = { ...definition.data(), current: task() }
for (const [name, method] of Object.entries(definition.methods)) state[name] = method.bind(state)
for (const [name, getter] of Object.entries(definition.computed)) Object.defineProperty(state, name, { get: () => getter.call(state) })