From 1f7d8e56e8c2c7a09838e78000c2c11d3c9e5370 Mon Sep 17 00:00:00 2001 From: Simon Ho Date: Wed, 3 Dec 2014 00:31:40 -0800 Subject: [PATCH] Add "booting" flag and emit "booted" event --- lib/executor.js | 14 +++++++++++++- test/executor.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/executor.js b/lib/executor.js index af67b4b..ff3e60a 100644 --- a/lib/executor.js +++ b/lib/executor.js @@ -16,6 +16,10 @@ var path = require('path'); */ module.exports = function execute(app, instructions, callback) { + callback = callback || function() {}; + + app.booting = true; + patchAppLoopback(app); assertLoopBackVersion(app); @@ -37,7 +41,15 @@ module.exports = function execute(app, instructions, callback) { function(done) { enableAnonymousSwagger(app, instructions); done(); - }], callback); + }], function(err) { + app.booting = false; + + if (err) return callback(err); + + app.emit('booted'); + + callback(); + }); }; function patchAppLoopback(app) { diff --git a/test/executor.test.js b/test/executor.test.js index c6fd891..b6b20fa 100644 --- a/test/executor.test.js +++ b/test/executor.test.js @@ -52,6 +52,32 @@ describe('executor', function() { } }); + describe('when booting', function() { + it('should set the booting status', function(done) { + expect(app.booting).to.be.undefined(); + boot.execute(app, dummyInstructions, function(err) { + expect(err).to.be.undefined(); + expect(app.booting).to.be.false(); + done(); + }); + }); + + it('should emit the `booted` event', function(done) { + app.on('booted', function() { + // This test fails with a timeout when the `booted` event has not been + // emitted correctly + done(); + }); + boot.execute(app, dummyInstructions, function(err) { + expect(err).to.be.undefined(); + }); + }); + + it('should work when called synchronously', function() { + boot.execute(app, dummyInstructions); + }); + }); + it('configures models', function() { boot.execute(app, dummyInstructions); assert(app.models);