Turn a Stream of Objects into an Object of Streams
Kostia Palchyk
Posted on September 18, 2020
Hi, fellow RxJS streamer! π
Today I want to share a JS/TS package that allows you to access props of objects on Observables:
source$.subscribe(o => console.log(o?.a?.b?.c))
// turn β into β
source$.a.b.c.subscribe(console.log)
tl;dr: github.com/kosich/rxjs-proxify
A simple use case: read the msg property of each value on the stream
The package has good TypeScript support, so all props are intelli-sensed by cats, dogs, and IDEs:
t's also possible to call methods on values (even those using this
keyword), e.g.:
And you are still free to apply RxJS operators at any depth:
The package uses Proxies under the hood, recursively applying it to sub-properties and method results, so the chain can be indefinitely deep. And you can apply .subscribe
or .pipe
at any time!
πΉ Try it
You can install it via npm i rxjs-proxify
Or test it online: stackblitz.com/edit/rxjs-proxify-repl
π Repository
The source code and more examples are available on github repo:
github.com/kosich/rxjs-proxify
Outro
Thank you for reading this article! Stay reactive and have a nice day π
If you enjoyed reading β please, indicate that with β€οΈ π¦ π buttons β it helps a lot!
Soon I'll post a more detailed review of the lib and how it works
Follow me here and on twitter for more RxJS, React, and JS posts:
π£ Would love to hear your thoughts!
Psst.. need something more to read?
I got you covered:
π
Cya π
Posted on September 18, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.