1.0.11 • Published 3 years ago

tricklejs v1.0.11

Weekly downloads
12
License
MIT
Repository
-
Last release
3 years ago

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!") }
);
1.0.11

3 years ago

1.0.10

3 years ago

1.0.7

3 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago