0.1.0 • Published 6 years ago

wheel-timer v0.1.0

Weekly downloads
4
License
Apache
Repository
github
Last release
6 years ago

Hashed and Hierarchical Timing Wheels Implement

Build Status License Prettier Node npm version Size Coverage Status Dependency

npm.io

Usage

const HashWheelTimer = require('wheel-timer');

const timer = new HashWheelTimer(60);

timer.on('tick', values => {});

timer.add({ uid: 'xxxx-xxxx-xxxx' });

setInterval(() => {
  timer.tick();
}, 1000);

Example

Here is a example to use with multiple time wheel

const HashWheelTimer = require('../index');

class SecondTimer extends HashWheelTimer {
  constructor() {
    super(60);
    this.currentIndex = new Date().getSeconds(); // get current minute and set
  }
  tick() {
    const beforeRound = this.round;
    super.tick();
    const afterRound = this.round;

    if (afterRound > beforeRound) {
      minute.tick();
    }
  }
}

class MinuteTimer extends HashWheelTimer {
  constructor() {
    super(60);
    this.currentIndex = new Date().getMinutes(); // get current minute and set
  }
  tick() {
    const beforeRound = this.round;
    super.tick();
    const afterRound = this.round;

    if (afterRound > beforeRound) {
      hour.tick();
    }
  }
}

class HourTimer extends HashWheelTimer {
  constructor() {
    super(24);
    this.currentIndex = new Date().getHours(); // get current hour and set
  }
}

const second = new SecondTimer();
const minute = new MinuteTimer();
const hour = new HourTimer();

second.on('tick', () => {
  console.log(`current second: ${second.currentIndex}`);
});

minute.on('tick', () => {
  console.log(`current minute: ${minute.currentIndex}`);
});

hour.on('tick', () => {
  console.log(`current hour: ${hour.currentIndex}`);
});

setInterval(() => {
  second.tick();
}, 1000);

Contributing

Contributing Guid

Contributors

Axetroy💻 🐛 🎨

License

FOSSA Status