ractor-callbag
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i ractor-callbag

Weekly Downloads

2

Version

0.1.2

License

MIT

Unpacked Size

14.4 kB

Total Files

16

Last publish

Collaborators

  • xaomon