Plato on Github
Report Home
node_modules/when/lib/scheduler.js
Maintainability
73.99
Lines of code
66
Difficulty
23.92
Estimated Errors
0.41
Function weight
By Complexity
By SLOC
/** @license MIT License (c) copyright 2010-2014 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ (function(define) { 'use strict'; define(function(require) { var Queue = require('./Queue'); // Credit to Twisol (https://github.com/Twisol) for suggesting // this type of extensible queue + trampoline approach for next-tick conflation. function Scheduler(enqueue) { this._enqueue = enqueue; this._handlerQueue = new Queue(15); this._afterQueue = new Queue(5); this._running = false; var self = this; this.drain = function() { self._drain(); }; } /** * Enqueue a task. If the queue is not currently scheduled to be * drained, schedule it. * @param {function} task */ Scheduler.prototype.enqueue = function(task) { this._handlerQueue.push(task); if(!this._running) { this._running = true; this._enqueue(this.drain); } }; Scheduler.prototype.afterQueue = function(f, x, y) { this._afterQueue.push(f); this._afterQueue.push(x); this._afterQueue.push(y); if(!this._running) { this._running = true; this._enqueue(this.drain); } }; /** * Drain the handler queue entirely, being careful to allow the * queue to be extended while it is being processed, and to continue * processing until it is truly empty. */ Scheduler.prototype._drain = function() { var q = this._handlerQueue; while(q.length > 0) { q.shift().run(); } this._running = false; q = this._afterQueue; while(q.length > 0) { q.shift()(q.shift(), q.shift()); } }; return Scheduler; }); }(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }));