Skip to content

Commit

Permalink
ref: Leave only valid buffer implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed Jun 24, 2021
1 parent 740443d commit 13894ff
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions packages/utils/src/promisebuffer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { SentryError } from './error';
import { isThenable } from './is';
import { SyncPromise } from './syncpromise';

type TaskProducer<T> = () => PromiseLike<T>;

/** A simple queue that holds promises. */
export class PromiseBuffer<T> {
/** Internal set of queued Promises */
Expand All @@ -21,22 +18,14 @@ export class PromiseBuffer<T> {
/**
* Add a promise to the queue.
*
* @param taskProducer A function producing any PromiseLike<T>; In previous versions this used to be `@param task: PromiseLike<T>`, however, Promises were instantly created on the call-site, making them fall through the buffer limit.
* @param taskProducer A function producing any PromiseLike<T>.
* @returns The original promise.
*/
public add(taskProducer: PromiseLike<T> | TaskProducer<T>): PromiseLike<T> {
// NOTE: This is necessary to preserve backwards compatibility
// It should accept _only_ `TaskProducer<T>` but we dont want to break other custom transports
// that are utilizing our `Buffer` implementation.
// see: https://github.com/getsentry/sentry-javascript/issues/3725
const normalizedTaskProducer: TaskProducer<T> = isThenable(taskProducer)
? () => taskProducer as PromiseLike<T>
: (taskProducer as TaskProducer<T>);

public add(taskProducer: () => PromiseLike<T>): PromiseLike<T> {
if (!this.isReady()) {
return SyncPromise.reject(new SentryError('Not adding Promise due to buffer limit reached.'));
}
const task = normalizedTaskProducer();
const task = taskProducer();
if (this._buffer.indexOf(task) === -1) {
this._buffer.push(task);
}
Expand Down

0 comments on commit 13894ff

Please sign in to comment.