Created
September 10, 2015 19:21
-
-
Save fudini/42881395df67931e1497 to your computer and use it in GitHub Desktop.
Rx combine as object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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