Skip to content

Commit

Permalink
refactor: socket.once을 최대한 사용하도록 리팩토링, 삼항연산자 사용
Browse files Browse the repository at this point in the history
- socket.on후 socket.off로 이벤트의 리스너를 지우는 방식을 socket.once로 이벤트 발생시 바로 한번만 호출되고 리스너가 없어지도록 변경
- 삼항연산자를 사용해 더 짧고 가독성있게 변경
  • Loading branch information
choyoungwoo9 committed Jun 17, 2024
1 parent eafa485 commit 10d2140
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
3 changes: 1 addition & 2 deletions backend/src/project/service/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export class ProjectService {

async deleteLink(project: Project, linkId: number) {
const result = await this.projectRepository.deleteLink(project, linkId);
if (result) return true;
else return false;
return result ? true : false;
}
}
12 changes: 4 additions & 8 deletions backend/test/project/ws-link.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,14 @@ describe('WS link', () => {

const expectCreateLink = (socket, url, description) => {
return new Promise<void>((resolve, reject) => {
socket.on('landing', async (data) => {
socket.once('landing', async (data) => {
const { content, action, domain } = data;
expect(domain).toBe('link');
expect(action).toBe('create');
expect(content?.description).toBe(description);
expect(content?.id).toBeDefined();
expect(content?.url).toBe(url);
expect(content?.description).toBe(description);
socket.off('landing');
resolve(content.id);
});
});
Expand Down Expand Up @@ -170,11 +169,10 @@ describe('WS link', () => {
});
const getCreateLinkMsg = (socket) => {
return new Promise<void>((res) => {
socket.on('landing', (data) => {
socket.once('landing', (data) => {
const { action, domain } = data;
expect(domain).toBe('link');
expect(action).toBe('create');
socket.off('landing');
res();
});
});
Expand Down Expand Up @@ -241,28 +239,26 @@ describe('WS link', () => {
});
const expectCreateLinkAndReturnFirstLinkId = (socket, url, description) => {
return new Promise<void>((resolve, reject) => {
socket.on('landing', async (data) => {
socket.once('landing', async (data) => {
const { content, action, domain } = data;
expect(domain).toBe('link');
expect(action).toBe('create');
expect(content?.description).toBe(description);
expect(content?.id).toBeDefined();
expect(content?.url).toBe(url);
expect(content?.description).toBe(description);
socket.off('landing');
resolve(content.id);
});
});
};

const expectDeleteLink = (socket, linkId) => {
return new Promise<void>((resolve, reject) => {
socket.on('landing', async (data) => {
socket.once('landing', async (data) => {
const { content, action, domain } = data;
expect(domain).toBe('link');
expect(action).toBe('delete');
expect(content?.id).toBe(linkId);
socket.off('landing');
resolve();
});
});
Expand Down
3 changes: 1 addition & 2 deletions backend/test/project/ws-project-landing-page.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,10 @@ describe('WS landing', () => {

const getCreateMemoMsg = (socket) => {
return new Promise<void>((res) => {
socket.on('landing', (data) => {
socket.once('landing', (data) => {
const { action, domain } = data;
expect(domain).toBe('memo');
expect(action).toBe('create');
socket.off('landing');
res();
});
});
Expand Down

0 comments on commit 10d2140

Please sign in to comment.