Created
March 10, 2016 21:12
-
-
Save stigok/8457d1e61431369916b6 to your computer and use it in GitHub Desktop.
Node.js Task & Worker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const _ = require('underscore'); | |
function Task(fn) { | |
this.fn = fn; | |
} | |
function Worker(interval, tasks) { | |
this.interval = interval; | |
this.tasks = tasks || []; | |
this._timers = []; | |
this.start = tick => { | |
let tasks = _.isFunction(tick) ? [tick, ...this.tasks] : this.tasks; | |
this._timers = _.map(tasks, task => setInterval(task, this.interval)); | |
}; | |
this.stop = () => { | |
_.each(this._timers, timer => clearInterval(timer)); | |
}; | |
} | |
module.exports = { | |
Task: Task, | |
Worker: Worker | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment