| 3 | import type { FooterApi, FooterEvent, RunPrompt, StreamCommit } from "@/cli/cmd/run/types" |
| 4 | |
| 5 | function footer() { |
| 6 | const prompts = new Set<(input: RunPrompt) => void>() |
| 7 | const queuedRemoves = new Set<(messageID: string) => void>() |
| 8 | const closes = new Set<() => void>() |
| 9 | const events: FooterEvent[] = [] |
| 10 | const commits: StreamCommit[] = [] |
| 11 | let closed = false |
| 12 | |
| 13 | const api: FooterApi = { |
| 14 | get isClosed() { |
| 15 | return closed |
| 16 | }, |
| 17 | onPrompt(fn) { |
| 18 | prompts.add(fn) |
| 19 | return () => { |
| 20 | prompts.delete(fn) |
| 21 | } |
| 22 | }, |
| 23 | onQueuedRemove(fn) { |
| 24 | queuedRemoves.add(fn) |
| 25 | return () => { |
| 26 | queuedRemoves.delete(fn) |
| 27 | } |
| 28 | }, |
| 29 | onClose(fn) { |
| 30 | if (closed) { |
| 31 | fn() |
| 32 | return () => {} |
| 33 | } |
| 34 | |
| 35 | closes.add(fn) |
| 36 | return () => { |
| 37 | closes.delete(fn) |
| 38 | } |
| 39 | }, |
| 40 | event(next) { |
| 41 | events.push(next) |
| 42 | }, |
| 43 | append(next) { |
| 44 | commits.push(next) |
| 45 | }, |
| 46 | idle() { |
| 47 | return Promise.resolve() |
| 48 | }, |
| 49 | close() { |
| 50 | if (closed) { |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | closed = true |
| 55 | for (const fn of [...closes]) { |
| 56 | fn() |
| 57 | } |
| 58 | }, |
| 59 | destroy() { |
| 60 | api.close() |
| 61 | prompts.clear() |
| 62 | closes.clear() |