@buzuli/scheduler

2.0.0 • Public • Published

scheduler

Build Status NPM version

A simple, in-memory scheduler.

Usage

Import and initialize

const scheduler = require('@buzuli/scheduler')()

Schedule after a delay

scheduler.after(1000, () => console.log('a second has elapsed'), 'some-context')

Schedule at a specific time

scheduler.at(Date.now + 500, () => console.log('half a second has elapsed'), {some: 'ctxt'})

Pause the scheduler (timer will be cleared, permitting a clean exit)

scheduler.pause()

Resume the scheduler (will immediately invoke all due or past-due actions)

scheduler.resume()

Cancel a scheduled action

const {cancel, context, when} = scheduler.after(
  100, () => console.log('not happening'), 'nil'
)

cancel()
console.log(`Cancelled '${context}' scheduled to run at '${new Date(when)}'`)

Cancel all actions passing a filtering function

scheduler.after(1000, () => console.log('hello'))
scheduler.after(1300, () => console.log('ye'), 'yo')
scheduler.after(1700, () => console.log('old'), 'yo')
scheduler.after(2000, () => console.log('world'))
scheduler.after(10000, () => console.log('...'))

scheduler.cancel(({when, context}) => context === 'yo' || when >= (Date.now() + 5000))

Readme

Keywords

Package Sidebar

Install

npm i @buzuli/scheduler

Weekly Downloads

38

Version

2.0.0

License

ISC

Unpacked Size

10.4 kB

Total Files

6

Last publish

Collaborators

  • buzuli