From fa308f61fb849ae04c1eae49192387d6972b7703 Mon Sep 17 00:00:00 2001 From: Sangminnn Date: Thu, 2 May 2024 00:07:39 +0900 Subject: [PATCH] =?UTF-8?q?feat(utils):=20delay=20=ED=95=A8=EC=88=98=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20(#92)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(utils): delay 함수 추가 * fix: delay 함수 관련 문서 내에 존재하는 불필요한 함수 수정 완료 * Create long-lobsters-type.md * fix: 테스트코드 동작시간 단축을 위한 delay함수 검증시간 수정 완료 * refac: useBlockPromiseMultipleClick 테스트코드에서 사용중인 delay함수를 utils에 있는 함수로 교체 완료 --------- Co-authored-by: Gromit (전민재) <64779472+ssi02014@users.noreply.github.com> --- .changeset/long-lobsters-type.md | 5 ++++ docs/docs/utils/common/delay.md | 24 +++++++++++++++++++ .../useBlockPromiseMultipleClick.spec.tsx | 9 ++----- packages/utils/src/common/delay/delay.spec.ts | 15 ++++++++++++ packages/utils/src/common/delay/index.ts | 5 ++++ packages/utils/src/common/index.ts | 1 + 6 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 .changeset/long-lobsters-type.md create mode 100644 docs/docs/utils/common/delay.md create mode 100644 packages/utils/src/common/delay/delay.spec.ts create mode 100644 packages/utils/src/common/delay/index.ts diff --git a/.changeset/long-lobsters-type.md b/.changeset/long-lobsters-type.md new file mode 100644 index 000000000..566817390 --- /dev/null +++ b/.changeset/long-lobsters-type.md @@ -0,0 +1,5 @@ +--- +"@modern-kit/utils": patch +--- + +feat(utils): delay 함수 추가 diff --git a/docs/docs/utils/common/delay.md b/docs/docs/utils/common/delay.md new file mode 100644 index 000000000..4182b7b44 --- /dev/null +++ b/docs/docs/utils/common/delay.md @@ -0,0 +1,24 @@ +# delay + +주어진 시간만큼 기다린 뒤 다음 동작을 수행할 수 있도록 하는 함수입니다. + +setTimeout을 사용하여 특정 시간 뒤의 동작을 정의할 경우, 해당 시간 뒤 동작해야하는 함수 다음에 Promise가 존재한다면 setTimeout은 macroTaskQueue에 속하고 Promise는 microTaskQueue에 속하게 되어 의도한 바와 같이 순서대로의 동작을 보장하지 못할 수 있습니다. delay 함수를 사용한다면 이러한 문제를 해결할 수 있습니다. + +
+ +## Interface +```tsx +const delay: (time: number) => Promise +``` + +## Usage +```ts +import { delay } from '@modern-kit/utils'; + +const something = () => Promise.resolve() + +const doSomethingAfterDelay = async () => { + await delay(1000) + await something() +} +``` \ No newline at end of file diff --git a/packages/react/src/hooks/useBlockPromiseMultipleClick/useBlockPromiseMultipleClick.spec.tsx b/packages/react/src/hooks/useBlockPromiseMultipleClick/useBlockPromiseMultipleClick.spec.tsx index 46001e03c..ff59dea88 100644 --- a/packages/react/src/hooks/useBlockPromiseMultipleClick/useBlockPromiseMultipleClick.spec.tsx +++ b/packages/react/src/hooks/useBlockPromiseMultipleClick/useBlockPromiseMultipleClick.spec.tsx @@ -2,12 +2,7 @@ import { describe, expect, it } from 'vitest'; import { screen, renderHook } from '@testing-library/react'; import { renderSetup } from '../../utils/test/renderSetup'; import { useBlockPromiseMultipleClick } from '.'; - -const delay = (time: number) => { - return new Promise((resolve) => { - setTimeout(() => resolve(), time); - }); -}; +import { delay } from '@modern-kit/utils'; beforeEach(() => { vi.useFakeTimers(); @@ -29,7 +24,7 @@ describe('useBlockPromiseMultipleClick', () => { const { user } = renderSetup( , - { delay: null } + { delay: null }, ); const button = screen.getByRole('button'); diff --git a/packages/utils/src/common/delay/delay.spec.ts b/packages/utils/src/common/delay/delay.spec.ts new file mode 100644 index 000000000..bb40c67a8 --- /dev/null +++ b/packages/utils/src/common/delay/delay.spec.ts @@ -0,0 +1,15 @@ +import { describe, expect, it } from 'vitest'; +import { delay } from '.'; + +describe('delay', () => { + it('should delay the promise by the given time', async () => { + const time = 200; + const start = Date.now(); + + await delay(time); + + const end = Date.now(); + + expect(end - start).toBeGreaterThanOrEqual(time); + }); +}); diff --git a/packages/utils/src/common/delay/index.ts b/packages/utils/src/common/delay/index.ts new file mode 100644 index 000000000..ea96ebc72 --- /dev/null +++ b/packages/utils/src/common/delay/index.ts @@ -0,0 +1,5 @@ +export const delay = (time: number) => { + return new Promise((resolve) => { + setTimeout(() => resolve(), time); + }); +}; diff --git a/packages/utils/src/common/index.ts b/packages/utils/src/common/index.ts index b2252b8d9..5f46ce6bd 100644 --- a/packages/utils/src/common/index.ts +++ b/packages/utils/src/common/index.ts @@ -2,6 +2,7 @@ export * from './abRandom'; export * from './asyncNoop'; export * from './deepCopy'; export * from './deepEqual'; +export * from './delay'; export * from './getUniqId'; export * from './getUniqTime'; export * from './getViewportSize';