From aa2816010dc724e3976dc3e639179221cdce7d66 Mon Sep 17 00:00:00 2001
From: kumavis <aaron@kumavis.me>
Date: Thu, 5 May 2016 16:04:43 -0700
Subject: inpage - automatic dapp reload

---
 app/scripts/contentscript.js | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

(limited to 'app/scripts/contentscript.js')

diff --git a/app/scripts/contentscript.js b/app/scripts/contentscript.js
index a256a3f5b..1b7b98ec9 100644
--- a/app/scripts/contentscript.js
+++ b/app/scripts/contentscript.js
@@ -1,5 +1,7 @@
 const LocalMessageDuplexStream = require('./lib/local-message-stream.js')
 const PortStream = require('./lib/port-stream.js')
+const ObjectMultiplex = require('./lib/obj-multiplex')
+
 
 
 // inject in-page script
@@ -15,13 +17,22 @@ var pageStream = new LocalMessageDuplexStream({
   name: 'contentscript',
   target: 'inpage',
 })
+pageStream.on('error', console.error.bind(console))
 var pluginPort = chrome.runtime.connect({name: 'contentscript'})
 var pluginStream = new PortStream(pluginPort)
+pluginStream.on('error', console.error.bind(console))
 
-// forward communication across
-pageStream.pipe(pluginStream)
-pluginStream.pipe(pageStream)
+// forward communication plugin->inpage
+pageStream.pipe(pluginStream).pipe(pageStream)
 
-// log errors
-pageStream.on('error', console.error.bind(console))
-pluginStream.on('error', console.error.bind(console))
\ No newline at end of file
+// connect contentscript->inpage control stream
+var mx = ObjectMultiplex()
+mx.on('error', console.error.bind(console))
+mx.pipe(pageStream)
+var controlStream = mx.createStream('control')
+controlStream.on('error', console.error.bind(console))
+
+// if we lose connection with the plugin, trigger tab refresh 
+pluginStream.on('close', function(){
+  controlStream.write({ method: 'reset' })
+})
\ No newline at end of file
-- 
cgit