callbag-doki
is a new lib for reactive programing inspired by RxJS and based on callbag specs.
If you already use functions based on callbag
specs, you can import only some sources or operators and use them with other packages based on the same spec.
I was looking for a lightweight alternative of RxJS because it was 1 of my biggest dependency for a mobile web-app.
Then I discover callbag A standard for JS callbacks that enables lightweight observables and iterables
.
Even if there are already some sources and operators available, I was interessed by creating my own lib to have a better knowledge about how a reactive library works under the hood.
yarn add @epimodev/callbag-doki
# or with npm
npm install --save @epimodev/callbag-doki
Basic source with subscription:
import subscribe from '@epimodev/callbag-doki/utils/subscribe'
import interval from '@epimodev/callbag-doki/sources/interval'
const unsubscribe = subscribe(interval(1000))(count => console.log(count))
// 1
// 2
// 3
setTimeout(() => {
unsubscribe()
}, 3500)
Source transformed with operator:
import subscribe from '@epimodev/callbag-doki/utils/subscribe'
import pipeSource from '@epimodev/callbag-doki/utils/pipeSource'
import interval from '@epimodev/callbag-doki/sources/interval'
import map from '@epimodev/callbag-doki/operators/map'
const customInterval = pipeSource(
interval(1000),
map(count => count * 2),
)
const unsubscribe = subscribe(customInterval)(value => console.log(value))
// 2
// 4
// 6
setTimeout(() => {
unsubscribe()
}, 3500)
http request:
import subscribe from '@epimodev/callbag-doki/utils/subscribe'
import httpRequest from '@epimodev/callbag-doki/sources/httpRequest'
const httpSource = httpRequest({ url: 'https://jsonplaceholder.typicode.com/albums' })
const cancelRequest = subscribe(httpSource)({
next: response => console.log('Succeed: ', response),
error: err => console.log('Failed: ', err),
complete: () => console.log('Complete'),
})