Skip to content

JS implementation of Dart Stream and StreamController

License

Notifications You must be signed in to change notification settings

davinche/tricklejs

Repository files navigation

TrickleJS

build Coverage Status

TrickleJS is a partial implementation of Dart's StreamController and Stream. The purpose is to provide a library with a similar interface to Dart streams such that one could start build applications using things like the BloC architecture in JS without diving into the hairy world of Reactive Extensions.

Installation

npm install --save tricklejs

Usage

Example: Countdown Timer

import { StreamController } from "tricklejs";

const getTickerStream = function (numberOfTicks) {
  const sc = new StreamController();
  let counter = 0;
  let timer = null;
  const start = () => {
    if (timer) return;
    timer = setInterval(() => {
      if (counter < numberOfTicks) {
        sc.add(counter);
        counter++;
      } else {
        stop();
        sc.close();
      }
    }, 1000);
  };

  const stop = () => {
    if (!timer) return;
    clearInterval(timer);
    timer = null;
  };

  sc.onListen = start;
  sc.onResume = start;
  sc.onPause = stop;
  sc.onCancel = stop;
  return sc.stream;
};

const countDown = getTickerStream(5);
console.log("Countdown...");
countDown.listen(
  (n) => {
    console.log(5 - n);
  },
  { onDone: () => console.log("Blastoff!") }
);

About

JS implementation of Dart Stream and StreamController

Resources

License

Stars

Watchers

Forks

Packages

No packages published