Skip to content

Commit b43b496

Browse files
authored
feat(utils): kill unused promise buffer api (#4330)
1 parent ae02a00 commit b43b496

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

packages/utils/src/promisebuffer.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ function allPromises<U = unknown>(collection: Array<U | PromiseLike<U>>): Promis
2323
}
2424

2525
export interface PromiseBuffer<T> {
26-
length(): number;
26+
// exposes the internal array so tests can assert on the state of it.
27+
// XXX: this really should not be public api.
28+
$: Array<PromiseLike<T>>;
2729
add(taskProducer: () => PromiseLike<T>): PromiseLike<T>;
28-
remove(task: PromiseLike<T>): PromiseLike<T>;
2930
drain(timeout?: number): PromiseLike<boolean>;
3031
}
3132

@@ -36,10 +37,6 @@ export interface PromiseBuffer<T> {
3637
export function makePromiseBuffer<T>(limit?: number): PromiseBuffer<T> {
3738
const buffer: Array<PromiseLike<T>> = [];
3839

39-
function length(): number {
40-
return buffer.length;
41-
}
42-
4340
function isReady(): boolean {
4441
return limit === undefined || buffer.length < limit;
4542
}
@@ -113,12 +110,9 @@ export function makePromiseBuffer<T>(limit?: number): PromiseBuffer<T> {
113110
});
114111
}
115112

116-
const promiseBuffer: PromiseBuffer<T> = {
117-
length,
113+
return {
114+
$: buffer,
118115
add,
119-
remove,
120116
drain,
121117
};
122-
123-
return promiseBuffer;
124118
}

packages/utils/test/promisebuffer.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('PromiseBuffer', () => {
77
const buffer = makePromiseBuffer();
88
const p = jest.fn(() => new SyncPromise(resolve => setTimeout(resolve)));
99
void buffer.add(p);
10-
expect(buffer.length()).toEqual(1);
10+
expect(buffer.$.length).toEqual(1);
1111
});
1212

1313
test('with limit', () => {
@@ -20,7 +20,7 @@ describe('PromiseBuffer', () => {
2020
const producer2 = jest.fn(() => new SyncPromise(resolve => setTimeout(resolve)));
2121
expect(buffer.add(producer1)).toEqual(task1);
2222
void expect(buffer.add(producer2)).rejects.toThrowError();
23-
expect(buffer.length()).toEqual(1);
23+
expect(buffer.$.length).toEqual(1);
2424
expect(producer1).toHaveBeenCalled();
2525
expect(producer2).not.toHaveBeenCalled();
2626
});
@@ -32,51 +32,51 @@ describe('PromiseBuffer', () => {
3232
for (let i = 0; i < 5; i++) {
3333
void buffer.add(() => new SyncPromise(resolve => setTimeout(resolve)));
3434
}
35-
expect(buffer.length()).toEqual(5);
35+
expect(buffer.$.length).toEqual(5);
3636
const result = await buffer.drain();
3737
expect(result).toEqual(true);
38-
expect(buffer.length()).toEqual(0);
38+
expect(buffer.$.length).toEqual(0);
3939
});
4040

4141
test('with timeout', async () => {
4242
const buffer = makePromiseBuffer();
4343
for (let i = 0; i < 5; i++) {
4444
void buffer.add(() => new SyncPromise(resolve => setTimeout(resolve, 100)));
4545
}
46-
expect(buffer.length()).toEqual(5);
46+
expect(buffer.$.length).toEqual(5);
4747
const result = await buffer.drain(50);
4848
expect(result).toEqual(false);
4949
});
5050

5151
test('on empty buffer', async () => {
5252
const buffer = makePromiseBuffer();
53-
expect(buffer.length()).toEqual(0);
53+
expect(buffer.$.length).toEqual(0);
5454
const result = await buffer.drain();
5555
expect(result).toEqual(true);
56-
expect(buffer.length()).toEqual(0);
56+
expect(buffer.$.length).toEqual(0);
5757
});
5858
});
5959

6060
test('resolved promises should not show up in buffer length', async () => {
6161
const buffer = makePromiseBuffer();
6262
const producer = () => new SyncPromise(resolve => setTimeout(resolve));
6363
const task = buffer.add(producer);
64-
expect(buffer.length()).toEqual(1);
64+
expect(buffer.$.length).toEqual(1);
6565
await task;
66-
expect(buffer.length()).toEqual(0);
66+
expect(buffer.$.length).toEqual(0);
6767
});
6868

6969
test('rejected promises should not show up in buffer length', async () => {
7070
const buffer = makePromiseBuffer();
7171
const producer = () => new SyncPromise((_, reject) => setTimeout(reject));
7272
const task = buffer.add(producer);
73-
expect(buffer.length()).toEqual(1);
73+
expect(buffer.$.length).toEqual(1);
7474
try {
7575
await task;
7676
} catch (_) {
7777
// no-empty
7878
}
79-
expect(buffer.length()).toEqual(0);
79+
expect(buffer.$.length).toEqual(0);
8080
});
8181

8282
test('resolved task should give an access to the return value', async () => {

0 commit comments

Comments
 (0)