Skip to content
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

[Refector] event 관리 코드 수정 #350

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open

Conversation

username1103
Copy link
Member

@username1103 username1103 commented Jan 24, 2023

체크 리스트

  • 적절한 제목으로 수정했나요?
  • 관련된 이슈와 연결 시켰나요?
  • Target Branch를 올바르게 설정했나요?
  • Label을 알맞게 설정했나요?

작업 내역

  • NestJS에서 제공하는@nestjs/event-emitter 는 내부적으로 event emitter2를 사용하고 있다. 기존에도 정상적으로 동작하긴 했으나 사용법이 적절하지 못해 수정했다.

기존에는 아래와 같이 비동기함수인 event listenr를 단순히 emit해 우회적으로 동작시켰다.

this.eventEmitter.emit(
'group.succeed',
new GroupSucceedEvent(groupArticle),
);

@OnEvent('group.succeed')
async handleGroupSucceedEvent(event: GroupSucceedEvent) {
const { groupArticle } = event;

async옵션을 통해 setImmediate를 사용해 비동기적으로 호출될 수 있도록 아래와 같이 변경했다.

await this.eventEmitter.emitAsync(
GroupSucceedEvent.event,
new GroupSucceedEvent(groupArticle),
);

@OnEvent(GroupSucceedEvent.event, { async: true })
async handleGroupSucceedEvent(event: GroupSucceedEvent) {
const { groupArticle } = event;

문제 상황과 해결

  • 현재 async는 true로 두어 비동기적으로 setImmediate을 사용하도록 되어있다. { nextTick: true } 옵션을 주어 next tick에 바로 실행되도록 할수도 있다. setImmediate는 이벤트 루프에 check phase에 실행되고 nextTick은 nextTickQueue에 담겨 다음 페이즈로 넘어가기전에 실행된다.

두 옵션 모두 비동기적으로 동작하게 할 수 있는데 어떤 옵션을 주는게 좋을까?

비고

  • 참고했던 링크 등 참고 사항을 적어주세요. 코드 리뷰하는 사람이 참고해야 하는 내용을 자유로운 형식으로 적을 수 있습니다.

Copy link
Collaborator

@kong430 kong430 left a comment

Choose a reason for hiding this comment

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

👏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants