Coder Social home page Coder Social logo

stateful-dynamic-interval's Introduction

stateful-dynamic-interval

๐Ÿ• The stateful dynamic interval


build npm

A pauseable and resumeable setInterval built around dynamic-interval

Install

npm install --save stateful-dynamic-interval

then

import setStatefulDynterval from 'stateful-dynamic-interval'

or

import { StatefulDynterval } from 'stateful-dynamic-interval'

Usage

import { StatefulDynterval } from 'stateful-dynamic-interval'

const timer = new StatefulDynterval(context => console.log('tick', context), 1000)

timer.pause()
// ...
timer.resume()

You may also customize the underlying timer and can change the duration of the interval on each tick.

Check out the dynamic-interval package for more details.

Example

This script doubles the amount of time between intervals on each iteration, starting with 50ms.

import { StatefulDynterval } from 'stateful-dynamic-interval'

const timer = new StatefulDynterval(context => ({ wait: context.wait * 2 }), 50)

setTimeout(() => {
  timer.pause()

  setTimeout(() => {
    // this resumed step will only run for the remaining time in the interval,
    // which may be dynamic, meaning it can change on each iteration based on
    // the return value of the callback function (this example is dynamic)
    timer.resume()
  }, 1000)
}, 1000)

Interface

setStatefulDynterval(step, config, api)

.run()

Starts the interval. Instantiated StatefulDyntervals will automatically call run unless the lazy config property is set to true.

  • Alias: play

.clear()

Stops or clears out the interval. Once an interval has been cleared it cannot be resumed.

  • Alias: stop

.pause()

Pauses the interval so that it can be resumed at a later point.

.resume()

Resumes a previously paused interval.

.add(interval)

Synchronizes the parent interval with a child interval.

Child intervals automatically subscribe to the following topics of their parents:

  • run
  • clear
  • pause
  • resume

.detach()

Desynchronizes a parent interval from all of its children by unsubscribing them from their parent topics.

License

MIT

stateful-dynamic-interval's People

Contributors

slurmulon avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

stateful-dynamic-interval's Issues

[Bug] Support interrupting resume of a pause (?)

If you pause an interval and then resume it, that resumption (setTimeout) could take a decent amount of time to complete. If the interval was say 5 seconds, and you paused halfway through, you have 2.5 seconds until the next step runs (i.e. resumes)

The problem here is that somebody might pause again WHILE you're already resuming from another pause, and the setTimeout currently being used right now cannot be interrupted.

resume essentially puts the user into a temporary limbo state where the interval can't be paused.

This could easily get pretty messy and add a bit more complexity, so this requires more thought.

[Feature] Support hierarchical intervals

It would be nice if you could group intervals together in a hierarchical fashion (DAG).

Resuming, pausing or stopping a parent interval would be reflected in any child intervals.

It would be nice to support bi-directional subscriptions built around EventEmitter, but this can be addressed in a subsequent issue.

This feature should also fix #1, as the resume timer can simply be a child of the parent interval.

[Bug] Action method cannot be an async generator

Example

import { setStatefulDynterval } from 'stateful-dynamic-interval'
import axios from 'axios'

setStatefulDynterval(async ctx => {
  await axios.get('https://reqres.in/api/users')

  return ctx
})

Result

Causes an infinite loop/recursion

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.