All Projects β†’ kosich β†’ rxjs-proxify

kosich / rxjs-proxify

Licence: MIT license
Turns a Stream of Objects into an Object of Streams

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to rxjs-proxify

observe-component
A library for accessing React component events as reactive observables
Stars: ✭ 36 (+5.88%)
Mutual labels:  rxjs, observable, reactive-programming
Od Virtualscroll
πŸš€ Observable-based virtual scroll implementation in Angular
Stars: ✭ 133 (+291.18%)
Mutual labels:  rxjs, observable, reactive-programming
observable-playground
Know your Observables before deploying to production
Stars: ✭ 96 (+182.35%)
Mutual labels:  rxjs, observable, reactive-programming
Redux Most
Most.js based middleware for Redux. Handle async actions with monadic streams & reactive programming.
Stars: ✭ 137 (+302.94%)
Mutual labels:  rxjs, observable, reactive-programming
ng-observe
Angular reactivity streamlined...
Stars: ✭ 65 (+91.18%)
Mutual labels:  rxjs, observable, reactive-programming
Recycle
Convert functional/reactive object description using RxJS into React component
Stars: ✭ 374 (+1000%)
Mutual labels:  rxjs, observable, reactive-programming
Evolui
A tiny reactive user interface library, built on top of RxJs.
Stars: ✭ 43 (+26.47%)
Mutual labels:  rxjs, observable, reactive-programming
Marble
Marble.js - functional reactive Node.js framework for building server-side applications, based on TypeScript and RxJS.
Stars: ✭ 1,947 (+5626.47%)
Mutual labels:  rxjs, observable
Awesome Rxjs
Awesome list of RxJS 5
Stars: ✭ 141 (+314.71%)
Mutual labels:  rxjs, observable
rxact
Rxact is an observable application management for Javascript app
Stars: ✭ 21 (-38.24%)
Mutual labels:  rxjs, observable
vuse-rx
Vue 3 + rxjs = ❀
Stars: ✭ 52 (+52.94%)
Mutual labels:  rxjs, reactive-programming
Rx Sandbox
Marble diagram DSL based test suite for RxJS 6
Stars: ✭ 151 (+344.12%)
Mutual labels:  rxjs, observable
observable-profiler
Tracks new & disposed Observable subscriptions
Stars: ✭ 41 (+20.59%)
Mutual labels:  rxjs, observable
Rxjs Hooks
React hooks for RxJS
Stars: ✭ 1,953 (+5644.12%)
Mutual labels:  rxjs, observable
Jupyterlab Data Explorer
First class datasets in JupyterLab
Stars: ✭ 146 (+329.41%)
Mutual labels:  rxjs, observable
Beicon
Reactive Streams for ClojureScript
Stars: ✭ 133 (+291.18%)
Mutual labels:  rxjs, reactive-programming
Awesome Reactive Programming
A repository for sharing all the resources available on Reactive Programming and Reactive Systems
Stars: ✭ 163 (+379.41%)
Mutual labels:  rxjs, reactive-programming
ObservableComputations
Cross-platform .NET library for computations whose arguments and results are objects that implement INotifyPropertyChanged and INotifyCollectionChanged (ObservableCollection) interfaces.
Stars: ✭ 94 (+176.47%)
Mutual labels:  observable, reactive-programming
reactive-angular-workshop
This is the source code for the world's greatest Reactive Angular Workshop.
Stars: ✭ 30 (-11.76%)
Mutual labels:  rxjs, reactive-programming
realar
5 kB Advanced state manager for React
Stars: ✭ 41 (+20.59%)
Mutual labels:  observable, reactive-programming


{ πŸ‘“ }
Turn a Stream of Objects into an Object of Streams

NPM Bundlephobia MIT license


Access values inside RxJS Observables as if they were directly available on the stream!

stream.pipe(pluck('msg')).subscribe(…);
// turn ↑ into ↓
stream.msg.subscribe(…);

With good TypeScript support! 😲 Roughly speaking:

proxify( Observable<{ msg: string }> ) β‰ˆ Observable<{ msg: string }> & { msg: Observable<string> }

But recursively. So stream.msg is a Proxy itself, allowing you to stream.msg.length.subscribe(…)!

Proxify lets you access Observable API as well as pluck props and call methods at any depth of an Observable, Subject, or BehaviorSubject! See the API and Examples sections to learn more.

πŸ“¦ Install

npm i rxjs-proxify

or try it online!

πŸ›  API

There are two methods available to you: proxify and statify

Proxify

proxify(stream) will wrap your Observable, Subject or BehaviorSubject in a Proxy:

Observable Proxy
subscribe at any depth

const observable = proxify( of({ p: 'πŸ‘' }) );
observable.subscribe(console.log); // > { p: πŸ‘ }
observable.p.subscribe(console.log); // > πŸ‘

Subject Proxy
subscribe at any depth, push at the root

const subject = proxify(new Subject<{ p: string }>());
subject.subscribe(console.log);
subject.p.subscribe(console.log);
subject.next({ p: 'πŸ₯' }); // > { p: πŸ₯ } // > πŸ₯

BehaviorSubject Proxy
subscribe at any depth, push at any depth, synchronously read the current state

const behavior = proxify(new BehaviorSubject({ p: 'πŸ–' }));
behavior.p.subscribe(console.log); // > πŸ–
behavior.p.next('πŸ‡'); // > πŸ‡
console.log(behavior.p.value) // > πŸ‡

Statify

statify(value) will put the value in a BehaviorSubject Proxy and add a distinctUntilChanged operator on each property access.

State Proxy
subscribe to distinct updates at any depth, push at any depth, synchronously read the current state

// create a state
const state = statify({ a: '🐰', z: '🏑' });

// listen to & log root state changes
state.subscribe(console.log); //> { a:🐰 z:🏑 }

// update particular substate
state.a.next('πŸ‡'); //> { a:πŸ‡ z:🏑 }

// read current values
console.log(state.z.value + state.a.value); //> πŸ‘πŸ‡

// update root state, still logging
state.next({ a: 'πŸ‡', z: '☁️' }) //> { a:πŸ‡ z:☁️ }

// and then…
state.z.next('πŸŒ™');   //> { a:πŸ‡  z:πŸŒ™ }
state.a.next('πŸ‡πŸ‘€'); //> { a:πŸ‡πŸ‘€ z:πŸŒ™ }
state.z.next('πŸ›Έ')    //> { a:πŸ‡πŸ‘€ z:πŸ›Έ }
state.a.next('πŸ’¨');   //> { a:πŸ’¨  z:πŸ›Έ }

See Examples section for more details.

πŸ“– Examples

Basic

import { proxify } from "rxjs-proxify";
import { of } from "rxjs";

const o = of({ msg: 'Hello' }, { msg: 'World' });
const p = proxify(o);
p.msg.subscribe(console.log);

// equivalent to
// o.pipe(pluck('msg')).subscribe(console.log);

With JS destructuring

Convenient stream props splitting

import { proxify } from "rxjs-proxify";
import { of } from "rxjs";

const o = of({ msg: 'Hello', status: 'ok'  }, { msg: 'World', status: 'ok' });
const { msg, status } = proxify(o);
msg.subscribe(console.log);
status.subscribe(console.log);

// equivalent to
// const msg = o.pipe(pluck('msg'));
// const status = o.pipe(pluck('status'));
// msg.subscribe(console.log);
// status.subscribe(console.log);

⚠️ WARNING: as shown in "equivalent" comment, this operation creates several Observables from the source Observable. Which means that if your source is cold β€” then you might get undesired subscriptions. This is a well-known nuance of working with Observables. To avoid this, you can use a multicasting operator on source before applying proxify, e.g. with shareReplay:

const { msg, status } = proxify(o.pipe(shareReplay(1)));

With pipe

Concatenate all messages using pipe with scan operator:

import { proxify } from "rxjs-proxify";
import { of } from "rxjs";
import { scan } from "rxjs/operators";

const o = of({ msg: 'Hello' }, { msg: 'World' });
const p = proxify(o);
p.msg.pipe(scan((a,c)=> a + c)).subscribe(console.log);

// equivalent to
// o.pipe(pluck('msg'), scan((a,c)=> a + c)).subscribe(console.log);

Calling methods

Pick a method and call it:

import { proxify } from "rxjs-proxify";
import { of } from "rxjs";

const o = of({ msg: () => 'Hello' }, { msg: () => 'World' });
const p = proxify(o);
p.msg().subscribe(console.log);

// equivalent to
// o.pipe(map(x => x?.map())).subscribe(console.log);

Accessing array values

Proxify is recursive, so you can keep chaining props or indices

import { proxify } from "rxjs-proxify";
import { of } from "rxjs";

const o = of({ msg: () => ['Hello'] }, { msg: () => ['World'] });
const p = proxify(o);
p.msg()[0].subscribe(console.log);

// equivalent to
// o.pipe(map(x => x?.map()), pluck(0)).subscribe(console.log);

🀝 Want to contribute to this project?

That will be awesome!

Please create an issue before submiting a PR β€” we'll be able to discuss it first!

Thanks!

Enjoy πŸ™‚

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].