Skip to content

Commit

Permalink
feat: batchOfRequestOf 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
brgndyy committed Sep 29, 2024
1 parent 9d3b79a commit f28193b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
62 changes: 62 additions & 0 deletions packages/common/utils/src/batchOfRequestOf.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, it, expect, vi } from 'vitest';
import { batchRequestsOf } from './batchOfRequestOf';

describe('batchRequestsOf에 대한 테스트 코드 작성', () => {
it('동일한 인자를 넣었을때 동일한 프로미스가 반환되어야 한다.', async () => {
const mockFunc = vi.fn((a: number, b: number) => {
return new Promise<number>((resolve) => {
setTimeout(() => resolve(a + b), 100);
});
});

const batchedFunc = batchRequestsOf(mockFunc);

const promise1 = batchedFunc(1, 2);
const promise2 = batchedFunc(1, 2);

expect(promise1).toBe(promise2);
expect(mockFunc).toHaveBeenCalledTimes(1);

const result = await promise1;
expect(result).toBe(3);
});

it('다른 인자를 넣었을때는 다른 프로미스가 반환 되어야 한다.', async () => {
const mockFunc = vi.fn((a: number, b: number) => {
return new Promise<number>((resolve) => {
setTimeout(() => resolve(a + b), 100);
});
});

const batchedFunc = batchRequestsOf(mockFunc);

const promise1 = batchedFunc(1, 2);
const promise2 = batchedFunc(3, 4);

expect(promise1).not.toBe(promise2);
expect(mockFunc).toHaveBeenCalledTimes(2);

const result1 = await promise1;
const result2 = await promise2;

expect(result1).toBe(3);
expect(result2).toBe(7);
});

it('프로미스가 resolve 된 후에는 새로운 프로미스가 반환 되어야 한다.', async () => {
const mockFunc = vi.fn((a: number, b: number) => {
return new Promise<number>((resolve) => {
setTimeout(() => resolve(a + b), 100);
});
});

const batchedFunc = batchRequestsOf(mockFunc);

const promise1 = batchedFunc(1, 2);
await promise1;

const promise2 = batchedFunc(1, 2);
expect(promise1).not.toBe(promise2);
expect(mockFunc).toHaveBeenCalledTimes(2);
});
});
19 changes: 19 additions & 0 deletions packages/common/utils/src/batchOfRequestOf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function batchRequestsOf<F extends (...args: any[]) => any>(func: F) {
const promiseByKey = new Map<string, Promise<ReturnType<F>>>();

return function (...args: Parameters<F>) {
const key = JSON.stringify(args);

if (promiseByKey.has(key)) {
return promiseByKey.get(key)!;
} else {
const promise = func(...args);
promise.then(() => {
promiseByKey.delete(key);
});
promiseByKey.set(key, promise);

return promise;
}
} as F;
}
1 change: 1 addition & 0 deletions packages/common/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './batchOfRequestOf';

0 comments on commit f28193b

Please sign in to comment.