๐Ÿ“ฆ antongolub / repeater

Helper for creating repetitive functions

โ˜… 1 stars โ‘‚ 0 forks ๐Ÿ‘ 1 watching โš–๏ธ MIT License
๐Ÿ“ฅ Clone https://github.com/antongolub/repeater.git
HTTPS git clone https://github.com/antongolub/repeater.git
SSH git clone git@github.com:antongolub/repeater.git
CLI gh repo clone antongolub/repeater
renovate[bot] renovate[bot] chore(deps): update paambaati/codeclimate-action action to v4 731b94e 2 years ago ๐Ÿ“ History
๐Ÿ“‚ master View all commits โ†’
๐Ÿ“ .github
๐Ÿ“ flow-typed
๐Ÿ“ src
๐Ÿ“ typings
๐Ÿ“„ .babelrc
๐Ÿ“„ .esdoc.json
๐Ÿ“„ .flowconfig
๐Ÿ“„ .gitignore
๐Ÿ“„ .npmignore
๐Ÿ“„ CHANGELOG.md
๐Ÿ“„ jest.config.json
๐Ÿ“„ LICENSE
๐Ÿ“„ package.json
๐Ÿ“„ README.md
๐Ÿ“„ renovate.json
๐Ÿ“„ yarn.lock
๐Ÿ“„ README.md

@antongolub/repeater

Helper for creating auto-calling functions

CI Maintainability Test Coverage npm (tag)

Motivation

The wheel was invented a long time ago: repeat. What's the diff?
  • Repeater stores the last call params and uses them for next invocations.
  • Inherits target's iface.
  • Allows to combine manual and automated calls.

Usage

import repeater from '@antongolub/repeater'
    
    const target = step => { this.i += step }
    const context = { i: 0 }
    const delay = 1000
    const rep = repeater(target, delay, context)
    
    rep(2)
    
    // Imagine, 5 seconds later new 'manual' call occurs
    setTimeout(() => rep(1), 5000)

    // ~10 seconds after start: 
    setTimeout(() => console.log(context.i), 10000) // 15

Repeater is just a wrapper around the target function. It exposes several util props:

PropDescription
delayinterval in ms
timeoutTimeoutID
targetref for original target function
contextoptional scope
limitoptional remainder of calls
argsarguments of the last invocation
So, anytime you're let to interrupt the repetitive call by clearing timeout:
clearTimeout(rep.timeout)

Parametrization

const rep1 = repeater(target, delay, context, limit)
    const rep2 = repeater({target, delay, context, limit})

License

MIT