Skip to content

Commit

Permalink
feat: connect already running daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Mar 21, 2018
2 parents 542d2ef + f5b2c44 commit e93875b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"file-extension": "^4.0.2",
"go-ipfs-dep": "^0.4.13",
"ipfs-stats": "^1.2.4",
"ipfsd-ctl": "^0.30.3",
"ipfsd-ctl": "^0.30.4",
"is-ipfs": "^0.3.2",
"lodash.clonedeep": "^4.5.0",
"moment": "^2.21.0",
Expand All @@ -38,7 +38,7 @@
"codecov": "^3.0.0",
"cross-env": "^5.1.4",
"electron-forge": "^5.1.1",
"electron-prebuilt-compile": "1.8.3",
"electron-prebuilt-compile": "1.8.4",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"eslint": "^4",
Expand All @@ -53,7 +53,7 @@
"mocha": "^5.0.4",
"nyc": "^11.6.0",
"pre-commit": "^1.2.2",
"sinon": "^4.4.6",
"sinon": "^4.4.8",
"spectron": "^3.8.0"
},
"scripts": {
Expand Down
44 changes: 20 additions & 24 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ app.makeSingleInstance(() => {

let IPFS
let menubar
let state = 'stopped'

function send (type, ...args) {
if (menubar && menubar.window && menubar.window.webContents) {
Expand All @@ -38,25 +39,18 @@ function send (type, ...args) {
config.send = send
config.ipfs = () => IPFS

function onRequestState (node, event) {
if (!node.started) {
return
}

let status = 'stopped'

node.pid((pid) => {
if (pid) {
status = IPFS ? 'running' : 'starting'
}
function updateState (st) {
state = st
onRequestState()
}

send('node-status', status)
})
function onRequestState (node, event) {
send('node-status', state)
}

function onStartDaemon (node) {
debug('Starting daemon')
send('node-status', 'starting')
updateState('starting')

// Tries to remove the repo.lock file if it already exists.
// This fixes a bug on Windows, where the daemon seems
Expand Down Expand Up @@ -87,21 +81,23 @@ function onStartDaemon (node) {
debug('Daemon started')
config.events.emit('node:started')

// Stop the executation of the program if some error
// occurs on the node.
node.subprocess.on('error', (e) => {
send('node-status', 'stopped')
debug(e)
})
if (node.subprocess) {
// Stop the executation of the program if some error
// occurs on the node.
node.subprocess.on('error', (e) => {
updateState('stopped')
debug(e)
})
}

menubar.tray.setImage(config.logo.ice)
send('node-status', 'running')
updateState('running')
})
}

function onStopDaemon (node, done) {
debug('Stopping daemon')
send('node-status', 'stopping')
updateState('stopping')

config.events.emit('node:stopped')

Expand All @@ -114,7 +110,7 @@ function onStopDaemon (node, done) {
menubar.tray.setImage(config.logo.black)

IPFS = null
send('node-status', 'stopped')
updateState('stopped')
done()
})
}
Expand Down Expand Up @@ -204,7 +200,7 @@ function initialize (path, node) {
config.settingsStore.set('ipfsPath', userPath)

send('initialization-complete')
send('node-status', 'stopped')
updateState('stopped')

onStartDaemon(node)
window.close()
Expand Down

0 comments on commit e93875b

Please sign in to comment.