observable-emitter
v1.1.1
Published
An Event-Emitter like API using Observables
Downloads
4
Maintainers
Readme
Observable Emitter
Like events but better
Usage
const createEmitter = require('observable-emitter')
// OR
import creatEmitter from 'observable-emitter'
const events = createEmitter({
source: sourceObs,
pingTimeout: 1000
})
const onUpperCase = events.on(/[A-Z]/)
onUpperCase.subscribe(action => console.log('Uppercase action!'))
events.emit({ type: 'ABC' }) // Uppercase action!
events.emit({ type: 'D' }) // Uppercase action!
events.emit({ type: 'abc' }) //
API
const events = createEmitter(opts)
opts
:source
: An observable to treat as incoming source along with emitted eventspingTimeout
: The amount of time between pings
Returns an object of ObservableEmitter
shape:
observableEmitter
:on
:(...regExpOrString) => Observable
- A function that takes 0->n amount of regular expressions or strings to match actions on and returns an observable of those actions emitted into our system
- Example:
const onUpperSubscription = events.on(/[A-Z]/).subscribe(console.log)
once
:(...regExpOrString) => Observable
- A function that takes 0->n amount of regular expressions or strings to match actions on and returns an observable of the first action emitted that matches any of the strings or expressions
- Example:
const onUpperSubscription = events.once(/[A-Z]/).subscribe(console.log)
clear
:(errorValue) => void
- A function that either takes in a value to pass to subscriptions as an
error
value or if not emits acomplete
action. - Example:
events.clear()
- A function that either takes in a value to pass to subscriptions as an
emit
:({ type, ...rest }) => void
- A function that takes an action and emits it into the system
- Example:
events.emit({ type: 'SOME_ACTION' })