Skip to content

Commit

Permalink
bug: plugin components aren't loaded before rest
Browse files Browse the repository at this point in the history
  • Loading branch information
ilfroloff committed Oct 3, 2017
1 parent 931d27d commit 5a839bd
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 37 deletions.
45 changes: 28 additions & 17 deletions core/Application.js
Expand Up @@ -53,7 +53,6 @@ function Application(options) {
IFNodeVirtualSchema
];
this._models_builder = null;
this._components_builder = null;
this._controllers_builder = null;

this.id = UUID.v4();
Expand All @@ -63,7 +62,7 @@ function Application(options) {

this.config = this._initialize_config(options.env || options.environment);
deepFreeze(this.config);

this.listener = this._initialize_listener();
this.connection = this._initialize_connection_server();

Expand All @@ -75,6 +74,7 @@ function Application(options) {

this.models = {};
this.components = {};
this._components_builder = new ComponentsBuilder(this.components, this.config.components);
this.controllers = {};
}

Expand Down Expand Up @@ -133,7 +133,7 @@ Application.prototype.load = function() {
this.models = this._initialize_models();
Object.freeze(this.models);

this.components = this._initialize_components();
this._initialize_components();
// Object.freeze(this.components);

this.controllers = this._initialize_controllers();
Expand All @@ -151,7 +151,7 @@ Application.prototype.load = function() {
*/
Application.prototype.extension = function(id) {
var cache = this._extensions_cache;

if(!(id in cache)) {
var custom_folder = this.config.application.folders.extensions;
var custom_full_path = Path.resolve(this._project_folder, custom_folder);
Expand Down Expand Up @@ -184,14 +184,21 @@ Application.prototype.component = function(id) {
}

var full_path = Path.resolve(this.config.application.folders.components, id);
var components_configs = this.config.components;
var components_builder = this._components_builder;
var component = components_builder.read_and_build_component(full_path, {
name: id,
config: (components_configs && components_configs[id]) || {}
});

if(!(full_path in this.components)) {
this.components[full_path] = this._components_builder.read_and_build_component(full_path, {
name: id
});
components_builder.compile(this, full_path);

if (!this.components[id]) {
components_builder.save_component(component, id);
components_builder.components_compiled[id] = true;
}

return this.components[full_path];
return component;
};

/**
Expand All @@ -214,7 +221,7 @@ Application.prototype.Component = function(custom_component_config) {
var builder = this._components_builder;

return builder.make(
builder.build_component_config(custom_component_config, this.config.components)
builder.build_component_config(custom_component_config)
);
};

Expand Down Expand Up @@ -374,20 +381,25 @@ Application.prototype._initialize_models = function _initialize_models() {
* @private
*/
Application.prototype._initialize_components = function _initialize_components() {
var components_builder = this._components_builder = new ComponentsBuilder;
var self = this;
var components_builder = this._components_builder;
var Component = this.Component.bind(this);
var modules = this._modules;

for(var i = 0; i < modules.length; ++i) {
var module = modules[i][PLUGIN_TYPES.COMPONENT];

if(module) {
module(this, Component);
var component = module(this, Component);

if(component) {
components_builder.build_component(component, {});
}

components_builder.compile(this);
}
}

var components_config = this.config.components;

Diread({
src: this.config.application.folders.components,
directories: true,
Expand All @@ -402,8 +414,9 @@ Application.prototype._initialize_components = function _initialize_components()
try {
components_builder.read_and_build_component(
component_path,
components_builder.build_component_config({}, components_config)
components_builder.build_component_config()
);
components_builder.compile(self, component_path);
} catch(error) {
/**
* Errors inside component will not catch by this handle
Expand All @@ -418,8 +431,6 @@ Application.prototype._initialize_components = function _initialize_components()
}
}
});

return components_builder.compile(this);
};

/**
Expand Down
58 changes: 43 additions & 15 deletions core/application/ComponentsBuilder.js
Expand Up @@ -11,25 +11,30 @@ var Component = require('./../Component');
/**
*
* @class ComponentsBuilder
*
* @param {Object} components
* @param {Object} [components_configs]
*/
function ComponentsBuilder() {
function ComponentsBuilder(components, components_configs) {
/**
*
* @type {Object.<string, Component>}
*/
this.components = {};
this.components = components;
this.components_compiled = {};

this._components_configs = components_configs || {};
this._autoformed_config = null;
}

/**
*
* @param {Object} custom_config
* @param {Object} [components_configs]
* @param {Object} [custom_config]
* @returns {Object}
*/
ComponentsBuilder.prototype.build_component_config = function build_component_config(custom_config, components_configs) {
ComponentsBuilder.prototype.build_component_config = function build_component_config(custom_config) {
custom_config = _defaults(custom_config || {}, this._autoformed_config);
custom_config.config = (components_configs && components_configs[custom_config.name]) || {};
custom_config.config = (this._components_configs[custom_config.name]) || {};

return custom_config;
};
Expand All @@ -49,14 +54,22 @@ ComponentsBuilder.prototype.build_and_memorize_config = function build_and_memor
return this._autoformed_config;
};


/**
*
* @param {string} component_path
* @param {Object} component_config
*/
ComponentsBuilder.prototype.read_and_build_component = function read_and_build_component(component_path, component_config) {
var component = require(component_path);
return this.build_component(require(component_path), component_config);
};

/**
*
* @param {*} component
* @param {Object} component_config
*/
ComponentsBuilder.prototype.build_component = function build_component(component, component_config) {
if(typeof component === 'function' && isInheritsFrom(component, Component)) {
var component_name = component_config.name;
var saved_component = this.components[component_name];
Expand All @@ -69,11 +82,13 @@ ComponentsBuilder.prototype.read_and_build_component = function read_and_build_c
return saved_component;
}

component = new component(component_config);
component_config.name = component_config.name || component.name;

component = new component(_defaults(component_config, this._components_configs[component_config.name]));
}

return component instanceof Component ?
this._save_component(component, component.name) :
this.save_component(component, component.name) :
component;
};

Expand All @@ -83,52 +98,65 @@ ComponentsBuilder.prototype.read_and_build_component = function read_and_build_c
* @returns {Component}
*/
ComponentsBuilder.prototype.make = function make(component_config) {
return this._save_component(new Component(component_config), component_config.name);
return this.save_component(new Component(component_config), component_config.name);
};

/**
*
* @param {Application} app
* @param {string} [component_path]
* @returns {Object.<string, Component>}
*/
ComponentsBuilder.prototype.compile = function compile(app) {
ComponentsBuilder.prototype.compile = function compile(app, component_path) {
var self = this;
var components = this.components;
var components_compiled = this.components_compiled;

Object.keys(components).forEach(function(unique_name) {
if (components_compiled[unique_name]) {
return;
}

if(unique_name in app) {
Log.error('application', 'Alias [' + unique_name + '] already busy in application instance.');
}

var component = components[unique_name];
app[unique_name] = component;

if(component.initialize) {
component.initialize(component.config);
}

components_compiled[unique_name] = true;
app[unique_name] = component;

component.alias.forEach(function(alias) {
self._save_component(component, alias);
self.save_component(component, alias);
components_compiled[alias] = true;

if(alias in app) {
Log.error('application', 'Alias [' + alias + '] already busy in application instance.');
}

app[alias] = component;
});

if(component_path) {
self.save_component(component, component_path);
components_compiled[component_path] = true;
}
});

return components;
};

/**
*
* @private
* @param {Component} component
* @param {string} key
* @returns {Component}
*/
ComponentsBuilder.prototype._save_component = function(component, key) {
ComponentsBuilder.prototype.save_component = function(component, key) {
var saved_component = this.components[key];

if(!saved_component) {
Expand Down
28 changes: 28 additions & 0 deletions examples/plugins/protected/components/ApplicationComponent.js
@@ -0,0 +1,28 @@
'use strict';

var Util = require('util');
var Component = require('./../../../../core/Component');
/**
*
* @type {Application}
*/
var app = require('./../../../..')('plugins');
var PluginComponent = app.component('PluginComponent');

/**
*
* @class
* @extends Component
*
* @param options
*/
function ApplicationComponent(options) {
Component.call(this, options);
}
Util.inherits(ApplicationComponent, Component);

ApplicationComponent.prototype.get_plugin_component = function() {
return PluginComponent;
};

module.exports = ApplicationComponent;
@@ -0,0 +1,23 @@
var _defaults = require('lodash/defaults');
var Util = require('util');
var Component = require('../../../../../core/Component');

module.exports = {
component: function() {
/**
*
* @class
* @extends Component
*
* @param {Object} options
*/
function PluginClassComponent(options) {
Component.call(this, _defaults(options, {
alias: 'plugin_class_component'
}));
}
Util.inherits(PluginClassComponent, Component);

return PluginClassComponent;
}
};
@@ -1,4 +1,8 @@
module.exports = {
component: function(app, Component) {
Component({
name: 'PluginComponent',
alias: 'plugin_component'
});
}
};
9 changes: 6 additions & 3 deletions package.json
Expand Up @@ -19,9 +19,9 @@
"author": "Ilya Frolov <ilfroloff@gmail.com>",
"license": "MIT",
"dependencies": {
"debug": "3.0.0",
"debug": "3.1.0",
"diread": "0.2.0",
"express": "4.15.4",
"express": "4.16.1",
"lodash": "4.17.4",
"uuid": "3.1.0"
},
Expand All @@ -35,6 +35,9 @@
"serve-static": "1.12.4",
"setprototypeof": "1.0.3",
"should": "11.2.1",
"supertest": "3.0.0"
"supertest": "2.0.1"
},
"engines": {
"node": ">=0.10.0"
}
}

0 comments on commit 5a839bd

Please sign in to comment.