Skip to content
This repository has been archived by the owner on Jun 3, 2020. It is now read-only.

Commit

Permalink
Update PeerSocket API to support read/write/duplex streams
Browse files Browse the repository at this point in the history
  • Loading branch information
pfrazee committed Oct 7, 2018
1 parent 06c8980 commit 389cb6b
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions web-apis/fg/peer-socket.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals ReadableStream */
/* globals ReadableStream WritableStream */

const errors = require('beaker-error-constants')
const {EventTarget, Event, fromEventStream} = require('./event-target')
Expand Down Expand Up @@ -113,22 +113,38 @@ exports.setup = function (rpc) {
TAB_IDENT = +n || 0
}

async send (data) {
async write (data) {
peerSocketRPC.socketSend(TAB_IDENT, this.lobby.type, this.lobby.name, this.id, data)
}

createMessageStream () {
createReadStream () {
var messageEventHandler
const cancel = () => this.removeEventListener('message', messageEventHandler)
return new ReadableStream({
start: (controller) => {
messageEventHandler = e => controller.enqueue(e.message)
this.addEventListener('message', messageEventHandler)
this.addEventListener('close', () => {
controller.close()
cancel()
})
},
cancel: () => {
this.removeEventListener('message', messageEventHandler)
}
cancel
})
}

createWriteStream () {
return new WritableStream({
write: (data) => this.write(data)
// NOTE
// currently not possible to close or cancel a peersocket
// -prf
})
}

createDuplexStream () {
return {readable: this.createReadStream(), writable: this.createWriteStream()}
}
}

function setImmutableAttr (obj, name, value) {
Expand Down

0 comments on commit 389cb6b

Please sign in to comment.