Skip to content

Commit

Permalink
feat: passing in logger
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed Apr 22, 2022
1 parent e48201a commit 2872716
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
4 changes: 2 additions & 2 deletions ts/Emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Emitter {
protected eventQueues: { [k: string]: AnyFunction[] } = {}
protected listenAlls: AnyFunction[] = []
protected listenMisses: AnyFunction[] = []
constructor() {
constructor(protected log: { error(...args: any[]): void } = console) {
this.emitter = new EventEmitter()
}
emit<Payload, Meta>({ type, payload, meta, error }: FSA<string, Payload, Meta>) {
Expand Down Expand Up @@ -99,7 +99,7 @@ export class Emitter {
listener(payload, meta, error)
}
catch (err) {
console.error('Error thrown in error event handler:', err)
this.log.error('Error thrown in error event handler:', err)
}
}
return this.emitter.addListener(errorEvent.type, wrappedListener)
Expand Down
22 changes: 13 additions & 9 deletions ts/TestEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import { errorEvent } from './errorEvent'
*/
export class TestEmitter extends Emitter {
private calledListeners: { [k: string]: boolean } = {}
constructor() {
super()
private defaultMiss: ({ type, payload, meta }: FluxStandardAction<any, any, any>) => void
constructor(protected log: { error(...args: any[]): void } = console) {
super(log)

this.listenMisses.push(defaultMiss)
this.defaultMiss = buildDefaultMiss(log)
this.listenMisses.push(this.defaultMiss)
}
addListener<Payload, Meta>(
event: TypedEvent<Payload, Meta> | string,
Expand Down Expand Up @@ -45,7 +47,7 @@ export class TestEmitter extends Emitter {
return this.listenedToEventMap(events)
}
onMissed(listener: (fsa: FluxStandardAction<any, any, any>) => void) {
if (this.listenMisses.length === 1 && this.listenMisses[0] === defaultMiss) {
if (this.listenMisses.length === 1 && this.listenMisses[0] === this.defaultMiss) {
this.listenMisses.splice(0, 1)
}
return super.onMissed(listener)
Expand All @@ -71,9 +73,11 @@ export class TestEmitter extends Emitter {
}
}

function defaultMiss({ type, payload, meta }: FluxStandardAction<any, any, any>) {
console.error(`missed event:
type: ${type}
payload: ${tersify(payload, { maxLength: Infinity })}
meta: ${tersify(meta)}`)
function buildDefaultMiss(log: { error: (...args: any[]) => void }) {
return function defaultMiss({ type, payload, meta }: FluxStandardAction<any, any, any>) {
log.error(`missed event:
type: ${type}
payload: ${tersify(payload, { maxLength: Infinity })}
meta: ${tersify(meta)}`)
}
}

0 comments on commit 2872716

Please # to comment.