Skip to content

Commit

Permalink
library versions, error handling, copy tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
krl committed Jun 24, 2015
1 parent afc5e41 commit bc6feda
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 75,136 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
debug.log
pkg
node_modules/
build
3 changes: 3 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mkdir build 2> /dev/null

This comment has been minimized.

Copy link
@jbenet

jbenet Jun 24, 2015

Member

mkdir -p build is nice because it doesn't return an error code if the dir exists which may be relevant if set -e is later added to the script (no need to change here, just a tip)

browserify js/menu.jsx > build/menu.js
browserify js/initialize.jsx > build/initialize.js
2 changes: 1 addition & 1 deletion html/initialize.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset='utf-8'/>
<link rel='stylesheet' href='../node_modules/bootstrap/dist/css/bootstrap.css'/>
<link rel='stylesheet' href='../css/common.css'/>
<script src="../js/initialize.js"></script>
<script src="../build/initialize.js"></script>
</head>
<body>
<div id='header' class='row'>
Expand Down
2 changes: 1 addition & 1 deletion html/menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.css"/>
<link rel="stylesheet" href="../node_modules/bootstrap-toggle/css/bootstrap2-toggle.css"/>
<link rel="stylesheet" href="../css/menu.css"/>
<script src="../js/menu.js"></script>
<script src="../build/menu.js"></script>

This comment has been minimized.

Copy link
@jbenet

jbenet Jun 24, 2015

Member

maybe build.sh should cp -r html build/html so that this script doesn't have to be relative to "build". in general, keeping build/ dirs totally isolated and files inside it relative to build/ simplifies a lot of things/expectations.

</head>
<body>
<div id="menu-app"/>
Expand Down
130 changes: 73 additions & 57 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,60 +63,6 @@ var initialize = function (path, node) {
})
}

var startTray = function (node) {
var poll
var statsCache = {}

ipc.on('request-state', function (event) {
ipfsd.version(function (err, res) {
if (err) throw err
ipc.emit('version', res)
})

if (node.initialized) {
ipc.emit('node-status', 'stopped')
}
})

ipc.on('start-daemon', function () {
ipc.emit('node-status', 'starting')
node.startDaemon(function (err, ipfs) {
if (err) throw err
ipc.emit('node-status', 'running')
poll = setInterval(function () { pollStats(ipfs) }, 500)

Ipfs = ipfs
})
})

ipc.on('stop-daemon', function () {
ipc.emit('node-status', 'stopping')
if (poll) {
delete statsCache.peers
ipc.emit('stats', statsCache)
clearInterval(poll)
poll = null
}

node.stopDaemon(function (err) {
if (err) throw err
ipc.emit('node-status', 'stopped')
ipc.emit('stats', statsCache)
})
})

ipc.on('open-console', openConsole)
ipc.on('open-browser', openBrowser)

var pollStats = function (ipfs) {
ipc.emit('stats', statsCache)
ipfs.swarm.peers(function (err, res) {
if (err) throw err
statsCache.peers = res.Strings.length
})
}
}

// main entry point
ipfsd.local(function (err, node) {
if (err) error(err)
Expand Down Expand Up @@ -153,6 +99,65 @@ ipfsd.local(function (err, node) {
mb.window.setSize(MENU_WIDTH, height)
})
})

var startTray = function (node) {
var poll
var statsCache = {}

ipc.on('request-state', function (event) {
ipfsd.version(function (err, res) {
if (err) throw err
ipc.emit('version', res)
})

if (node.initialized) {
ipc.emit('node-status', 'stopped')
}
})

ipc.on('start-daemon', function () {
ipc.emit('node-status', 'starting')
node.startDaemon(function (err, ipfs) {
if (err) throw err
ipc.emit('node-status', 'running')

poll = setInterval(function () {
if (mb.window.isVisible()) {
pollStats(ipfs)
}
}, 500)

Ipfs = ipfs
})
})

ipc.on('stop-daemon', function () {
ipc.emit('node-status', 'stopping')
if (poll) {
delete statsCache.peers
ipc.emit('stats', statsCache)
clearInterval(poll)
poll = null
}

node.stopDaemon(function (err) {
if (err) throw err
ipc.emit('node-status', 'stopped')
ipc.emit('stats', statsCache)
})
})

ipc.on('open-console', openConsole)
ipc.on('open-browser', openBrowser)

var pollStats = function (ipfs) {
ipc.emit('stats', statsCache)
ipfs.swarm.peers(function (err, res) {
if (err) throw err
statsCache.peers = res.Strings.length
})
}
}
})

var apiAddrToUrl = function (apiAddr) {
Expand All @@ -161,26 +166,37 @@ var apiAddrToUrl = function (apiAddr) {
return url
}

var openConsole = function () {
var openConsole = function (cb) {
if (Ipfs) {
Ipfs.config.get('Addresses.API', function (err, res) {
if (err && cb) return cb(err)
if (err) throw err

mainWindow = new BrowserWindow({icon: LOGO, width: 800, height: 600})
mainWindow.on('closed', function () {
mainWindow = null
})

mainWindow.loadUrl(apiAddrToUrl(res.Value))
cb && cb()
})
} else {
var err = new Error('Cannot open console, IPFS daemon not running')
if (err && cb) return cb(err)
if (err) throw err
}
}

var openBrowser = function () {
var openBrowser = function (cb) {
if (Ipfs) {
Ipfs.config.get('Addresses.API', function (err, res) {
if (err && cb) return cb(err)
if (err) throw err
open(apiAddrToUrl(res.Value))
cb && cb()
})
} else {
var err = new Error('Cannot open browser, IPFS daemon not running')
if (err && cb) return cb(err)
if (err) throw err
}
}
Loading

0 comments on commit bc6feda

Please sign in to comment.