Skip to content

Instantly share code, notes, and snippets.

@fudini
Created September 10, 2015 19:21
Show Gist options
  • Save fudini/42881395df67931e1497 to your computer and use it in GitHub Desktop.
Save fudini/42881395df67931e1497 to your computer and use it in GitHub Desktop.
Rx combine as object
const combineObject = config => {
const streams = _.values(config)
const keys = _.keys(config)
return Rx.Observable.combineLatest(
...streams,
(...values) => _.zip(values, keys).reduce((acc, [value, key]) => {
acc[key] = value
return acc
}, {})
)
}
const withLatestFromObject = (s$, property, config) => {
const streams = _.values(config)
const keys = _.keys(config)
return s$.withLatestFrom(
...streams,
(s, ...values) => _.zip(values, keys).reduce((acc, [value, key]) => {
acc[key] = value
return acc
}, {
[property]: s
})
)
}
// Usage
const a$ = Rx.Observable.just(1)
const b$ = Rx.Observable.just(2)
combineObject({
a: a$,
b: b$
})
.subscribe(c => console.log(c))
// {a: 1, b: 2}
withLatestFromObject(a$, 'a', {
b: b$
})
.subscribe(c => console.log(c))
// {a: 1, b: 2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment