From c4130b9ef679a2abb796f20192d519587203e049 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 20 Mar 2017 14:55:35 +0000 Subject: [PATCH] fix: offer an init event to monitor when repo is there and avoid setTimeout --- gulpfile.js | 4 ++-- src/core/components/init.js | 10 +++++++++- test/core/create-node.spec.js | 8 +++----- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 6789290edd..a1a9367b76 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -42,11 +42,11 @@ function spawnDaemon (num, callback) { } }) - setTimeout(() => { + node.on('init', () => { const daemon = new HTTPAPI(node.repo.path()) nodes.push(daemon) setTimeout(() => daemon.start(callback), 400) - }, 800) + }) } gulp.task('libnode:start', (done) => { diff --git a/src/core/components/init.js b/src/core/components/init.js index 59d517544a..5fcfcca428 100644 --- a/src/core/components/init.js +++ b/src/core/components/init.js @@ -70,6 +70,14 @@ module.exports = function init (self) { cb(null, true) }) } - ], callback) + ], (err, success) => { + if (err) { + return callback(err) + } + + self.emit('init') + + callback(null, success) + }) } } diff --git a/test/core/create-node.spec.js b/test/core/create-node.spec.js index cda6f07d92..891eb7381d 100644 --- a/test/core/create-node.spec.js +++ b/test/core/create-node.spec.js @@ -135,11 +135,11 @@ describe('create node', () => { start: false }) - setTimeout(() => { + node.on('init', () => { node.on('stop', () => done()) node.on('start', () => node.stop()) node.start() - }, 1000) + }) }) it('init: true, start: false, use callback', (done) => { @@ -154,9 +154,7 @@ describe('create node', () => { start: false }) - setTimeout(() => { - node.start(() => node.stop(done)) - }, 1000) + node.on('init', () => node.start(() => node.stop(done))) }) it('overload config', (done) => {