realar-project / realar

5 kB Advanced state manager for React
MIT License
44 stars 0 forks source link

Is it okay and if there is a recommended way to use RxJS with realar? #102

Closed bangonkali closed 3 years ago

bangonkali commented 3 years ago

Hi, awesome work so far. I've been using it for a few hours now and I like the concept. Unfortunately I needed more complex ways of filtering, debouncing and managing multiple eventsources do you think it's okay and do you have a recommended way of using RxJS with this project?

betula commented 3 years ago

Thanks a lot for your time! Of course, you are absolutely right, there are not enough streaming operations at the moment. I plan to implement more advanced work with streams in the next releases so that it would be possible to migrate from RxJS.

In the examples below, you can see how to use Realar together with RxJS, and for the best possibilities, I want to implement everything you need inside Realar later.

play on runkit

import { Observable, Subject } from 'rxjs';
import { signal } from 'realar';

const signalFromSubject = (subj) => { // valueFromSubject will be similar
  const sig = signal();
  subj.subscribe({ next: sig });
  return sig;
}
const signalToSubject = (sig) => { // valueToSubject will be same
  const subj = new Subject();
  sig.to((v) => subj.next(v));
  return subj;
}
const signalToObservable = (sig) => { // valueToObservable will be same
  return new Observable(subscriber => {
    sig.to((v) => subscriber.next(v));
  });
}

// Examples

// make an instance of RxJS Subject
const subj = new Subject();

// make Realar signal from RxJS Subject
const sig = signalFromSubject(subj).to(v => console.log(`signal: ${v}`));

// make RxJS Subject from Realar signal
const sig_subj = signalToSubject(sig);
sig_subj.subscribe({ next: (v) => console.log(`signal subject: ${v}`) });

// make RxJS Observable from Realar signal
const sig_obs = signalToObservable(sig);
sig_obs.subscribe((v) => console.log(`signal observable: ${v}`));

// Try It

subj.next(10);
// console output:
// signal: 10
// signal subject: 10
// signal observable: 10

play on runkit

Thank you for helping me make the world a better place!