This story on HackerNoon has a decentralized backup on Sia.
Transaction ID: D9wjSrOKXXjO-r_T2sxbYYZZrbIGtVj8ey62BP28fMs
Cover

How To Access Properties Of Objects On Observables Like A True Expert

Written by @kddsky | Published on 2020/11/11

TL;DR โ€”
The RxJS/TS package allows you to access props of objects on Observables. It uses Proxies under the hood, recursively applying it to sub-properties and method results. The package has good TypeScript support, so all props are intelli-sensed by cats, dogs, and IDEs. The source code and more examples are available on the project's GitHub repo: GitHub.com/kosich/Rxjs-proxify-repl. It's also possible to call methods on values using RxJS operators at any depth.
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)
A simple use case: read the
msg
property of each value on the stream
import { proxify } from "rxjs-proxify";
import { of } from "rxjs";

const source = of({ msg: 'Hello' }, { msg: 'World' });
const stream = proxify(source);
stream.msg.subscribe(console.log); // 'Hello', 'World'
โ˜๏ธ 
proxify
 will create a Proxy for given Observable
You can even use JS destruction assignment:
const { msg } = proxify(source);
msg.subscribe(console.log); // 'Hello', 'World'
The package has good TypeScript support, so all props are intelli-sensed by cats, dogs, and IDEs:
import { of } from 'rxjs';
import { proxify } from 'rxjs-proxify';

const source = of({ a:1, b:1 }, { a:2, b:2 });
const stream = proxify(source);
stream. // <- will suggest .a .b .pipe .subscribe etc
๐Ÿ‘€ I can see your intentions
It's also possible to call methods on values (even those using
this
keyword), e.g.:
import { proxify } from "rxjs-proxify";
import { of } from "rxjs";

const source = of({ msg: () => 'Hello' }, { msg: () => 'World' });
const stream = proxify(source);
// calls msg() fn on each value of the stream
stream.msg().subscribe(console.log); // 'Hello', 'World'
๐Ÿคฏ pure magic, I tell you
And you are still free to apply RxJS operators at any depth:
import { proxify } from "rxjs-proxify";
import { of } from "rxjs";
import { scan } from "rxjs/operators";

const source = of({ msg: 'Hello' }, { msg: 'World' });
const stream = proxify(source);
stream.msg.pipe(scan((a, c)=> a + c)).subscribe(console.log); // 'HelloWorld'
Just like regular Observables!
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

๐Ÿ“– Repository

The source code and more examples are available on the project's GitHub repo: github.com/kosich/rxjs-proxify

Outro

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 on twitter for more RxJS, React, and JS posts
Thank you for reading this article! Stay reactive and have a nice day ๐Ÿ™‚

Psst.. need something more to read?

I got you covered:
Cya ๐Ÿ‘‹

[story continues]


Written by
@kddsky
Technical Writer on HackerNoon.

Topics and
tags
rxjs|angular|webdev|frontend|frp|reactive-programming|coding|software-development
This story on HackerNoon has a decentralized backup on Sia.
Transaction ID: D9wjSrOKXXjO-r_T2sxbYYZZrbIGtVj8ey62BP28fMs