🚀 DAY 5 OF LAUNCH WEEK:Introducing Webhook Events for Alert Changes.Learn more →
Socket
Book a DemoInstallSign in
Socket

@antongolub/repeater

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@antongolub/repeater

Helper for creating auto-calling functions

latest
Source
npmnpm
Version
1.5.2
Version published
Maintainers
1
Created
Source

@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

Keywords

repeater

FAQs

Package last updated on 16 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts