-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.js
148 lines (128 loc) · 3.44 KB
/
queue.js
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
'use strict'
const Shifter = require('./shifter')
const consume = require('./consume')
class Sync {
constructor (queue) {
this.async = queue
}
get sync () {
return this
}
get size () {
return this.async.size
}
shifter () {
return this.async.shifter().sync
}
// Do not awake until all values are enqueued.
_push (value, heft) {
const queue = this.async
queue.size += heft
queue._head = queue._head.next = {
next: null,
heft: heft,
value: value,
end: value == null,
count: 0,
shifters: 0,
unshifters: 0
}
}
push (value) {
this._push(value, 1)
this.async._resolve()
}
enqueue (values) {
for (let value of values) {
this._push(value, 1)
}
this.async._resolve()
}
consume (iterable, arrayed = false) {
consume.sync(this, iterable, arrayed)
}
}
class Queue {
constructor (max = Infinity, heftify = null) {
this.shifters = 0
this.size = 0
this.max = max
this.heftify = heftify
this._head = { next: null }
this._shifting = []
this._enqueuing = []
this.sync = new Sync(this)
}
get async () {
return this
}
shifter () {
this._head = this._head.next = {
next: null,
value: null,
end: false,
count: 0,
shifters: 1,
unshifters: 0
}
this.shifters++
return new Shifter(this, this._head)
}
_resolve () {
if (this._shifting.length != 0) {
for (const resolve of this._shifting.splice(0)) {
resolve.call()
}
}
}
_twist () {
if (this._enqueuing.length != 0 && this.size < this.max) {
this._enqueuing.shift().call()
}
}
push (value) {
if (this.shifters != 0) {
const heft = this.heftify == null ? 1 : (this.heftify)(value)
if (this.size + heft > this.max) {
return (async () => {
while (this.size + heft > this.max) {
await new Promise(resolve => this._enqueuing.push(resolve))
}
this.sync._push(value, heft)
this._resolve()
if (this._enqueuing.length != 0 && this.size < this.max) {
this._enqueuing.shift().call()
}
}) ()
}
this.sync._push(value, heft)
this._resolve()
return null
}
return null
}
async enqueue (values) {
if (this.shifters != 0) {
if (this.heftify == null && values.length + this.size < this.max) {
this.sync.enqueue(values)
} else {
for (const value of values) {
const promise = this.push(value)
if (promise != null) {
await promise
}
}
}
}
}
async join (f) {
const shifter = this.shifter()
const entry = await shifter.join(f)
shifter.destroy()
return entry
}
async consume (iterable, arrayed = false) {
return consume.async(this, iterable, arrayed)
}
}
exports.Queue = Queue