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');
// Public node modules.
const _ = require('lodash');
const winston = require('winston');
// Local dependencies.
@ -37,17 +36,6 @@ function Strapi() {
// Mixin support for `Strapi.prototype.after()`.
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`.
this.server = require('koa');
this.app = require('koa')();

View File

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

View File

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

View File

@ -4,9 +4,6 @@
* Module dependencies
*/
// Public node modules.
const _ = require('lodash');
// Local dependencies.
const Strapi = require('./Strapi');
@ -15,13 +12,6 @@ const Strapi = require('./Strapi');
* (maintains backwards compatibility with constructor usage).
*/
module.exports = strapiFactory;
function strapiFactory() {
module.exports = function () {
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([
function (cb) {
self.load(configOverride, cb);
},
self.initialize
cb => self.load(configOverride, cb),
this.initialize
],
function strapiReady(err) {

View File

@ -12,15 +12,14 @@
*/
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.
self._exiting = true;
this._exiting = true;
// Exit the REPL.
process.exit(0);
// Emit a `stop` event.
self.emit('stop');
this.emit('stop');
};