Merge pull request #42 from dciccale/master

Use ES5 .bind() and ES6 arrow functions
This commit is contained in:
Loïc Saint-Roch 2015-12-02 16:31:38 +01:00
commit ada453aa3e
6 changed files with 34 additions and 71 deletions

View File

@ -10,7 +10,6 @@ const events = require('events');
const util = require('util'); const util = require('util');
// Public node modules. // Public node modules.
const _ = require('lodash');
const winston = require('winston'); const winston = require('winston');
// Local dependencies. // Local dependencies.
@ -37,17 +36,6 @@ function Strapi() {
// Mixin support for `Strapi.prototype.after()`. // Mixin support for `Strapi.prototype.after()`.
mixinAfter(this); mixinAfter(this);
// Bind `this` context for all `Strapi.prototype.*` methods.
this.load = _.bind(this.load, this);
this.start = _.bind(this.start, this);
this.stop = _.bind(this.stop, this);
this.restart = _.bind(this.restart, this);
this.initialize = _.bind(this.initialize, this);
this.exposeGlobals = _.bind(this.exposeGlobals, this);
this.runBootstrap = _.bind(this.runBootstrap, this);
this.isLocalStrapiValid = _.bind(this.isLocalStrapiValid, this);
this.isStrapiAppSync = _.bind(this.isStrapiAppSync, this);
// Expose `koa`. // Expose `koa`.
this.server = require('koa'); this.server = require('koa');
this.app = require('koa')(); this.app = require('koa')();

View File

@ -17,7 +17,7 @@ const __initializeHooks = require('./private/loadHooks');
* Load the Strapi instance * Load the Strapi instance
*/ */
module.exports = function (strapi) { module.exports = strapi => {
const Configuration = __Configuration(strapi); const Configuration = __Configuration(strapi);
const initializeHooks = __initializeHooks(strapi); const initializeHooks = __initializeHooks(strapi);
@ -80,13 +80,9 @@ module.exports = function (strapi) {
} }
async.series([ async.series([
function (cb) { cb => loadHookDefinitions(strapi.hooks, cb),
loadHookDefinitions(strapi.hooks, cb); cb => initializeHooks(strapi.hooks, cb)
}, ], err => {
function (cb) {
initializeHooks(strapi.hooks, cb);
}
], function (err) {
if (err) { if (err) {
return cb(err); return cb(err);
} }

View File

@ -16,81 +16,73 @@ const async = require('async');
* (useful for the Studio) * (useful for the Studio)
*/ */
module.exports = function (cb) { module.exports = cb => {
const self = this;
console.log(); console.log();
// Update the Strapi status (might be used // Update the Strapi status (might be used
// by the core or some hooks). // by the core or some hooks).
self.reloading = true; this.reloading = true;
// Async module loader to rebuild a // Async module loader to rebuild a
// dictionary of the application. // dictionary of the application.
async.auto({ async.auto({
// Rebuild the dictionaries. // Rebuild the dictionaries.
dictionaries: function (cb) { dictionaries: cb => {
self.on('hook:_config:reloaded', function () { this.on('hook:_config:reloaded', () => {
self.on('hook:_api:reloaded', function () { this.on('hook:_api:reloaded', () => cb());
cb(); this.hooks._api.reload();
}); });
self.hooks._api.reload(); this.hooks._config.reload();
});
self.hooks._config.reload();
} }
}, },
// Callback. // Callback.
function (err) { err => {
// Just in case there is an error. // Just in case there is an error.
if (err) { if (err) {
self.log.error('Impossible to reload the server'); this.log.error('Impossible to reload the server');
self.log.error('Please restart the server manually'); this.log.error('Please restart the server manually');
self.stop(); this.stop();
} }
// Tell the application the framework is reloading // Tell the application the framework is reloading
// (might be used by some hooks). // (might be used by some hooks).
self.reloading = true; this.reloading = true;
// Teardown Waterline adapters and // Teardown Waterline adapters and
// reload the Waterline ORM. // reload the Waterline ORM.
self.after('hook:waterline:reloaded', function () { this.after('hook:waterline:reloaded', () => {
self.after('hook:router:reloaded', function () { this.after('hook:router:reloaded', () => {
process.nextTick(function () { process.nextTick(() => cb());
cb();
});
// Update `strapi` status. // Update `strapi` status.
self.reloaded = true; this.reloaded = true;
self.reloading = false; this.reloading = false;
// Finally inform the developer everything seems ok. // Finally inform the developer everything seems ok.
if (cluster.isMaster && _.isPlainObject(strapi.config.reload) && !_.isEmpty(strapi.config.reload) && strapi.config.reload.workers < 1) { if (cluster.isMaster && _.isPlainObject(strapi.config.reload) && !_.isEmpty(strapi.config.reload) && strapi.config.reload.workers < 1) {
self.log.info('Application\'s dictionnary updated'); this.log.info('Application\'s dictionnary updated');
self.log.warn('You still need to restart your server to fully enjoy changes...'); this.log.warn('You still need to restart your server to fully enjoy changes...');
} }
// Kill every worker processes. // Kill every worker processes.
_.forEach(cluster.workers, function () { _.forEach(cluster.workers, () => process.kill(process.pid, 'SIGHUP'));
process.kill(process.pid, 'SIGHUP');
});
if (cluster.isMaster && _.isPlainObject(strapi.config.reload) && !_.isEmpty(strapi.config.reload) && strapi.config.reload.workers > 0) { if (cluster.isMaster && _.isPlainObject(strapi.config.reload) && !_.isEmpty(strapi.config.reload) && strapi.config.reload.workers > 0) {
self.log.info('Application restarted'); this.log.info('Application restarted');
console.log(); console.log();
} }
}); });
// Reloading the router. // Reloading the router.
self.hooks.router.reload(); this.hooks.router.reload();
}); });
// Reloading the ORM. // Reloading the ORM.
self.hooks.waterline.reload(); this.hooks.waterline.reload();
}); });
}; };

View File

@ -4,9 +4,6 @@
* Module dependencies * Module dependencies
*/ */
// Public node modules.
const _ = require('lodash');
// Local dependencies. // Local dependencies.
const Strapi = require('./Strapi'); const Strapi = require('./Strapi');
@ -15,13 +12,6 @@ const Strapi = require('./Strapi');
* (maintains backwards compatibility with constructor usage). * (maintains backwards compatibility with constructor usage).
*/ */
module.exports = strapiFactory; module.exports = function () {
function strapiFactory() {
return new Strapi(); return new Strapi();
} };
// Backwards compatibility for Strapi singleton usage.
const singleton = strapiFactory();
strapiFactory.isLocalStrapiValid = _.bind(singleton.isLocalStrapiValid, singleton);
strapiFactory.isStrapiAppSync = _.bind(singleton.isStrapiAppSync, singleton);

View File

@ -29,10 +29,8 @@ module.exports = function start(configOverride, cb) {
}; };
async.series([ async.series([
function (cb) { cb => self.load(configOverride, cb),
self.load(configOverride, cb); this.initialize
},
self.initialize
], ],
function strapiReady(err) { function strapiReady(err) {

View File

@ -12,15 +12,14 @@
*/ */
module.exports = function stop() { module.exports = function stop() {
const self = this;
// Flag `self._exiting` as soon as the application has begun to shutdown. // Flag `this._exiting` as soon as the application has begun to shutdown.
// This may be used by hooks and other parts of core. // This may be used by hooks and other parts of core.
self._exiting = true; this._exiting = true;
// Exit the REPL. // Exit the REPL.
process.exit(0); process.exit(0);
// Emit a `stop` event. // Emit a `stop` event.
self.emit('stop'); this.emit('stop');
}; };