@36node/redux-cron 中文文档教程

发布于 4年前 浏览 23 项目主页 更新于 3年前

Redux Cron

用于执行 cron 作业的 redux 库

版本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);

Redux Cron

An redux library for doing cron job

versiondownloads

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);
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文