| 29 | } |
| 30 | |
| 31 | export async function dispose(key: string) { |
| 32 | const entries = recordsByKey.get(key) |
| 33 | if (!entries) return |
| 34 | |
| 35 | log.info("waiting for state disposal to complete", { key }) |
| 36 | |
| 37 | let disposalFinished = false |
| 38 | |
| 39 | setTimeout(() => { |
| 40 | if (!disposalFinished) { |
| 41 | log.warn( |
| 42 | "state disposal is taking an unusually long time - if it does not complete in a reasonable time, please report this as a bug", |
| 43 | { key }, |
| 44 | ) |
| 45 | } |
| 46 | }, 10000).unref() |
| 47 | |
| 48 | const tasks: Promise<void>[] = [] |
| 49 | for (const entry of entries.values()) { |
| 50 | if (!entry.dispose) continue |
| 51 | |
| 52 | const task = Promise.resolve(entry.state) |
| 53 | .then((state) => entry.dispose!(state)) |
| 54 | .catch((error) => { |
| 55 | log.error("Error while disposing state:", { error, key }) |
| 56 | }) |
| 57 | |
| 58 | tasks.push(task) |
| 59 | } |
| 60 | entries.clear() |
| 61 | await Promise.all(tasks) |
| 62 | disposalFinished = true |
| 63 | log.info("state disposal completed", { key }) |
| 64 | } |
| 65 | } |