Skip to content

Commit

Permalink
Moves the background services to promises so the menu will resolve.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrux committed Nov 3, 2018
1 parent 4446333 commit 3879d76
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 63 deletions.
19 changes: 11 additions & 8 deletions app/background/network-scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,17 @@ function scanNetwork(sender) {
}));
}

export function listenForNetworkScanner() {
writeLog("Waiting for Network Scans...");
ipcMain.on(types.SCAN_NETWORK,(evt) => {
const { sender } = evt;
scanNetwork(sender).then((results) => {
sender.send(types.SCAN_NETWORK_COMPLETE,results);
}).catch((err) => {
sender.send(types.SCAN_NETWORK_FAIL,err);
export function startScanner() {
return new Promise((resolve, reject) => {
ipcMain.on(types.SCAN_NETWORK,(evt) => {
const { sender } = evt;
scanNetwork(sender).then((results) => {
sender.send(types.SCAN_NETWORK_COMPLETE,results);
}).catch((err) => {
sender.send(types.SCAN_NETWORK_FAIL,err);
});
});
writeLog("Started Scanner Service");
resolve();
});
}
19 changes: 11 additions & 8 deletions app/server.js → app/background/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,15 @@ app.ws('/terminals/:pid', function (ws, req) {

module.exports = {
startServer() {
if (!port) {
console.error('Please provide a port: node ./src/server.js --port=XXXX');
process.exit(1);
} else {
app.listen(port, host);
writeLog('Workbench terminal emulator server listening at http://' + host + ':' + port);
}
return new Promise((resolve, reject) => {
if (!port) {
console.error('Please provide a port: node ./src/server.js --port=XXXX');
process.exit(1);
reject();
} else {
writeLog("Started Terminal Service");
resolve(app.listen(port, host));
}
});
}
}
}
44 changes: 24 additions & 20 deletions app/background/zmq.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,30 @@ function onMessage(sender, event_message, service, cb) {
sender.send(types.MESSAGE, msgResp);
}

export function listenForZmq() {
writeLog("Waiting for ZMQ...");
const sock = zmq.socket('sub');
sock.subscribe('');

let msgHandler;
ipcMain.on(types.CONNECT, (evt, ip, service) => {
const { sender } = evt;
const addr = `tcp://${ip}:${service.port}`;
msgHandler = throttle((msg) => { return onMessage(sender, msg, service); }, 200, { leading: true });
sock.on('message', msgHandler);
writeLog(`Connecting to ${addr}`, service.key);
sock.connect(addr);
});
export function startZmq() {
return new Promise((resolve, reject) => {
const sock = zmq.socket('sub');
sock.subscribe('');

let msgHandler;
ipcMain.on(types.CONNECT, (evt, ip, service) => {
const { sender } = evt;
const addr = `tcp://${ip}:${service.port}`;
msgHandler = throttle((msg) => { return onMessage(sender, msg, service); }, 200, { leading: true });
sock.on('message', msgHandler);
writeLog(`Connecting to ${addr}`, service.key);
sock.connect(addr);
});

ipcMain.on(types.DISCONNECT, (evt, ip, service) => {
const { sender } = evt;
const addr = `tcp://${ip}:${service.port}`;
sock.removeListener('message', msgHandler);
writeLog(`Disconnect from ${addr}`, service.key);
sock.disconnect(addr);
});

ipcMain.on(types.DISCONNECT, (evt, ip, service) => {
const { sender } = evt;
const addr = `tcp://${ip}:${service.port}`;
sock.removeListener('message', msgHandler);
writeLog(`Disconnect from ${addr}`, service.key);
sock.disconnect(addr);
writeLog("Started ØMQ Service");
resolve();
});
}
2 changes: 1 addition & 1 deletion app/components/Layout/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default class Header extends React.PureComponent {
onChange: this.onChangeIntent
});
const {borderColor} = props;
let title = 'Hyper';
let title = 'Workbench';
if (props.tabs.length === 1 && props.tabs[0].title) {
// if there's only one tab we use its title as the window title
title = props.tabs[0].title;
Expand Down
2 changes: 1 addition & 1 deletion app/components/Terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ReactTerminal extends React.Component {
getTermDocument() {
// eslint-disable-next-line no-console
// console.warn(
// 'The underlying terminal engine of Hyper no longer ' +
// 'The underlying terminal engine of Workbench no longer ' +
// 'uses iframes with individual `document` objects for each ' +
// 'terminal instance. This method call is retained for ' +
// "backwards compatibility reasons. It's ok to attach directly" +
Expand Down
31 changes: 8 additions & 23 deletions app/main.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { app, ipcMain, BrowserWindow } from 'electron';
import MenuBuilder from './menu';
import log from 'electron-log';
import { autoUpdater } from "electron-updater";
import { startServer } from './server';
import { listenForNetworkScanner } from './background/network-scanner';
import { listenForZmq } from './background/zmq';
import { startServer } from './background/server';
import { startScanner } from './background/network-scanner';
import { startZmq } from './background/zmq';
//-------------------------------------------------------------------
// Logging
//
Expand Down Expand Up @@ -97,39 +97,24 @@ app.on('ready', async () => {
// }

mainWindow = new BrowserWindow({
// show: false,
frame: (process.platform !== 'darwin') ? true : false,
titleBarStyle: (process.platform !== 'darwin') ? null : "hiddenInset",
// titleBarStyle: 'hiddenIn set',
backgroundColor: "#000000",
// width: 540,
// height: 640,
// maxWidth: 540,
// maxHeight: 640,
minWidth: 540,
minHeight: 640
});
let webContents = mainWindow.webContents;

mainWindow.loadURL(`file://${__dirname}/app.html`);

startServer();
listenForNetworkScanner();
listenForZmq();

Promise.all({
server: startServer(),
scanner: startScanner(),
zmq: startZmq()
});
const menuBuilder = new MenuBuilder(mainWindow);
menuBuilder.buildMenu();

// mainWindow.on('resize', () => {
// console.log(store.get('windowBounds'));
// The event doesn't pass us the window size, so we call the `getBounds` method which returns an object with
// the height, width, and x and y coordinates.
// let { width, height } = mainWindow.getBounds();
// Now that we have them, save them using the `set` method.
// settings.set('windowBounds', { width, height });
// });
// @TODO: Use 'ready-to-show' event
// https://github.com/electron/electron/blob/master/docs/api/browser-window.md#using-ready-to-show-event
webContents.on('did-finish-load', () => {
if (!mainWindow) {
throw new Error('"mainWindow" is not defined');
Expand Down
89 changes: 89 additions & 0 deletions app/system-context-menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const Registry = require('winreg');

const appPath = `"${process.execPath}"`;
const regKey = `\\Software\\Classes\\Directory\\background\\shell\\Workbench`;
const regParts = [
{key: 'command', name: '', value: `${appPath} "%V"`},
{name: '', value: 'Open Workbench here'},
{name: 'Icon', value: `${appPath}`}
];

function addValues(hyperKey, commandKey, callback) {
hyperKey.set(regParts[1].name, Registry.REG_SZ, regParts[1].value, error => {
if (error) {
//eslint-disable-next-line no-console
console.error(error.message);
}
hyperKey.set(regParts[2].name, Registry.REG_SZ, regParts[2].value, err => {
if (err) {
//eslint-disable-next-line no-console
console.error(err.message);
}
commandKey.set(regParts[0].name, Registry.REG_SZ, regParts[0].value, err_ => {
if (err_) {
//eslint-disable-next-line no-console
console.error(err_.message);
}
callback();
});
});
});
}

exports.add = callback => {
const hyperKey = new Registry({hive: 'HKCU', key: regKey});
const commandKey = new Registry({
hive: 'HKCU',
key: `${regKey}\\${regParts[0].key}`
});

hyperKey.keyExists((error, exists) => {
if (error) {
//eslint-disable-next-line no-console
console.error(error.message);
}
if (exists) {
commandKey.keyExists((err_, exists_) => {
if (err_) {
//eslint-disable-next-line no-console
console.error(err_.message);
}
if (exists_) {
addValues(hyperKey, commandKey, callback);
} else {
commandKey.create(err => {
if (err) {
//eslint-disable-next-line no-console
console.error(err.message);
}
addValues(hyperKey, commandKey, callback);
});
}
});
} else {
hyperKey.create(err => {
if (err) {
//eslint-disable-next-line no-console
console.error(err.message);
}
commandKey.create(err_ => {
if (err_) {
//eslint-disable-next-line no-console
console.error(err_.message);
}
addValues(hyperKey, commandKey, callback);
});
});
}
});
};

exports.remove = callback => {
new Registry({hive: 'HKCU', key: regKey}).destroy(err => {
if (err) {
//eslint-disable-next-line no-console
console.error(err.message);
}
callback();
});
};
26 changes: 24 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4978,6 +4978,18 @@ electron-rebuild@^1.8.2:
spawn-rx "^2.0.10"
yargs "^7.0.2"

electron-remote@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/electron-remote/-/electron-remote-1.3.0.tgz#5c4bd278bd86d8aca0a9215c8d31440f1a0f133a"
integrity sha512-i00MD42fzlmyhsYRUDrMM104OQTT/soEmBmZ707CZ3k/nwa0rrB3a3mpxvR0EI2Q+Xw2VBdhWbk2gYmyg0PS0g==
dependencies:
debug "^2.5.1"
hashids "^1.1.1"
lodash.get "^4.4.2"
pify "^2.3.0"
rxjs "^5.0.0-beta.12"
xmlhttprequest "^1.8.0"

electron-settings@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/electron-settings/-/electron-settings-3.2.0.tgz#01461e153f95b6f18adbe0c360c70898eb0f43c3"
Expand Down Expand Up @@ -6783,6 +6795,11 @@ hash.js@^1.0.0, hash.js@^1.0.3:
inherits "^2.0.3"
minimalistic-assert "^1.0.1"

hashids@^1.1.1:
version "1.2.2"
resolved "https://registry.yarnpkg.com/hashids/-/hashids-1.2.2.tgz#28635c7f2f7360ba463686078eee837479e8eafb"
integrity sha512-dEHCG2LraR6PNvSGxosZHIRgxF5sNLOIBFEHbj8lfP9WWmu/PWPMzsip1drdVSOFi51N2pU7gZavrgn7sbGFuw==

hex-color-regex@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/hex-color-regex/-/hex-color-regex-1.1.0.tgz#4c06fccb4602fe2602b3c93df82d7e7dbf1a8a8e"
Expand Down Expand Up @@ -10762,7 +10779,7 @@ performance-now@^2.1.0:
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=

pify@^2.0.0:
pify@^2.0.0, pify@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c"
integrity sha1-7RQaasBDqEnqWISY59yosVMw6Qw=
Expand Down Expand Up @@ -12604,7 +12621,7 @@ rx@2.3.24:
resolved "https://registry.yarnpkg.com/rx/-/rx-2.3.24.tgz#14f950a4217d7e35daa71bbcbe58eff68ea4b2b7"
integrity sha1-FPlQpCF9fjXapxu8vljv9o6ksrc=

rxjs@^5.1.1:
rxjs@^5.0.0-beta.12, rxjs@^5.1.1:
version "5.5.12"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-5.5.12.tgz#6fa61b8a77c3d793dbaf270bee2f43f652d741cc"
integrity sha512-xx2itnL5sBbqeeiVgNPVuQQ1nC8Jp2WfNJhXWHmElW9YmrpS9UVnNzhP3EH3HFqexO5Tlp8GhYY+WEcqcVMvGw==
Expand Down Expand Up @@ -15096,6 +15113,11 @@ xmldom@0.1.x:
resolved "https://registry.yarnpkg.com/xmldom/-/xmldom-0.1.27.tgz#d501f97b3bdb403af8ef9ecc20573187aadac0e9"
integrity sha1-1QH5ezvbQDr4757MIFcxh6rawOk=

xmlhttprequest@^1.8.0:
version "1.8.0"
resolved "https://registry.yarnpkg.com/xmlhttprequest/-/xmlhttprequest-1.8.0.tgz#67fe075c5c24fef39f9d65f5f7b7fe75171968fc"
integrity sha1-Z/4HXFwk/vOfnWX197f+dRcZaPw=

xregexp@4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/xregexp/-/xregexp-4.0.0.tgz#e698189de49dd2a18cc5687b05e17c8e43943020"
Expand Down

0 comments on commit 3879d76

Please sign in to comment.