Skip to content

Commit

Permalink
Consolidate connect logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mantoni committed Aug 21, 2018
1 parent 6159e90 commit 4c70735
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 37 deletions.
19 changes: 1 addition & 18 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
'use strict';

const net = require('net');
const out = require('./out');
const portfile = require('./portfile');

function connect(callback) {
portfile.read((config) => {
if (!config) {
callback('Not running');
return;
}
const socket = net.connect(config.port, '127.0.0.1', () => {
callback(null, socket, config.token);
});
socket.once('error', () => {
process.exitCode = 1;
callback('Could not connect');
});
});
}
const connect = require('./connect');

exports.stop = function (callback) {
connect((err, socket, token) => {
Expand Down
20 changes: 20 additions & 0 deletions lib/connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const net = require('net');
const portfile = require('./portfile');

module.exports = function (callback) {
portfile.read((config) => {
if (!config) {
callback('Not running');
return;
}
const socket = net.connect(config.port, '127.0.0.1', () => {
callback(null, socket, config.token);
});
socket.once('error', () => {
process.exitCode = 1;
callback('Could not connect');
});
});
};
22 changes: 3 additions & 19 deletions lib/launcher.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
'use strict';

const net = require('net');
const out = require('./out');
const portfile = require('./portfile');

function check(callback) {
portfile.read((config) => {
if (!config) {
callback('Not running');
return;
}
const socket = net.connect(config.port, '127.0.0.1', () => {
callback(null, socket, config.token);
});
socket.once('error', () => {
callback('Could not connect');
});
});
}
const connect = require('./connect');

function wait(callback) {
check((err, socket, token) => {
connect((err, socket, token) => {
if (err) {
setTimeout(() => {
wait(callback);
Expand Down Expand Up @@ -54,7 +38,7 @@ function launch(callback) {
}

exports.launch = function (callback) {
check((err, socket) => {
connect((err, socket) => {
if (err) {
launch(callback);
} else {
Expand Down

0 comments on commit 4c70735

Please sign in to comment.