Skip to content

Commit

Permalink
Merge c7df5e7 into 9980ef2
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed May 13, 2018
2 parents 9980ef2 + c7df5e7 commit cf0cadd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/TestEmitter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,9 @@ test('automatically listen to missed', t => {

t.is(emitter['listenMisses'].length, 1)
})

test('explicit onMissed() will remove default listener', t => {
const emitter = new TestEmitter()
emitter.onMissed(() => { return })
t.is(emitter['listenMisses'].length, 1)
})
21 changes: 15 additions & 6 deletions src/TestEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TypedEvent } from './createEvent'
import { Emitter } from './Emitter'
import { errorEvent } from './errorEvent'
import { EventSubscription } from 'fbemitter'
import { FluxStandardAction } from 'flux-standard-action';

/**
* Emitter used for testing.
Expand All @@ -15,12 +16,7 @@ export class TestEmitter extends Emitter {
constructor() {
super()

this.onMissed(({ type, payload, meta }) => {
console.error(`missed event:
type: ${type}
payload: ${tersify(payload, { maxLength: Infinity })}
meta: ${tersify(meta)}`)
})
this.listenMisses.push(defaultMiss)
}
addListener<Payload, Meta>(
event: TypedEvent<Payload, Meta> | string,
Expand Down Expand Up @@ -49,6 +45,12 @@ export class TestEmitter extends Emitter {
else
return this.listenedToEventMap(events)
}
onMissed(listener: (fsa: FluxStandardAction<any, undefined>) => void) {
if (this.listenMisses.length === 1 && this.listenMisses[0] === defaultMiss) {
this.listenMisses.splice(0, 1)
}
return super.onMissed(listener)
}

private listenedToEventArray(events: (TypedEvent<any, any> | string)[]) {
const keys = Object.keys(this.calledListeners)
Expand All @@ -69,3 +71,10 @@ export class TestEmitter extends Emitter {
return this.emitter.addListener(errorEvent.type, listener)
}
}

function defaultMiss({ type, payload, meta }) {
console.error(`missed event:
type: ${type}
payload: ${tersify(payload, { maxLength: Infinity })}
meta: ${tersify(meta)}`)
}

0 comments on commit cf0cadd

Please sign in to comment.