@@ -3,18 +3,44 @@ import updateState from 'remotedev-app/lib/store/updateState';
33import syncOptions from '../options/syncOptions' ;
44import createMenu from './contextMenus' ;
55import openDevToolsWindow from './openWindow' ;
6- let connections = { } ;
6+ let panelConnections = { } ;
7+ let tabConnections = { } ;
78let catchedErrors = { } ;
89
910window . syncOptions = syncOptions ; // Used in the options page
1011
1112const naMessage = { type : 'NA' } ;
1213
13- // Connect to devpanel
14- onConnect ( ( tabId ) => {
15- if ( tabId !== store . id ) return naMessage ;
16- return { } ;
17- } , { } , connections ) ;
14+ function initPanel ( msg , port ) {
15+ panelConnections [ msg . tabId ] = port ;
16+ if ( msg . tabId !== store . id ) return naMessage ;
17+ }
18+
19+ function initInstance ( msg , port ) {
20+ const id = port . sender . tab . id ;
21+ tabConnections [ id ] = port ;
22+ store . liftedStore . instances [ id ] = msg . instance ;
23+ store . id = id ;
24+ if ( typeof id === 'number' ) chrome . pageAction . show ( id ) ;
25+ return { type : 'START' } ;
26+ }
27+
28+ function disconnect ( port ) {
29+ if ( ! port . sender . tab ) return ;
30+ const id = port . sender . tab . id ;
31+ delete tabConnections [ id ] ;
32+ if ( panelConnections [ id ] ) panelConnections [ id ] . postMessage ( naMessage ) ;
33+ if ( window . store . liftedStore . instances [ id ] ) {
34+ delete window . store . liftedStore . instances [ id ] ;
35+ window . store . liftedStore . deleteInstance ( id ) ;
36+ }
37+ }
38+
39+ onConnect ( undefined , {
40+ INIT_PANEL : initPanel ,
41+ INIT_INSTANCE : initInstance ,
42+ RELAY : ( msg , port ) => { messaging ( msg . message , port . sender ) ; }
43+ } , panelConnections , disconnect ) ;
1844
1945function handleInstancesChanged ( instance , name ) {
2046 window . store . liftedStore . instances [ instance ] = name || instance ;
@@ -24,15 +50,6 @@ function handleInstancesChanged(instance, name) {
2450function messaging ( request , sender , sendResponse ) {
2551 const tabId = sender . tab ? sender . tab . id : sender . id ;
2652 if ( tabId ) {
27- if ( request . type === 'PAGE_UNLOADED' ) {
28- handleInstancesChanged ( tabId , undefined , true ) ;
29- if ( connections [ tabId ] ) connections [ tabId ] . postMessage ( naMessage ) ;
30- if ( window . store . liftedStore . instances [ tabId ] ) {
31- delete window . store . liftedStore . instances [ tabId ] ;
32- window . store . liftedStore . deleteInstance ( tabId ) ;
33- }
34- return true ;
35- }
3653 if ( request . type === 'GET_OPTIONS' ) {
3754 syncOptions . get ( options => {
3855 sendResponse ( { options : options } ) ;
@@ -60,14 +77,9 @@ function messaging(request, sender, sendResponse) {
6077 const payload = updateState ( store , request , handleInstancesChanged , store . liftedStore . instance ) ;
6178 if ( ! payload ) return true ;
6279
63- if ( request . init ) {
64- store . id = tabId ;
65- createMenu ( sender . url , tabId ) ;
66- }
67-
68- // Relay the message to the devTools page
69- if ( tabId in connections ) {
70- connections [ tabId ] . postMessage ( request ) ;
80+ // Relay the message to the devTools panel
81+ if ( tabId in panelConnections ) {
82+ panelConnections [ tabId ] . postMessage ( request ) ;
7183 }
7284
7385 // Notify when errors occur in the app
@@ -107,9 +119,9 @@ export function toContentScript(action) {
107119 const message = { type : 'DISPATCH' , action : action } ;
108120 let id = store . liftedStore . instance ;
109121 if ( ! id || id === 'auto' ) id = store . id ;
110- if ( id in connections ) {
111- connections [ id ] . postMessage ( message ) ;
122+ if ( id in panelConnections ) {
123+ panelConnections [ id ] . postMessage ( message ) ;
112124 } else {
113- sendToTab ( Number ( id ) , message ) ;
125+ tabConnections [ id ] . postMessage ( message ) ;
114126 }
115127}
0 commit comments