Observevent
Event Observer inspired by svelte/store and rxjs
Installation
npm i observevent
Usage
subjectify
import { subjectify } from 'observevent'
const food = subjectify('')
const unsubscribe = food.subscribe((newValue, oldValue) => console.log(newValue, oldValue))
food.notify('apple')
food.notify((it) => it + ' pie')
unsubscribe()
observify
import { observify, select } from 'observevent'
import { EventEmitter } from 'events'
const emitter = new EventEmitter()
const logger = {
info: (...args: unknown[]) => console.log('[food]', ...args)
}
function isPie(food: string): boolean {
return food.indexOf('pie') > -1
}
const food = observify('', (trigger) => {
emitter.on('food', trigger)
return () => emitter.removeAllListener('food')
}, { logging: true, logger })
const unsubscribe = select(food, isPie).subscribe((it) => it)
emitter.emit('food', 'apple')
unsubscribe()
Options
observify
and subjectify
and combine
and combineMap
have the following options:
logging
Setting logging
to true
will output changelog.
Default value is false
.
import { subjectify } from 'observevent'
const food = subjectify('', { logging: true })
const unsubscribe = food.subscribe((it) => it)
food.notify('apple')
food.notify((it) => it + ' pie')
unsubscribe()
logger
You can set a custom logger.
import { subjectify } from 'observevent'
const logger = {
info: (value: unknown) => console.log('[food]', value)
}
const food = subjectify('', { logging: true, logger })
const unsubscribe = food.subscribe((it) => it)
food.notify('apple')
food.notify((it) => it + ' pie')
unsubscribe()
immediate
If immediate
is set to false
, the current value will not be notified and will be notified from changes after subscription.
Default value is true
.
import { subjectify } from 'observevent'
const food = subjectify('', { immediate: false })
const unsubscribe = food.subscribe((it) => console.log(it))
food.notify('apple')
food.notify((it) => it + ' pie')
unsubscribe()
Derives