(load: PluginLoad, id: string, disposeTimeoutMs: number)
| 386 | } |
| 387 | |
| 388 | function createPluginScope(load: PluginLoad, id: string, disposeTimeoutMs: number) { |
| 389 | const ctrl = new AbortController() |
| 390 | let list: { key: symbol; fn: TuiDispose }[] = [] |
| 391 | let done = false |
| 392 | |
| 393 | const onDispose = (fn: TuiDispose) => { |
| 394 | if (done) return () => {} |
| 395 | const key = Symbol() |
| 396 | list.push({ key, fn }) |
| 397 | let drop = false |
| 398 | return () => { |
| 399 | if (drop) return |
| 400 | drop = true |
| 401 | list = list.filter((x) => x.key !== key) |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | const track = (fn: (() => void) | undefined) => { |
| 406 | if (!fn) return () => {} |
| 407 | let drop = false |
| 408 | let off = () => {} |
| 409 | const wrapped = () => { |
| 410 | if (drop) return |
| 411 | drop = true |
| 412 | off() |
| 413 | fn() |
| 414 | } |
| 415 | off = onDispose(wrapped) |
| 416 | return wrapped |
| 417 | } |
| 418 | |
| 419 | const lifecycle: TuiPluginApi["lifecycle"] = { |
| 420 | signal: ctrl.signal, |
| 421 | onDispose, |
| 422 | } |
| 423 | |
| 424 | const dispose = async () => { |
| 425 | if (done) return |
| 426 | done = true |
| 427 | ctrl.abort() |
| 428 | const queue = [...list].reverse() |
| 429 | list = [] |
| 430 | const until = Date.now() + disposeTimeoutMs |
| 431 | for (const item of queue) { |
| 432 | const left = until - Date.now() |
| 433 | if (left <= 0) { |
| 434 | fail("timed out cleaning up tui plugin", { |
| 435 | path: load.spec, |
| 436 | id, |
| 437 | timeout: disposeTimeoutMs, |
| 438 | }) |
| 439 | break |
| 440 | } |
| 441 | |
| 442 | const out = await runCleanup(item.fn, left) |
| 443 | if (out.type === "ok") continue |
| 444 | if (out.type === "timeout") { |
| 445 | fail("timed out cleaning up tui plugin", { |
no outgoing calls
no test coverage detected