1d50e025 days ago18 lines · 632 B| 1 | /** Runs work once per key at a time: a caller arriving while it runs shares that run's promise. */ |
| 2 | export class SingleFlight |
| 3 | private readonly running |
| 4 | |
| 5 | run(key: |
| 6 | const pending = |
| 7 | if (pending) return |
| 8 | |
| 9 | const run = |
| 10 | this.running.set |
| 11 | return run; |
| 12 | } |
| 13 | |
| 14 | /** Resolves once the run in flight for `key`, if any, has finished, whatever its outcome. */ |
| 15 | async settle(key |
| 16 | await this.running |
| 17 | } |
| 18 | } |