| 244 | } |
| 245 | |
| 246 | export class EventQueue<T> { |
| 247 | private tail: Promise<void> = Promise.resolve() |
| 248 | private handler: (e: T) => Promise<void> |
| 249 | |
| 250 | constructor(handler: (e: T) => Promise<void>) { |
| 251 | this.handler = handler |
| 252 | } |
| 253 | |
| 254 | public enqueue(e: T): void { |
| 255 | // 每次把新任务链到上一个任务后面 |
| 256 | this.tail = this.tail |
| 257 | .then(() => this.handler(e)) |
| 258 | .catch((error) => { |
| 259 | console.error('Error processing event:', error) |
| 260 | }) |
| 261 | } |
| 262 | } |
nothing calls this directly
no outgoing calls
no test coverage detected