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

Commit 9283c40

Browse files
committed
move electron shell to the refactored panel
1 parent c85008d commit 9283c40

File tree

5 files changed

+83
-230
lines changed

5 files changed

+83
-230
lines changed

frontend/Store.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,9 @@ class Store extends EventEmitter {
455455

456456
_removeFromNodesByName(id: ElementID) {
457457
var node = this._nodes.get(id);
458-
this._nodesByName = this._nodesByName.set(node.get('name'), this._nodesByName.get(node.get('name')).delete(id));
458+
if (node) {
459+
this._nodesByName = this._nodesByName.set(node.get('name'), this._nodesByName.get(node.get('name')).delete(id));
460+
}
459461
}
460462
}
461463

shells/electron/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
color: #aaa;
3838
}
3939
</style>
40+
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
4041
</head>
4142
<body>
4243
<div id="container">
@@ -45,7 +46,6 @@ <h2 id="waiting">Waiting for a connection from React Native</h2>
4546
<script>
4647
require('babel-core').register({stage: 0});
4748
require('./node');
48-
require('./panel/run');
4949
</script>
5050
</body>
5151
</html>

shells/electron/node/index.js

Lines changed: 79 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,37 @@
99
*/
1010
'use strict';
1111

12-
// this is not compiled by babel / webpack
1312
var ws = require('ws');
1413
var fs = require('fs');
1514
var path = require('path');
1615

17-
var socket = ws.connect('ws://localhost:8081/devtools');
16+
var globalHook = require('../../../backend/GlobalHook');
17+
globalHook(window);
18+
var Panel = require('../../../frontend/Panel');
19+
var React = require('react');
1820

19-
socket.onmessage = initialMessage;
21+
var node = document.getElementById('container');
22+
var wall = null;
2023

21-
function initialMessage(evt) {
22-
if (evt.data === 'attach:agent') {
23-
initialize();
24-
}
24+
var config = {
25+
reload,
26+
alreadyFoundReact: true,
27+
inject(done) {
28+
done(wall);
29+
},
30+
};
31+
32+
function reload() {
33+
React.unmountComponentAtNode(node);
34+
node.innerHTML = '';
35+
setTimeout(() => {
36+
React.render(<Panel {...config} />, node);
37+
}, 100);
2538
}
2639

27-
socket.onerror = function (err) {
28-
window.onDisconnected();
29-
console.log('error connection', err);
30-
};
31-
socket.onclose = function () {
32-
window.onDisconnected();
33-
console.log('error things');
40+
function onDisconnected() {
41+
React.unmountComponentAtNode(node);
42+
node.innerHTML = '<h2 id="waiting">Waiting for a connection from React Native</h2>';
3443
};
3544

3645
function initialize() {
@@ -39,43 +48,86 @@ function initialize() {
3948
return console.error('failed to load...', err);
4049
}
4150
socket.send('eval:' + backendScript.toString('utf8'));
51+
var listeners = [];
4252
socket.onmessage = function (evt) {
43-
// console.log('<<--', evt.data);
4453
var data = JSON.parse(evt.data);
4554
if (data.$close || data.$error) {
4655
console.log('Closing or Erroring');
47-
window.onDisconnected();
48-
socket.onmessage = initialMessage;
56+
onDisconnected();
57+
socket.onmessage = evt => {
58+
if (evt.data === 'attach:agent') {
59+
initialize(socket);
60+
}
61+
};
4962
return;
5063
}
5164
if (data.$open) {
5265
return; // ignore
5366
}
5467
listeners.forEach(function (fn) {fn(data); });
5568
};
56-
console.log('connected to react native');
57-
var listeners = [];
5869

59-
var wall = {
70+
wall = {
6071
listen(fn) {
6172
listeners.push(fn);
6273
},
6374
send(data) {
64-
// console.log('-->>' + JSON.stringify(data));
6575
socket.send(JSON.stringify(data));
6676
},
6777
disconnect() {
6878
socket.close();
6979
},
7080
};
7181

72-
window.onConnected(wall);
82+
console.log('connected');
83+
reload();
7384
});
7485
}
7586

76-
window.onConnected = function () {
77-
console.error('No onConnected set');
78-
};
79-
window.onDisconnected = function () {
80-
console.error('No onDisconnected set');
87+
/**
88+
* This is the normal mode, where it connects to the react native packager
89+
*/
90+
window.connectToSocket = function () {
91+
var socket = ws.connect('ws://localhost:8081/devtools');
92+
socket.onmessage = evt => {
93+
if (evt.data === 'attach:agent') {
94+
initialize(socket);
95+
}
96+
};
97+
socket.onerror = function (err) {
98+
onDisconnected();
99+
console.log('Error with websocket connection', err);
100+
};
101+
socket.onclose = function () {
102+
onDisconnected();
103+
console.log('Connection to RN closed');
104+
};
105+
}
106+
107+
/**
108+
* When the Electron app is running in "server mode"
109+
*/
110+
window.startServer = function () {
111+
var server = new ws.Server({port: 8097})
112+
var connected = false;
113+
server.on('connection', function (socket) {
114+
if (connected) {
115+
console.warn('only one connection allowed at a time');
116+
socket.close();
117+
return;
118+
}
119+
connected = true;
120+
socket.onerror = function (err) {
121+
connected = false;
122+
onDisconnected();
123+
console.log('Error with websocket connection', err);
124+
};
125+
socket.onclose = function () {
126+
connected = false;
127+
onDisconnected();
128+
console.log('Connection to RN closed');
129+
};
130+
initialize(socket);
131+
});
81132
};
133+

shells/electron/panel/Panel.js

Lines changed: 0 additions & 161 deletions
This file was deleted.

shells/electron/panel/run.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)