1 |
|
2 |
|
3 |
|
4 |
|
5 | (function(define) { 'use strict';
|
6 | define(function(require) {
|
7 |
|
8 | var Queue = require('./Queue');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 | function Scheduler(async) {
|
19 | this._async = async;
|
20 | this._queue = new Queue(15);
|
21 | this._afterQueue = new Queue(5);
|
22 | this._running = false;
|
23 |
|
24 | var self = this;
|
25 | this.drain = function() {
|
26 | self._drain();
|
27 | };
|
28 | }
|
29 |
|
30 | |
31 |
|
32 |
|
33 |
|
34 | Scheduler.prototype.enqueue = function(task) {
|
35 | this._add(this._queue, task);
|
36 | };
|
37 |
|
38 | |
39 |
|
40 |
|
41 |
|
42 | Scheduler.prototype.afterQueue = function(task) {
|
43 | this._add(this._afterQueue, task);
|
44 | };
|
45 |
|
46 | |
47 |
|
48 |
|
49 | Scheduler.prototype._drain = function() {
|
50 | runQueue(this._queue);
|
51 | this._running = false;
|
52 | runQueue(this._afterQueue);
|
53 | };
|
54 |
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | Scheduler.prototype._add = function(queue, task) {
|
62 | queue.push(task);
|
63 | if(!this._running) {
|
64 | this._running = true;
|
65 | this._async(this.drain);
|
66 | }
|
67 | };
|
68 |
|
69 | |
70 |
|
71 |
|
72 |
|
73 | function runQueue(queue) {
|
74 | while(queue.length > 0) {
|
75 | queue.shift().run();
|
76 | }
|
77 | }
|
78 |
|
79 | return Scheduler;
|
80 |
|
81 | });
|
82 | }(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }));
|