|
1 |
| -const promises1 = require('./promises'); |
2 |
| -const tracer1 = require('./tracer'); |
| 1 | +import RequestTracer from './tracer'; |
| 2 | + |
| 3 | +class PromiseSettledCoordinator { |
| 4 | + constructor(finished) { |
| 5 | + this.finished = finished; |
| 6 | + this.promises = []; |
| 7 | + this.allSettled = false; |
| 8 | + } |
| 9 | + |
| 10 | + addPromise(promise) { |
| 11 | + if (this.allSettled) { |
| 12 | + throw Error('All promises have already been settled!'); |
| 13 | + } |
| 14 | + this.promises.push(promise); |
| 15 | + const currentLength = this.promises.length; |
| 16 | + const settled = Promise.allSettled(this.promises); |
| 17 | + settled.then((results) => { |
| 18 | + if (currentLength === this.promises.length) { |
| 19 | + this.allSettled = true; |
| 20 | + this.finished(results); |
| 21 | + } |
| 22 | + }); |
| 23 | + } |
| 24 | +} |
| 25 | +exports.PromiseSettledCoordinator = PromiseSettledCoordinator; |
3 | 26 |
|
4 | 27 | class TraceWrapper {
|
5 | 28 | constructor(event, listener, config) {
|
6 | 29 | this.event = event;
|
7 | 30 | this.listener = listener;
|
8 | 31 | this.waitUntilUsed = false;
|
9 | 32 | this.config = config;
|
10 |
| - this.tracer = new tracer1.RequestTracer(event.request, this.config); |
| 33 | + this.tracer = new RequestTracer(event.request, this.config); |
11 | 34 | this.waitUntilSpan = this.tracer.startChildSpan('waitUntil', 'worker');
|
12 |
| - this.settler = new promises1.PromiseSettledCoordinator(() => { |
| 35 | + this.settler = new PromiseSettledCoordinator(() => { |
13 | 36 | this.waitUntilSpan.finish();
|
14 | 37 | this.sendEvents();
|
15 | 38 | });
|
|
0 commit comments