(
unit: (...args: any[]) => any,
{scope, safe}: {scope?: Scope; safe?: true} = {},
)
| 4 | |
| 5 | /** bind event to scope */ |
| 6 | export function scopeBind( |
| 7 | unit: (...args: any[]) => any, |
| 8 | {scope, safe}: {scope?: Scope; safe?: true} = {}, |
| 9 | ) { |
| 10 | assert(scope || forkPage || safe, 'scopeBind: scope not found') |
| 11 | const targetForkPage = scope || forkPage! |
| 12 | |
| 13 | return (...args: any[]) => { |
| 14 | let final: any |
| 15 | let failed = false |
| 16 | |
| 17 | const lastForkPage = forkPage |
| 18 | function restoreLastForkPage() { |
| 19 | setForkPage(lastForkPage) |
| 20 | } |
| 21 | |
| 22 | setForkPage(targetForkPage) |
| 23 | try { |
| 24 | final = unit(...args) |
| 25 | } catch (err) { |
| 26 | final = err |
| 27 | failed = true |
| 28 | } |
| 29 | restoreLastForkPage() |
| 30 | |
| 31 | if (failed) throw final |
| 32 | |
| 33 | if (final instanceof Promise) { |
| 34 | final.then(restoreLastForkPage, restoreLastForkPage) |
| 35 | } |
| 36 | |
| 37 | return final |
| 38 | } |
| 39 | } |
searching dependent graphs…