@36node/redux-cron

An redux libary for do cron job

Usage no npm install needed!

<script type="module">
  import 36nodeReduxCron from 'https://cdn.skypack.dev/@36node/redux-cron';
</script>

README

Redux Cron

An redux library for doing cron job

version downloads

Use

Config redux

import { cronReducer } from "@36node/redux-cron";

export default combineReducers({
  crons: cronReducer,
});

Actions

import { createCronActions } from "@36node/redux-cron";
import { put } from "redux-saga/effects";

const cronActions = createCronActions("SOME_KEY", {
  // onTick can be a fucntion or an action object
  onTick: function*(count, actions) {
    // count: current tick count
    // actions: cron actions

    if (count >= 10) {
      yield put(actions.stop());
    }
  },
});

// dispatch, interval 1s
dispatch(cronActions.start(1000));
export interface CronActions {
  start: (interval?: number, meta?: Object) => StartAction; // start cron
  stop: (meta?: Object) => StopAction; // stop cron
  cancel: (meta?: Object) => CancelAction; // reset cron, set count=0
}

Redux state

export interface CronState {
  count: number; // tick count
  lastAt: string; // last tick at
  lastStartAt: string; // last start at
  lastStopAt: string; // last stop at
  interval: number; // current interval
  status: "STOP" | "RUNNING"; // current status
}

Selectors

import { createCronSelector } from "@36node/redux-cron";

// define
const selector = createCronSelector("SOME_KEY");

// use
const cronState = selector(state);