Files: 3820100dc50eb86a7f6f941410dc7e8dd2c7aaa7 / port.js
637 bytesRaw
1 | module.exports = class Port { |
2 | constructor (name) { |
3 | this.name = name |
4 | this._queue = [] |
5 | this.ticks = 0 |
6 | } |
7 | |
8 | queue (message) { |
9 | this.ticks = message._ticks |
10 | if (this._resolve) { |
11 | return this._resolve(message) |
12 | } else { |
13 | this._queue.push(message) |
14 | } |
15 | } |
16 | |
17 | // this only workls for one Promise |
18 | nextMessage () { |
19 | const message = this.queue.shift() |
20 | |
21 | return new Promise((resolve, reject) => { |
22 | if (message) { |
23 | resolve(message) |
24 | } else { |
25 | this._resolve = resolve |
26 | } |
27 | }) |
28 | } |
29 | |
30 | peek () { |
31 | return this._queue[0] |
32 | } |
33 | |
34 | shift () { |
35 | return this._queue.shift() |
36 | } |
37 | } |
38 |
Built with git-ssb-web