kosich / rxjs-proxify

Turns a Stream of Objects into an Object of Streams
MIT License
35 stars 3 forks source link

Extensibility #3

Open kosich opened 3 years ago

kosich commented 3 years ago

It'd be good to have ability to extend some levels of proxify, e.g.:

Rx Simple State example

// create a state
const state = createState({ count: 0, timer: 100 });

// listen to state changes
state.count.subscribe(c => console.log('C:', c)); // > C:0

// write to the state
state.count.next(1); // > C:1

// reset the state
state.next({ timer: 2, count: 2 }) // > C:2

state.count += 1; // > C:3
state.count.next(3); // ignored: same value

// read current values:
console.log(state.timer + state.count); // > 2 + 3 = 5

OR to be able to add rxjs-autorun functions, e.g.:

// pseudocode
// state.js
import { $, _ } from 'rxjs-autorun';

export const State = v => proxify(
  new BehaviorSubject(v), // base
  { $, _ } // extender
);

// index.js
import { run } from 'rxjs-autorun';
import { State } from './state';

const state = State(0);
run(() => state.$ + ' 🐑'); // > 0 🐑
state.next(1); // > 1 🐑
kosich commented 3 years ago

I've drafted this feature in #4, but want to hold it till we see more use-cases as it complicates types and code.