Skip to content

Commit

Permalink
fix(debounceAsyncResult): do the same for rejection as for resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
bowencool committed Apr 4, 2022
1 parent 1dcc43f commit 1f05064
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 6 deletions.
52 changes: 52 additions & 0 deletions packages/debounceAsyncResult/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { debounceAsyncResult } from './index';

// jest.useFakeTimers();
function flushPromises() {
return new Promise((resolve) => setImmediate(resolve));
}

function someAsyncTask<T>(data: T, delay = 100, fail?: boolean): Promise<T> {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (fail) {
reject(data);
} else {
resolve(data);
}
}, delay);
});
}

describe('debounceAsync', () => {
test('keep result correctly', async () => {
jest.useFakeTimers();
const debounced = debounceAsyncResult(someAsyncTask);
const N = Math.random();
const p = debounced(N, 1);
jest.advanceTimersByTime(1000);
await expect(p).resolves.toBe(N);
const p2 = debounced(N, 0, true);
jest.advanceTimersByTime(1000);
await expect(p2).rejects.toBe(N);
});
test('keep result correctly with multiple calls', async () => {
jest.useFakeTimers();
const rawFn = jest.fn(someAsyncTask);
const debounced = debounceAsyncResult(rawFn);
const resolveOrReject = jest.fn();
new Array(5).fill(0).map((_, i) =>
debounced(i * 2, 100 - 10 * i, i % 2 === 0)
.then(resolveOrReject)
.catch(resolveOrReject),
);

expect(rawFn).toHaveBeenCalledTimes(5);
expect(resolveOrReject).not.toHaveBeenCalled();
jest.advanceTimersByTime(500);
await flushPromises();
expect(rawFn).toHaveBeenCalledTimes(5);
expect(rawFn).lastCalledWith(8, 60, true);
expect(resolveOrReject).toHaveBeenCalledTimes(1);
expect(resolveOrReject).lastCalledWith(8);
});
});
33 changes: 27 additions & 6 deletions packages/debounceAsyncResult/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,33 @@ export function debounceAsyncResult<T, P extends any[], R>(fn: (this: T, ...p: P
let lastFetchId = 0;
return function asyncDebounced(this: T, ...args: P): Promise<R> {
const fetchId = ++lastFetchId;

return fn.call(this, ...args).then((...a1) => {
if (fetchId !== lastFetchId) {
return new Promise(() => {});
}
return Promise.resolve(...a1);
return new Promise<R>((resolve, reject) => {
fn.call(this, ...args)
.then((...rez) => {
if (fetchId === lastFetchId) {
resolve(...rez);
}
})
.catch((...err) => {
if (fetchId === lastFetchId) {
reject(...err);
}
});
});

// return fn
// .call(this, ...args)
// .then((...a1) => {
// if (fetchId !== lastFetchId) {
// return new Promise(() => {});
// }
// return Promise.resolve(...a1);
// })
// .catch((err) => {
// if (fetchId !== lastFetchId) {
// return new Promise(() => {}) as Promise<R>;
// }
// return Promise.reject(err);
// });
};
}

0 comments on commit 1f05064

Please sign in to comment.