blob: 5df1e00921d8436407c9fe94743a1266178321d0 [file] [log] [blame]
murgatroid99221ae632015-12-11 15:06:32 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
murgatroid99221ae632015-12-11 15:06:32 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34'use strict';
35
36var _ = require('lodash');
37
38/**
murgatroid99496ca762016-01-04 15:25:09 -080039 * This class represents a queue of callbacks that must happen sequentially,
40 * each with a specific delay after the previous event.
murgatroid99221ae632015-12-11 15:06:32 -080041 */
42function AsyncDelayQueue() {
43 this.queue = [];
44
45 this.callback_pending = false;
46}
47
48/**
49 * Run the next callback after its corresponding delay, if there are any
50 * remaining.
51 */
52AsyncDelayQueue.prototype.runNext = function() {
53 var next = this.queue.shift();
54 var continueCallback = _.bind(this.runNext, this);
55 if (next) {
56 this.callback_pending = true;
57 setTimeout(function() {
58 next.callback(continueCallback);
59 }, next.delay);
60 } else {
61 this.callback_pending = false;
62 }
63};
64
65/**
66 * Add a callback to be called with a specific delay after now or after the
67 * current last item in the queue or current pending callback, whichever is
68 * latest.
69 * @param {function(function())} callback The callback
70 * @param {Number} The delay to apply, in milliseconds
71 */
72AsyncDelayQueue.prototype.add = function(callback, delay) {
73 this.queue.push({callback: callback, delay: delay});
74 if (!this.callback_pending) {
75 this.runNext();
76 }
77};
78
David Garcia Quintas2bf574f2016-01-14 15:27:08 -080079module.exports = AsyncDelayQueue;