-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#103): custom event and event manager
- Loading branch information
Showing
6 changed files
with
69 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { singleton } from 'tsyringe' | ||
|
||
import { Logger } from '@services' | ||
|
||
@singleton() | ||
export class EventManager { | ||
|
||
private _events: Map<string, Function[]> = new Map() | ||
|
||
constructor( | ||
private logger: Logger | ||
) { | ||
} | ||
|
||
register(event: string, callback: Function): void { | ||
|
||
this._events.set(event, [...this._events.get(event) || [], callback]) | ||
} | ||
|
||
async emit(event: string, ...args: any[]): Promise<void> { | ||
|
||
const callbacks = this._events.get(event) | ||
|
||
if (!callbacks) return | ||
|
||
for (const callback of callbacks) { | ||
|
||
try { | ||
await callback(...args) | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
this.logger.log(error.toString(), 'error', true) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { resolveDependency } from '@utils/functions' | ||
|
||
export const OnCustom = (event: string) => { | ||
|
||
return function ( | ||
target: any, | ||
propertyKey: string, | ||
descriptor: PropertyDescriptor | ||
) { | ||
|
||
import('@services').then(async ({ EventManager }) => { | ||
|
||
const eventManager = await resolveDependency(EventManager) | ||
|
||
eventManager.register(event, descriptor.value) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters