aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/port-stream.js
diff options
context:
space:
mode:
Diffstat (limited to 'app/scripts/lib/port-stream.js')
-rw-r--r--app/scripts/lib/port-stream.js36
1 files changed, 36 insertions, 0 deletions
diff --git a/app/scripts/lib/port-stream.js b/app/scripts/lib/port-stream.js
new file mode 100644
index 000000000..d256efc9a
--- /dev/null
+++ b/app/scripts/lib/port-stream.js
@@ -0,0 +1,36 @@
+const Duplex = require('readable-stream').Duplex
+const inherits = require('util').inherits
+
+module.exports = PortDuplexStream
+
+
+inherits(PortDuplexStream, Duplex)
+
+function PortDuplexStream(port){
+ Duplex.call(this, {
+ objectMode: true,
+ })
+ this._port = port
+ port.onMessage.addListener(this._onMessage.bind(this))
+}
+
+// private
+
+PortDuplexStream.prototype._onMessage = function(msg){
+ // console.log('PortDuplexStream - saw message', msg)
+ this.push(msg)
+}
+
+// stream plumbing
+
+PortDuplexStream.prototype._read = noop
+
+PortDuplexStream.prototype._write = function(msg, encoding, cb){
+ // console.log('PortDuplexStream - sent message', msg)
+ this._port.postMessage(msg)
+ cb()
+}
+
+// util
+
+function noop(){} \ No newline at end of file