-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathdeferred-action-queues.ts
110 lines (92 loc) · 2.82 KB
/
deferred-action-queues.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { IQueueItem } from './interfaces';
import Queue, { QUEUE_STATE } from './queue';
export interface IDebugInfo {
[key: string]: IQueueItem[] | undefined;
}
export default class DeferredActionQueues {
public queues: { [name: string]: Queue } = {};
public queueNameIndex = 0;
private queueNames: string[];
constructor(queueNames: string[] = [], options: any) {
this.queueNames = queueNames;
queueNames.reduce(function(queues, queueName) {
queues[queueName] = new Queue(queueName, options);
return queues;
}, this.queues);
}
/**
* @method schedule
* @param {String} queueName
* @param {Any} target
* @param {Any} method
* @param {Any} args
* @param {Boolean} onceFlag
* @param {Any} stack
* @return queue
*/
public schedule(queueName: string, target: any, method: any, args: any, onceFlag: boolean, stack: any) {
let queues = this.queues;
let queue = queues[queueName];
if (queue === undefined) {
throw new Error(`You attempted to schedule an action in a queue (${queueName}) that doesn\'t exist`);
}
if (method === undefined || method === null) {
throw new Error(`You attempted to schedule an action in a queue (${queueName}) for a method that doesn\'t exist`);
}
this.queueNameIndex = 0;
if (onceFlag) {
return queue.pushUnique(target, method, args, stack);
} else {
return queue.push(target, method, args, stack);
}
}
/**
* DeferredActionQueues.flush() calls Queue.flush()
*
* @method flush
* @param {Boolean} fromAutorun
*/
public flush(fromAutorun = false) {
let queue;
let queueName;
let numberOfQueues = this.queueNames.length;
while (this.queueNameIndex < numberOfQueues) {
queueName = this.queueNames[this.queueNameIndex];
queue = this.queues[queueName];
if (queue.hasWork() === false) {
this.queueNameIndex++;
if (fromAutorun && this.queueNameIndex < numberOfQueues) {
return QUEUE_STATE.Pause;
}
} else {
if (queue.flush(false /* async */) === QUEUE_STATE.Pause) {
return QUEUE_STATE.Pause;
}
}
}
}
/**
* Returns debug information for the current queues.
*
* @method _getDebugInfo
* @param {Boolean} debugEnabled
* @returns {IDebugInfo | undefined}
*/
public _getDebugInfo(debugEnabled: boolean): IDebugInfo | undefined {
if (debugEnabled) {
let debugInfo: IDebugInfo = {};
let queue: Queue;
let queueName: string;
let numberOfQueues: number = this.queueNames.length;
let i: number = 0;
while (i < numberOfQueues) {
queueName = this.queueNames[i];
queue = this.queues[queueName];
debugInfo[queueName] = queue._getDebugInfo(debugEnabled);
i++;
}
return debugInfo;
}
return;
}
}