Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(event): consolidate events through a bus
- Loading branch information
Showing
19 changed files
with
222 additions
and
151 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
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 |
---|---|---|
@@ -1,45 +1,11 @@ | ||
import { EventEmitter } from 'events'; | ||
|
||
import { Service } from '..'; | ||
import { Command } from '../../model/Command'; | ||
import { Actor } from '../../model/entity/Actor'; | ||
import { Room } from '../../model/entity/Room'; | ||
import { ErrorHandler, EventHandler } from '../../util/types'; | ||
import { StepResult } from '../state'; | ||
|
||
export interface InputEvent { | ||
lines: Array<string>; | ||
} | ||
|
||
export interface RoomEvent { | ||
room: Room; | ||
} | ||
|
||
export interface OutputEvent { | ||
lines: Array<string>; | ||
step: StepResult; | ||
} | ||
|
||
export interface CommandEvent { | ||
actor: Actor; | ||
command: Command; | ||
} | ||
|
||
export interface ActorService extends EventEmitter, Service { | ||
export interface ActorService extends Service { | ||
last(): Promise<Command>; | ||
|
||
/** | ||
* @todo remove, do in start | ||
*/ | ||
translate(verbs: ReadonlyArray<string>): Promise<void>; | ||
|
||
emit(name: 'input', event: InputEvent): boolean; | ||
emit(name: 'room', event: RoomEvent): boolean; | ||
emit(name: 'output', event: OutputEvent): boolean; | ||
|
||
on(name: 'command', handler: EventHandler<CommandEvent>): this; | ||
on(name: 'error', handler: ErrorHandler): this; | ||
on(name: 'output', handler: EventHandler<OutputEvent>): this; | ||
on(name: 'quit', handler: EventHandler<void>): this; | ||
on(name: 'room', handler: EventHandler<RoomEvent>): this; | ||
} |
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,32 @@ | ||
import { constructorName, mustExist } from '@apextoaster/js-utils'; | ||
import { EventEmitter } from 'events'; | ||
import { BaseOptions, Inject, Logger } from 'noicejs'; | ||
|
||
import { EventBus } from '.'; | ||
import { INJECT_LOGGER } from '../../module'; | ||
|
||
interface EventBusOptions extends BaseOptions { | ||
[INJECT_LOGGER]?: Logger; | ||
} | ||
|
||
@Inject(INJECT_LOGGER) | ||
export class NodeEventBus extends EventEmitter implements EventBus { | ||
protected logger: Logger; | ||
|
||
constructor(options: EventBusOptions) { | ||
super(); | ||
|
||
this.logger = mustExist(options[INJECT_LOGGER]).child({ | ||
kind: constructorName(this), | ||
}); | ||
} | ||
|
||
public emit(name: string, ...args: Array<unknown>): boolean { | ||
this.logger.debug({ | ||
eventArgs: args, | ||
eventName: name, | ||
}, 'bus proxying event'); | ||
|
||
return super.emit(name, ...args); | ||
} | ||
} |
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,68 @@ | ||
import { EventEmitter } from 'events'; | ||
|
||
import { Command } from '../../model/Command'; | ||
import { Actor } from '../../model/entity/Actor'; | ||
import { Room } from '../../model/entity/Room'; | ||
import { ErrorHandler, EventHandler } from '../../util/event'; | ||
import { StepResult } from '../state'; | ||
|
||
export interface InputEvent { | ||
lines: Array<string>; | ||
} | ||
|
||
export interface RoomEvent { | ||
room: Room; | ||
} | ||
|
||
export interface OutputEvent { | ||
lines: Array<string>; | ||
step: StepResult; | ||
} | ||
|
||
export interface CommandEvent { | ||
// actor: Actor; | ||
command: Command; | ||
} | ||
|
||
export interface EventBus extends EventEmitter { | ||
emit(name: 'error', err: Error): boolean; | ||
emit(name: 'quit'): boolean; | ||
emit(name: 'step', step: StepResult): boolean; | ||
|
||
/** | ||
* Parsed commands coming from actor service. | ||
*/ | ||
emit(name: 'actor-command', event: CommandEvent): boolean; | ||
|
||
/** | ||
* Translated output coming from actor service. | ||
*/ | ||
emit(name: 'actor-output', event: OutputEvent): boolean; | ||
|
||
/** | ||
* Unparsed input coming from render service. | ||
*/ | ||
emit(name: 'render-output', event: InputEvent): boolean; | ||
|
||
/** | ||
* Updated room events coming from state service. | ||
*/ | ||
emit(name: 'state-room', event: RoomEvent): boolean; | ||
|
||
/** | ||
* Untranslated output coming from state service. | ||
*/ | ||
emit(name: 'state-output', event: OutputEvent): boolean; | ||
|
||
on(name: 'actor-command', handler: EventHandler<CommandEvent>): this; | ||
on(name: 'actor-output', handler: EventHandler<OutputEvent>): this; | ||
on(name: 'render-output', handler: EventHandler<InputEvent>): this; | ||
on(name: 'state-room', event: EventHandler<RoomEvent>): this; | ||
on(name: 'state-output', event: EventHandler<OutputEvent>): this; | ||
|
||
// unqualified | ||
on(name: 'error', handler: ErrorHandler): this; | ||
on(name: 'quit', event: EventHandler<void>): this; | ||
on(name: 'step', event: EventHandler<StepResult>): this; | ||
} | ||
|
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
Oops, something went wrong.