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