-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathqueue-with-sizes.js
More file actions
61 lines (51 loc) · 1.74 KB
/
queue-with-sizes.js
File metadata and controls
61 lines (51 loc) · 1.74 KB
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
'use strict';
const assert = require('assert');
const { IsNonNegativeNumber, StructuredTransferOrClone } = require('./miscellaneous.js');
exports.DequeueValue = container => {
assert('_queue' in container && '_queueTotalSize' in container);
assert(container._queue.length > 0);
const pair = container._queue.shift();
container._queueTotalSize -= pair.size;
if (container._queueTotalSize < 0) {
container._queueTotalSize = 0;
}
return pair.value;
};
exports.EnqueueValueWithSize = (container, value, size, transferList) => {
assert('_queue' in container && '_queueTotalSize' in container);
if (!IsNonNegativeNumber(size)) {
throw new RangeError('Size must be a finite, non-NaN, non-negative number.');
}
if (size === Infinity) {
throw new RangeError('Size must be a finite, non-NaN, non-negative number.');
}
if (container._isOwning) {
value = StructuredTransferOrClone(value, transferList);
}
container._queue.push({ value, size });
container._queueTotalSize += size;
};
exports.PeekQueueValue = container => {
assert('_queue' in container && '_queueTotalSize' in container);
assert(container._queue.length > 0);
const pair = container._queue[0];
return pair.value;
};
const disposeStepsSymbol = Symbol('dispose-steps');
exports.ResetQueue = container => {
assert('_queue' in container && '_queueTotalSize' in container);
if (container._isOwning) {
while (container._queue.length > 0) {
const value = exports.DequeueValue(container);
if (typeof value[disposeStepsSymbol] === 'function') {
try {
value[disposeStepsSymbol]();
} catch (closeException) {
// Nothing to do.
}
}
}
}
container._queue = [];
container._queueTotalSize = 0;
};