Skip to content

Commit

Permalink
Merge fb7fa0e into 45c31b1
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed Mar 15, 2018
2 parents 45c31b1 + fb7fa0e commit dd5e4e0
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 2 deletions.
90 changes: 90 additions & 0 deletions src/Emitter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,93 @@ test('queue(): calling remove() on queued subscription will prevent it from invo
emitter.emit(count(1, undefined))
emitter.emit(count(2, undefined))
})

test('onAny() will listen to all events', t => {
const emitter = new Emitter()

let type = ''
emitter.onAny(fsa => {
type += fsa.type
})

emitter.emit({ type: 'x', payload: 1, meta: undefined })
emitter.emit({ type: 'y', payload: 1, meta: undefined })

t.is(type, 'xy')
})

test('onAny() returns subscription for removing itself', t => {
const emitter = new Emitter()

let type = ''
const sub = emitter.onAny(fsa => {
type += fsa.type
})

emitter.emit({ type: 'x', payload: 1, meta: undefined })
sub.remove()
emitter.emit({ type: 'y', payload: 1, meta: undefined })

t.is(type, 'x')
})

test('onAny() returns subscription second remove is noop', t => {
const emitter = new Emitter()

let type = ''
const sub = emitter.onAny(fsa => {
type += fsa.type
})

emitter.emit({ type: 'x', payload: 1, meta: undefined })
sub.remove()
sub.remove()
emitter.emit({ type: 'y', payload: 1, meta: undefined })

t.is(type, 'x')
})

test('onMiss() listens to all not listened events', t => {
const emitter = new Emitter()

let type = ''
emitter.onMiss(fsa => {
type += fsa.type
})
emitter.on('x', () => { return })
emitter.emit({ type: 'x', payload: 1, meta: undefined })
emitter.emit({ type: 'y', payload: 1, meta: undefined })

t.is(type, 'y')
})

test('onMiss() returns subscription for removing itself', t => {
const emitter = new Emitter()

let type = ''
const sub = emitter.onMiss(fsa => {
type += fsa.type
})
emitter.on('x', () => { return })
emitter.emit({ type: 'x', payload: 1, meta: undefined })
sub.remove()
emitter.emit({ type: 'y', payload: 1, meta: undefined })

t.is(type, '')
})

test('onMiss() returns subscription second remove is noop', t => {
const emitter = new Emitter()

let type = ''
const sub = emitter.onMiss(fsa => {
type += fsa.type
})
emitter.on('x', () => { return })
emitter.emit({ type: 'x', payload: 1, meta: undefined })
sub.remove()
sub.remove()
emitter.emit({ type: 'y', payload: 1, meta: undefined })

t.is(type, '')
})
36 changes: 34 additions & 2 deletions src/Emitter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FSA } from 'flux-standard-action'
import { FSA, FluxStandardAction } from 'flux-standard-action'
import { EventEmitter, EventSubscription } from 'fbemitter'

import { TypedEvent } from './createEvent'
Expand All @@ -7,11 +7,21 @@ import { errorEvent } from './errorEvent'
export class Emitter {
protected emitter: EventEmitter
protected eventQueues: { [k: string]: Function[] } = {}
protected listenAlls: Function[] = []
protected listenMisses: Function[] = []
constructor() {
this.emitter = new EventEmitter()
// const emit = this.emitter.emit
// this.emitter.emit = (eventType, ...data) => {
// emit.call(this.emitter, eventType, ...data)

// }
}
emit<Payload, Meta>({ type, payload, meta, error }: FSA<Payload, Meta>) {
return this.emitter.emit(type as string, payload, meta, error)
this.emitter.emit(type as string, payload, meta, error)
this.listenAlls.forEach(l => l({ type, payload, meta, error }))
if (this.listenMisses.length > 0 && this.emitter.listeners(type).length === 0)
this.listenMisses.forEach(l => l({ type, payload, meta, error }))
}

addListener<Payload, Meta>(
Expand Down Expand Up @@ -65,6 +75,28 @@ export class Emitter {
}
}

onAny(listener: (fsa: FluxStandardAction<any>) => void) {
this.listenAlls.push(listener)
return {
remove: () => {
const i = this.listenAlls.indexOf(listener)
if (i >= 0)
this.listenAlls.splice(i, 1)
}
}
}

onMiss(listener: (fsa: FluxStandardAction<any>) => void) {
this.listenMisses.push(listener)
return {
remove: () => {
const i = this.listenMisses.indexOf(listener)
if (i >= 0)
this.listenMisses.splice(i, 1)
}
}
}

protected addErrorEventListener<Payload, Meta>(listener: (payload: Payload, meta: Meta, error: boolean) => void): EventSubscription {
const wrappedListener = (payload, meta, error) => {
try {
Expand Down

0 comments on commit dd5e4e0

Please sign in to comment.