(key: string, fn: () => Promise<T>)
| 67 | private readonly chains = new Map<string, Promise<unknown>>() |
| 68 | |
| 69 | withLock<T>(key: string, fn: () => Promise<T>): Promise<T> { |
| 70 | const prior = this.chains.get(key) ?? Promise.resolve() |
| 71 | // Chain after the prior holder regardless of how it settled. |
| 72 | const run = prior.then(fn, fn) |
| 73 | // Keep the chain alive but swallow rejections so one failure doesn't poison the lock. |
| 74 | this.chains.set( |
| 75 | key, |
| 76 | run.then( |
| 77 | () => undefined, |
| 78 | () => undefined, |
| 79 | ), |
| 80 | ) |
| 81 | return run |
| 82 | } |
| 83 | } |