-
Notifications
You must be signed in to change notification settings - Fork 1
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
Event-bus ( WIP ) #250
base: main
Are you sure you want to change the base?
Event-bus ( WIP ) #250
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Event that is emitted when a note is added. | ||
*/ | ||
export const NOTE_ADDED_EVENT_NAME = 'noteAdded'; | ||
|
||
|
||
/** | ||
* Note added event | ||
*/ | ||
export class NoteAddedEvent extends CustomEvent<{ noteId: number; userId: number }> { | ||
/** | ||
* Note added event constructor | ||
* @param noteId - note internal id | ||
* @param userId - user id | ||
*/ | ||
constructor(noteId: number, userId: number) { | ||
super(NOTE_ADDED_EVENT_NAME, { | ||
detail: { noteId, | ||
userId }, | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,18 @@ | ||||||||||||||||||
// domain/event-bus/events/noteVisitedEvent.ts | ||||||||||||||||||
|
||||||||||||||||||
export const NOTE_VISITED_EVENT_NAME = 'noteVisited'; | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* Note visited event | ||||||||||||||||||
*/ | ||||||||||||||||||
export class NoteVisitedEvent extends CustomEvent<{ noteId: number; userId: number }> { | ||||||||||||||||||
/** | ||||||||||||||||||
* Note visited event constructor | ||||||||||||||||||
* @param noteId - note internal id | ||||||||||||||||||
* @param userId - user id | ||||||||||||||||||
*/ | ||||||||||||||||||
constructor(noteId: number, userId: number) { | ||||||||||||||||||
super(NOTE_VISITED_EVENT_NAME, { detail: { noteId, | ||||||||||||||||||
userId } }); | ||||||||||||||||||
Comment on lines
+15
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bad line break
Suggested change
|
||||||||||||||||||
} | ||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { NOTE_ADDED_EVENT_NAME, NoteAddedEvent } from './events/noteAddedEvent.js'; | ||
|
||
/** | ||
* Event Bus provides a loosely coupled communication way between Domain and some other layers | ||
* | ||
* Extends native event emitter called EventTarget | ||
*/ | ||
export default class EventBus extends EventTarget { | ||
private static instance: EventBus; | ||
|
||
/** | ||
* EventBus constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Gets the singleton instance of the EventBus | ||
*/ | ||
public static getInstance(): EventBus { | ||
if (EventBus.instance === undefined) { | ||
EventBus.instance = new EventBus(); | ||
} | ||
|
||
return EventBus.instance; | ||
} | ||
|
||
/** | ||
* Dispatches an event | ||
* | ||
* @param event - The event to dispatch | ||
*/ | ||
public dispatch(event: Event): boolean { | ||
return this.dispatchEvent(event); | ||
} | ||
} | ||
|
||
export type CrossDomainEventMap = { | ||
[NOTE_ADDED_EVENT_NAME]: NoteAddedEvent, | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ import type User from '@domain/entities/user.js'; | |
import { createInvitationHash } from '@infrastructure/utils/invitationHash.js'; | ||
import { DomainError } from '@domain/entities/DomainError.js'; | ||
import type { SharedDomainMethods } from './shared/index.js'; | ||
import EventBus from '@domain/event-bus/index.js'; | ||
import { NOTE_ADDED_EVENT_NAME } from '@domain/event-bus/events/noteAddedEvent.js'; | ||
|
||
/** | ||
* Service responsible for Note Settings | ||
|
@@ -31,6 +33,24 @@ export default class NoteSettingsService { | |
constructor(noteSettingsRepository: NoteSettingsRepository, teamRepository: TeamRepository, private readonly shared: SharedDomainMethods) { | ||
this.noteSettingsRepository = noteSettingsRepository; | ||
this.teamRepository = teamRepository; | ||
|
||
/** | ||
* Listen to the note related events | ||
*/ | ||
EventBus.getInstance().addEventListener(NOTE_ADDED_EVENT_NAME, async (event) => { | ||
const { noteId, userId } = (event as CustomEvent<{ noteId: number; userId: number }>).detail; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we get rid of this casting? Maybe we can declare EventMap. Example: https://github.com/codex-team/notes.web/blob/main/src/domain/event-bus/index.ts#L14-L29 |
||
|
||
try { | ||
await this.addNoteSettings(noteId); | ||
await this.createTeamMember({ | ||
noteId: noteId, | ||
userId: userId, | ||
role: MemberRole.Write, | ||
}); | ||
} catch (error) { | ||
throw error; | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.