All Projects → staltz → Toy Rx

staltz / Toy Rx

Licence: mit
A tiny implementation of RxJS that actually works, for learning

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Toy Rx

Inferno Most Fp Demo
A demo for the ReactJS Tampa Bay meetup showing how to build a React+Redux-like architecture from scratch using Inferno, Most.js, reactive programmning, and various functional programming tools & techniques
Stars: ✭ 45 (-84.48%)
Mutual labels:  reactive, rxjs, reactive-programming, observables
Awesome Rxjs
A collection of awesome RxJS resources
Stars: ✭ 314 (+8.28%)
Mutual labels:  reactive, rxjs, reactive-programming, observables
Rxviz
Rx Visualizer - Animated playground for Rx Observables
Stars: ✭ 1,471 (+407.24%)
Mutual labels:  reactive, rxjs, rx
Ayanami
🍭 A better way to react with state
Stars: ✭ 129 (-55.52%)
Mutual labels:  reactive, rxjs, reactive-programming
Awesome Reactive Programming
A repository for sharing all the resources available on Reactive Programming and Reactive Systems
Stars: ✭ 163 (-43.79%)
Mutual labels:  reactive, rxjs, reactive-programming
Platform
Reactive libraries for Angular
Stars: ✭ 7,020 (+2320.69%)
Mutual labels:  reactive, rxjs, observables
Rx Connect
Glue your state and pure React components with RxJS
Stars: ✭ 86 (-70.34%)
Mutual labels:  reactive, rxjs, rx
Redux Most
Most.js based middleware for Redux. Handle async actions with monadic streams & reactive programming.
Stars: ✭ 137 (-52.76%)
Mutual labels:  reactive, rxjs, reactive-programming
Learn Rxjs
Clear examples, explanations, and resources for RxJS
Stars: ✭ 3,475 (+1098.28%)
Mutual labels:  rxjs, reactive-programming, observables
servable
"simple" observable implementation based off RxJS & kefir Docs
Stars: ✭ 14 (-95.17%)
Mutual labels:  reactive, reactive-programming, observables
redrock
Typesafe, reactive redux
Stars: ✭ 14 (-95.17%)
Mutual labels:  reactive, rxjs, rx
callbag-subscribe
A callbag sink (listener) that connects an Observer a-la RxJS. 👜
Stars: ✭ 17 (-94.14%)
Mutual labels:  reactive, rx, observables
Rxios
A RxJS wrapper for axios
Stars: ✭ 119 (-58.97%)
Mutual labels:  rxjs, rx, observables
Reactive.how
Learn reactive programming with animated cards.
Stars: ✭ 592 (+104.14%)
Mutual labels:  rxjs, reactive-programming, rx
fpEs
Functional Programming for EcmaScript(Javascript)
Stars: ✭ 40 (-86.21%)
Mutual labels:  reactive, reactive-programming, rx
Beicon
Reactive Streams for ClojureScript
Stars: ✭ 133 (-54.14%)
Mutual labels:  reactive, rxjs, reactive-programming
vuse-rx
Vue 3 + rxjs = ❤
Stars: ✭ 52 (-82.07%)
Mutual labels:  rxjs, reactive-programming, rx
Rx.Http
A reactive way to make HTTP Request in .NET Core 🚀
Stars: ✭ 62 (-78.62%)
Mutual labels:  reactive, reactive-programming, rx
axios-for-observable
A RxJS wrapper for axios, same api as axios absolutely
Stars: ✭ 13 (-95.52%)
Mutual labels:  rxjs, rx, observables
angular2-instagram
🔥Instagram like photo filter playground built with Angular2 (Web | Desktop)
Stars: ✭ 91 (-68.62%)
Mutual labels:  rxjs, reactive-programming

Toy RxJS

A tiny implementation of RxJS that actually works, for learning.

Usage

npm install toy-rx

const Rx = require('toy-rx')
Rx.Observable.fromEvent = require('toy-rx/fromEvent')
Rx.Observable.prototype.map = require('toy-rx/map')
Rx.Observable.prototype.filter = require('toy-rx/filter')
Rx.Observable.prototype.delay = require('toy-rx/delay')

Rx.Observable.fromEvent(document, 'click')
  .delay(500)
  .map(ev => ev.clientX)
  .filter(x => x < 200)
  .subscribe({
    next: x => console.log(x),
    error: e => console.error(e),
    complete: () => {},
  })

Why

I made this so people can look into the implementation of a simple RxJS and feel like they can actually understand it. I mean, the implementation is literally this below:

class Subscription {
  constructor(unsubscribe) {
    this.unsubscribe = unsubscribe;
  }
}

class Subscriber extends Subscription {
  constructor(observer) {
    super(function unsubscribe() {});
    this.observer = observer;
  }

  next(x) {
    this.observer.next(x);
  }

  error(e) {
    this.observer.error(e);
    this.unsubscribe();
  }

  complete() {
    this.observer.complete();
    this.unsubscribe();
  }
}

class Observable {
  constructor(subscribe) {
    this.subscribe = subscribe;
  }

  static create(subscribe) {
    return new Observable(function internalSubscribe(observer) {
      const subscriber = new Subscriber(observer);
      const subscription = subscribe(subscriber);
      subscriber.unsubscribe = subscription.unsubscribe.bind(subscription);
      return subscription;
    });
  }
}

class Subject extends Observable {
  constructor() {
    super(function subscribe(observer) {
      this.observers.push(observer);
      return new Subscription(() => {
        const index = this.observers.indexOf(observer);
        if (index >= 0) this.observers.splice(index, 1);
      });
    });
    this.observers = [];
  }

  next(x) {
    this.observers.forEach((observer) => observer.next(x));
  }

  error(e) {
    this.observers.forEach((observer) => observer.error(e));
  }

  complete() {
    this.observers.forEach((observer) => observer.complete());
  }
}

See? It fit easily in a README and you weren't scared of looking at it even though it's source code. That was index.js.

Where are all the operators? Well, here's map for instance:

function map(transformFn) {
  const inObservable = this;
  const outObservable = Rx.Observable.create(function subscribe(outObserver) {
    const inObserver = {
      next: (x) => {
        try {
          var y = transformFn(x);
        } catch (e) {
          outObserver.error(e);
          return;
        }
        outObserver.next(y);
      },
      error: (e) => {
        outObserver.error(e);
      },
      complete: () => {
        outObserver.complete();
      }
    };
    return inObservable.subscribe(inObserver);
  });
  return outObservable;
}

What about filter, and combineLatest and all the rest? Just look for yourself, don't be afraid to click on the JS files in this project.

How is this different to the official RxJS?

This is just meant for education. It's missing a ton of stuff that the official RxJS covers, such as:

  • Subscribe supports partial Observer objects
  • Subscribe supports functions as arguments
  • Dozens of useful operators
  • The wonderful and underrated Lift Architecture
  • Schedulers (and in turn, testing with marble diagrams)
  • Corner cases covered against bugs
  • Thorough documentation and examples
  • Thorough tests
  • TypeScript support

Overall, this project is a simplification of RxJS which describes "close enough" how RxJS works, but has a lot of holes and bugs because we deliberately are not taking care of many corner cases in this code.

Use this project to gain confidence peeking into the implementation of a library and get familiar with RxJS internals.

Contributing

Don't bother submitting PRs for this project to add more operators and more bug safety. Let's just keep this simple enough to read for any developer with a few minutes of free time.

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].