mpsc-channel
Version:
Multi-producer, single-consumer FIFO queue communication primitives
2 lines (1 loc) • 505 B
JavaScript
class s{constructor(e){this.values=[];e(this)}}class r{constructor(e){e(this)}recv(e){const c=this.channel.values.pop();return c&&(this.channel.receiver=e,this.channel.receiver(c)),c}}class a{constructor(e){e(this)}send(e){this.channel.values.unshift(e),typeof this.channel.receiver=="function"&&this.channel.values.length&&this.channel.receiver(this.channel.values.pop())}}function h(){const n=new s(l=>{l.values=[]}),e=new a(l=>{l.channel=n}),c=new r(l=>{l.channel=n});return[e,c]}export{h as channel};