Skip to content

[URH-14] useTimer 신규 #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 31, 2024
Merged

[URH-14] useTimer 신규 #33

merged 12 commits into from
Jul 31, 2024

Conversation

jeongbaebang
Copy link
Member

👾 Pull Request

  • 상태: 신규

1️⃣ Spec

  • timer 기능을 지원하는 hook
  • 전달된 콜백에 대한 시작과 취소기능

2️⃣ 변경 사항

  • 변경 사항 없음

3️⃣ 예시 코드

const Component = () => {
  const [count, setCount] = useState(0);
  const { start, cancel } = useTimer(() => {
    setCount(1234);
  }, delay);

  return (
    <div>
      <p aria-label="count-display">{count}</p>
      <button onClick={start}>start</button>
      <button onClick={cancel}>cancel</button>
    </div>
  );
};

4️⃣ 관련 문서 (선택 사항)

  • 없음

callbackRef.current = callback;
}, [callback]);

useUnmountEffect(clearActiveTimer);
Copy link
Member

@Choozii Choozii Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥺 갬둥..

Copy link
Member

@Choozii Choozii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤩 천재만재

Comment on lines +24 to +31
(async () => {
cancelTokenRef.current = { isCancelled: false };
timerRef.current = delayExecution(ms);

await timerRef.current.start(cancelTokenRef.current);
callbackRef.current();
})();
}, [ms]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(async () => {
cancelTokenRef.current = { isCancelled: false };
timerRef.current = delayExecution(ms);
await timerRef.current.start(cancelTokenRef.current);
callbackRef.current();
})();
}, [ms]);
cancelTokenRef.current = { isCancelled: false };
timerRef.current = delayExecution(ms);
timerRef.current.start(cancelTokenRef.current).then(() =>{
callbackRef.current();
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P4] 아니믄

cosnt startTimer = async () => {
clearActiveTimer(); // 기존 타이머 취소
      cancelTokenRef.current = { isCancelled: false };
      timerRef.current = delayExecution(ms);

      await timerRef.current.start(cancelTokenRef.current);
      callbackRef.current();
}

  const startHandler = useCallback(() => {
    startTimer();
    },[ms]);

이렇게는 어떠세요?
IIFE가 있는게 가독성이 안좋은거 같아서요!

Copy link
Member Author

@jeongbaebang jeongbaebang Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

짧은 IIFE는 오히려 쉽게 로직이 읽을 수 있을 거 같기도 한데 다른 분들 의견도 궁금하네요.

@jeongbaebang jeongbaebang removed the request for review from seobew July 29, 2024 14:12
Copy link
Member

@bicochan bicochan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useTimer가 머지되면 useDelayFlag에도 적용해 봐야겠어요!🔥

@jeongbaebang jeongbaebang merged commit 77ca1d5 into master Jul 31, 2024
@jeongbaebang jeongbaebang deleted the URH-14/use-timer branch July 31, 2024 02:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants