| 19 | * SDK can send for a given edge function invocation. |
| 20 | */ |
| 21 | export class IsolatedPromiseBuffer { |
| 22 | // We just have this field because the promise buffer interface requires it. |
| 23 | // If we ever remove it from the interface we should also remove it here. |
| 24 | public $: Array<PromiseLike<TransportMakeRequestResponse>>; |
| 25 | |
| 26 | private _taskProducers: (() => PromiseLike<TransportMakeRequestResponse>)[]; |
| 27 | |
| 28 | private readonly _bufferSize: number; |
| 29 | |
| 30 | public constructor(_bufferSize = DEFAULT_TRANSPORT_BUFFER_SIZE) { |
| 31 | this.$ = []; |
| 32 | this._taskProducers = []; |
| 33 | this._bufferSize = _bufferSize; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @inheritdoc |
| 38 | */ |
| 39 | public add(taskProducer: () => PromiseLike<TransportMakeRequestResponse>): PromiseLike<TransportMakeRequestResponse> { |
| 40 | if (this._taskProducers.length >= this._bufferSize) { |
| 41 | return Promise.reject(SENTRY_BUFFER_FULL_ERROR); |
| 42 | } |
| 43 | |
| 44 | this._taskProducers.push(taskProducer); |
| 45 | return Promise.resolve({}); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @inheritdoc |
| 50 | */ |
| 51 | public drain(timeout?: number): PromiseLike<boolean> { |
| 52 | const oldTaskProducers = [...this._taskProducers]; |
| 53 | this._taskProducers = []; |
| 54 | |
| 55 | return new Promise(resolve => { |
| 56 | const timer = setTimeout(() => { |
| 57 | if (timeout && timeout > 0) { |
| 58 | resolve(false); |
| 59 | } |
| 60 | }, timeout); |
| 61 | |
| 62 | // This cannot reject |
| 63 | // eslint-disable-next-line @typescript-eslint/no-floating-promises |
| 64 | Promise.all( |
| 65 | oldTaskProducers.map(taskProducer => |
| 66 | taskProducer().then(null, () => { |
| 67 | // catch all failed requests |
| 68 | }), |
| 69 | ), |
| 70 | ).then(() => { |
| 71 | // resolve to true if all fetch requests settled |
| 72 | clearTimeout(timer); |
| 73 | resolve(true); |
| 74 | }); |
| 75 | }); |
| 76 | } |
| 77 | } |
| 78 |
nothing calls this directly
no outgoing calls
no test coverage detected