0.1.2 • Published 5 years ago

ractor-callbag v0.1.2

Weekly downloads
1
License
MIT
Repository
-
Last release
5 years ago

ractor-callbag

Callbag middleware for Ractor

Usage

import { CallbagStore } from "ractor-callbag";
import { ChangeTab } from "./ChangeTab";
import { map, fromPromise, pipe } from "callbag-basics";
import mapPromise from "callbag-map-promise";
import switchMap from "callbag-flat-map-operator";

export class TabStore extends CallbagStore {
  state = { content: "", actionNames: [] };
  preStart() {
    this.context.system.eventStream.on("*", obj => {
      const actionNames = [...this.state.actionNames];
      actionNames.push(obj.__proto__.constructor.name);
      this.setState({ actionNames });
    });
  }
  createReceive() {
    return this.receiveBuilder()
      .match(ChangeTab, changeTab$ =>
        pipe(
          changeTab$,
          switchMap(
            changeTab =>
              changeTab.tabid === 1
                ? fromPromise(api1000())
                : fromPromise(api3000())
          ),
          map(content => ({ content }))
        )
      )
      .build();
  }
}

function api1000() {
  return new Promise((resolve, reject) => {
    setTimeout(() => resolve("first"), 1000);
  });
}

function api3000() {
  return new Promise((resolve, reject) => {
    setTimeout(() => resolve("second"), 3000);
  });
}

Demo

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago