- 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
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
/**
* Smaller version of the async library constructs.
* @ignore
*/
function _noop(): void
{ /* empty */
}
/**
* Ensures a function is only called once.
* @ignore
* @param {Function} fn - The function to wrap.
* @returns {Function} The wrapping function.
*/
function onlyOnce(fn: () => void): () => void
{
return function onceWrapper(this: any, ...args: any)
{
if (fn === null)
{
throw new Error('Callback was already called.');
}
const callFn = fn;
fn = null;
callFn.apply(this, args);
};
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IQueue
{
}
/**
* @private
* @memberof PIXI
*/
export class AsyncQueueItem<TaskData>
{
data: TaskData;
callback: (...args: any[]) => void;
/**
* @param data
* @param callback
* @private
*/
constructor(data: TaskData, callback: (...args: any[]) => void)
{
this.data = data;
this.callback = callback;
}
}
/**
* @private
* @memberof PIXI
*/
export class AsyncQueue<TaskData>
{
workers = 0;
concurrency: number;
buffer: number;
saturated: () => void = _noop;
unsaturated: () => void = _noop;
empty: () => void = _noop;
drain: () => void = _noop;
error: (err: Error, task: TaskData) => void = _noop;
started = false;
paused = false;
private _worker: (x: TaskData, next: () => void) => void;
_tasks: Array<AsyncQueueItem<TaskData>> = [];
/**
* @param worker
* @param concurrency
* @private
*/
constructor(worker: (x: TaskData, next: () => void) => void, concurrency = 1)
{
this._worker = worker;
if (concurrency === 0)
{
throw new Error('Concurrency must not be zero');
}
this.concurrency = concurrency;
this.buffer = concurrency / 4.0;
}
private _insert = (data: any, insertAtFront: boolean, callback?: () => void) =>
{
if (callback && typeof callback !== 'function')
{
throw new Error('task callback must be a function');
}
this.started = true;
// eslint-disable-next-line no-eq-null,eqeqeq
if (data == null && this.idle())
{
// call drain immediately if there are no tasks
setTimeout(() => this.drain(), 1);
return;
}
const item = new AsyncQueueItem<TaskData>(
data,
typeof callback === 'function' ? callback : _noop
);
if (insertAtFront)
{
this._tasks.unshift(item);
}
else
{
this._tasks.push(item);
}
setTimeout(this.process, 1);
};
process = (): void =>
{
while (!this.paused && this.workers < this.concurrency && this._tasks.length)
{
const task = this._tasks.shift();
if (this._tasks.length === 0)
{
this.empty();
}
this.workers += 1;
if (this.workers === this.concurrency)
{
this.saturated();
}
this._worker(task.data, onlyOnce(this._next(task)));
}
};
/**
* @param task
* @private
*/
_next(task: AsyncQueueItem<TaskData>): (...args: any) => void
{
return (...args: any) =>
{
this.workers -= 1;
task.callback(...args);
// eslint-disable-next-line no-eq-null,eqeqeq
if (args[0] != null)
{
this.error(args[0], task.data);
}
if (this.workers <= (this.concurrency - this.buffer))
{
this.unsaturated();
}
if (this.idle())
{
this.drain();
}
this.process();
};
}
// That was in object
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
push(data: any, callback?: (...args: any[]) => void): void
{
this._insert(data, false, callback);
}
kill(): void
{
this.workers = 0;
this.drain = _noop;
this.started = false;
this._tasks = [];
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
unshift(data: any, callback?: (...args: any[]) => void): void
{
this._insert(data, true, callback);
}
length(): number
{
return this._tasks.length;
}
running(): number
{
return this.workers;
}
idle(): boolean
{
return this._tasks.length + this.workers === 0;
}
pause(): void
{
if (this.paused === true)
{
return;
}
this.paused = true;
}
resume(): void
{
if (this.paused === false)
{
return;
}
this.paused = false;
// Need to call this.process once per concurrent
// worker to preserve full concurrency after pause
for (let w = 1; w <= this.concurrency; w++)
{
this.process();
}
}
/**
* Iterates an array in series.
* @param {Array.<*>} array - Array to iterate.
* @param {Function} iterator - Function to call for each element.
* @param {Function} callback - Function to call when done, or on error.
* @param {boolean} [deferNext=false] - Break synchronous each loop by calling next with a setTimeout of 1.
*/
static eachSeries(array: Array<any>, iterator: (x: any, next: (err?: any) => void) => void,
callback?: (err?: any) => void, deferNext?: boolean): void
{
let i = 0;
const len = array.length;
function next(err?: any)
{
if (err || i === len)
{
if (callback)
{
callback(err);
}
return;
}
if (deferNext)
{
setTimeout(() =>
{
iterator(array[i++], next);
}, 1);
}
else
{
iterator(array[i++], next);
}
}
next();
}
/**
* Async queue implementation,
* @param {Function} worker - The worker function to call for each task.
* @param {number} concurrency - How many workers to run in parrallel.
* @returns {*} The async queue object.
*/
static queue(worker: (x: any, next: (...args: any) => void) => void, concurrency?: number): AsyncQueue<any>
{
return new AsyncQueue<any>(worker, concurrency);
}
}