git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 318e88b7d16e694a7a34015a3c1a6b831c91c49e

Files: 318e88b7d16e694a7a34015a3c1a6b831c91c49e / port.js

704 bytesRaw
1module.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 unshift (message) {
39 return this._queue.unshift(message)
40 }
41}
42

Built with git-ssb-web