tickable-timer

Manual ticking timer API

Usage no npm install needed!

<script type="module">
  import tickableTimer from 'https://cdn.skypack.dev/tickable-timer';
</script>

README

tickable-timer

Build Status NPM Version License

Manual ticking timer API for test CI

Features

  • Compatible with the native timer API
  • Provides hook events before/after ticking

Installation

npm install tickable-timer

API

Properties

  • timers: number[]
  • intervals: number[]
  • timeouts: number[]

Methods

Inherits methods from EventEmitter.

  • setTimeout(callback: function, delay: number, ...params: any): number
  • clearTimeout(timerId: number): void
  • setInterval(callback: function, delay: number, ...params: any): number
  • clearInterval(timerId: number): void
  • tick(tick: number = 1): void
  • clearAllTimers(): void

Events

  • tick
    • tick: number The ticking interval
  • ticked
    • tick: number The ticking interval

Usage

function counter(start, stop, interval, timerAPI = global) {
  let emitter = new EventEmitter();
  let count = start;
  let timerId = timerAPI.setInterval(() => {
    emitter.emit("bang", count++);
    if (stop <= count) {
      emitter.emit("end");
      timerAPI.clearInterval(timerId);
    }
  }, interval);

  return emitter;
}

// in the production (use native timer)
counter(0, 10, 1000).on("bang", (count) => {
  console.log(`bang! ${count}`);
});

// in the test (use tickable timer)
import tickable from "tickable-timer";

counter(0, 10, 1000, tickable).on("bang", spy);

Example

import tickable from "tickable-timer";

tickable.setTimeout(() => {
  console.log("fired: timer1");
}, 1000);

tickable.setInterval(() => {
  console.log("fired: timer2");
}, 500);

tickable.setTimeout(() => {
  // never called
  console.log("fired: timer3");
}, 3000);

tickable.setInterval(() => {
  // never called
  console.log("fired: timer4");
}, 3000);

tickable.setTimeout(() => {
  console.log("clear: all timers");
  tickable.clearAllTimers();
  console.log("timers:", tickable.timers);
}, 2500);

tickable.on("tick", (tick) => {
  console.log(`tick: ${tick}`);
});
tickable.on("ticked", (tick) => {
  console.log(`ticked: ${tick}\n`);
});

tickable.tick(5000);

output:

tick: 500
fired: timer2
ticked: 500

tick: 500
fired: timer1
fired: timer2
ticked: 500

tick: 500
fired: timer2
ticked: 500

tick: 500
fired: timer2
ticked: 500

tick: 500
fired: timer2
clear: all timers
timers: []
ticked: 500

tick: 2500
ticked: 2500

License

MIT