| 1 | export class ApplyAbortManager { |
| 2 | private static instance: ApplyAbortManager; |
| 3 | private controllers: Map<string, AbortController>; |
| 4 | |
| 5 | private constructor() { |
| 6 | this.controllers = new Map(); |
| 7 | } |
| 8 | |
| 9 | public static getInstance(): ApplyAbortManager { |
| 10 | if (!ApplyAbortManager.instance) { |
| 11 | ApplyAbortManager.instance = new ApplyAbortManager(); |
| 12 | } |
| 13 | return ApplyAbortManager.instance; |
| 14 | } |
| 15 | |
| 16 | public get(id: string): AbortController { |
| 17 | let controller = this.controllers.get(id); |
| 18 | if (!controller) { |
| 19 | controller = new AbortController(); |
| 20 | this.controllers.set(id, controller); |
| 21 | } |
| 22 | return controller; |
| 23 | } |
| 24 | |
| 25 | public abort(id: string): void { |
| 26 | const controller = this.controllers.get(id); |
| 27 | if (controller) { |
| 28 | controller.abort(); |
| 29 | this.controllers.delete(id); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | public clear(): void { |
| 34 | this.controllers.forEach((controller) => controller.abort()); |
| 35 | this.controllers.clear(); |
| 36 | } |
| 37 | } |