Coder Social home page Coder Social logo

redux-timer-middleware's Introduction

redux-timer-middleware

Simple middleware to dispatch actions periodically

Installation

$ npm i --save redux-timer-middleware

Usage

import timerMiddleware from 'redux-timer-middleware'

applyMiddleware(timerMiddleware)(createStore)

Start Timer

You need to dispatch action START_TIMER and provide in payload:

  • actionName (required) - action that will be dispatched each timer tick
  • timerName (required) - timer name (need for stop feature)
  • actionPayload - payload that will be provided to the actionName
  • timerInterval - interval in ms (default 1000)
  • timerPeriod - tick count after which timer ends, when timer ends - action name with _END will be dispatched

Stop Timer or Stop multiple timers

You need to dispatch action STOP_TIMER and provide in payload:

  • timerName (required) - timer name that we stop
  • timerNames [] - timer names in array if we need to stop multiple timers

Examples

Infinite timer:

import {START_TIMER} from 'redux-timer-middleware';

dispatch({
    type: START_TIMER,
    payload: {
        actionName: 'SOME_ACTION_TICK',
        timerName: 'infiniteTimer'
    }
});

With a payload:

import {START_TIMER} from 'redux-timer-middleware';

dispatch({
    type: START_TIMER,
    payload: {
        actionName: 'SOME_ACTION_TICK',
        actionPayload: { /* my cool payload */ },
        timerName: 'infiniteTimer'
    }
});

To stop this timer:

import {STOP_TIMER} from 'redux-timer-middleware';

dispatch({
    type: STOP_TIMER,
    payload: {
        timerName: 'infiniteTimer'
    }
});

Timer that ends after 10 seconds:

import {START_TIMER} from 'redux-timer-middleware';

dispatch({
    type: START_TIMER,
    payload: {
        actionName: 'SOME_ACTION_TICK',
        timerName: 'testTimer',
        timerPeriod: 10
    }
});

After timer ends action with type 'SOME_ACTION_TICK_END' will be dispatched

redux-timer-middleware's People

Contributors

ahmednuaman avatar matpaul avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

redux-timer-middleware's Issues

Android Warning - Setting a timer for a long period of time

Thanks for the library!

I've started a couple of timers for my project, like the one below:

dispatch({
  type: START_TIMER,
  payload: {
    actionName: 'TOKEN_EXPIRED',
    timerName: 'refreshToken',
    timerInterval: 45 * 60 * 1000,
  },
});

On Android, I'm getting one of these warnings for for every timer. I guess that's normal, right?

Setting a timer for a long period of time, i.e. multiple minutes, is a performance and correctness issue on Android as it keeps the timer module awake, and timers can only be called when the app is in the foreground. See https://github.com/facebook/react-native/issues/12981 for more info.
(Saw setInterval with duration 2640000ms)
- node_modules/expo/build/environment/muteWarnings.fx.js:17:23 in warn
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:274:8 in setInterval
- node_modules/redux-timer-middleware/lib/index.js:87:73 in <unknown>
- node_modules/redux-timer-middleware/lib/index.js:40:35 in <unknown>
* src/actions/authentication.actions.ts:67:13 in <unknown>
* src/config/application-loading.tsx:129:33 in _callee$
- node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:45:44 in tryCatch
- node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:271:30 in invoke

Is there any workaround to muting these warnings?

actionPayload is not implemented for endless timers

ActionPayload doesn't work for endless timers since it's only used when timerPeriod is set. As a workaround I set timerPeriod to -1 which will result in an infinit timer since timerPeriod never will be 0.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.