-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(nexp-240): refactor event handler
* refactor(nexp): eventhandler as injectable dependency
- Loading branch information
1 parent
6b9f1ac
commit 66bc8e6
Showing
11 changed files
with
99 additions
and
8 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
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,71 @@ | ||
import { Subject } from 'rxjs'; | ||
import { IEvent } from './IEvent'; | ||
export interface IEventHandler | ||
{ | ||
execute(eventName: string, args: any): void; | ||
setEvent(_event: IEvent): void; | ||
removeListeners(): void; | ||
} | ||
|
||
type SubscribeEventProps = | ||
{ | ||
eventName: string; | ||
args: Record<string, unknown>; | ||
} | ||
|
||
export class EventHandler implements IEventHandler | ||
{ | ||
private eventSubject: Subject<any>; | ||
private events: Map<string, (args: any) => Promise<void>>; | ||
|
||
constructor() | ||
{ | ||
console.log(1); | ||
this.events = new Map<string, (args: any) => Promise<void>>(); | ||
this.eventSubject = new Subject<any>(); | ||
|
||
this.eventSubject.subscribe((event: SubscribeEventProps) => | ||
{ | ||
const { eventName, args } = event; | ||
const eventHandler = this.events.get(eventName); | ||
|
||
if (eventHandler) | ||
{ | ||
void (async() => | ||
{ | ||
try | ||
{ | ||
await eventHandler(args).then(); | ||
} | ||
catch (error) | ||
{ | ||
console.log(error); | ||
} | ||
})(); | ||
} | ||
}); | ||
} | ||
|
||
|
||
public execute(eventName: string, args: any) | ||
{ | ||
this.eventSubject.next({ eventName, args }); | ||
} | ||
|
||
public setEvent(_event: IEvent) | ||
{ | ||
if (this.events.has(_event.name)) | ||
{ | ||
return; | ||
} | ||
|
||
this.events.set(_event.name, _event.handle); | ||
} | ||
|
||
public async removeListeners() | ||
{ | ||
this.eventSubject.complete(); | ||
} | ||
} | ||
|
||
export default EventHandler; |
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,5 @@ | ||
export interface IEvent | ||
{ | ||
name: string; | ||
handle(props: any): Promise<void>; | ||
} |
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,2 @@ | ||
export * from './EventHandler'; | ||
export * from './IEvent'; |
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
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