hyperflow
Version:
A javascript state flow and mutation management toolkit & library for developing universal app.
98 lines (87 loc) • 3.26 kB
JavaScript
/**
* Copyright 2015-present Tuan Le.
*
* Licensed under the MIT License.
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://opensource.org/licenses/mit-license.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*------------------------------------------------------------------------
*
* @module ReactAppComponentComposite
* @description - A React app component composite for native environment.
*
* @author Tuan Le (tuan.t.lei@gmail.com)
*
* @flow
*/
; // eslint-disable-line
import CommonElement from '../../../core/elements/common-element';
import CompositeElement from '../../../core/elements/composite-element';
const Hf = CommonElement();
export default CompositeElement({
template: {
/**
* @description - Initialized and check that factory is valid for this composite.
*
* @method $initReactAppComponentComposite
* @return void
*/
$initReactAppComponentComposite () {
const app = this;
if (Hf.DEVELOPMENT) {
if (!Hf.isSchema({
name: `string`,
getTopDomain: `function`
}).of(app)) {
Hf.log(`error`, `ReactAppComponentComposite.$init - App is invalid. Cannot apply composite.`);
}
}
},
/**
* @description - Get the composed app top interface component from domain interface.
*
* @method getTopComponent
* @param {object} option
* @return {object|function}
*/
getTopComponent (option = {}) {
const app = this;
const domain = app.getTopDomain();
option = Hf.isObject(option) ? option : {};
if (Hf.DEVELOPMENT) {
if (!Hf.isSchema({
name: `string`,
getInterface: `function`
}).of(domain)) {
Hf.log(`error`, `ReactAppComponentComposite.getTopComponent - App:${app.name} domain is invalid.`);
}
}
const intf = domain.getInterface();
if (Hf.DEVELOPMENT) {
if (!Hf.isSchema({
toComponent: `function`
}).of(intf)) {
Hf.log(`error`, `ReactAppComponentComposite.getTopComponent - App:${app.name} top domain:${domain.name} interface is invalid.`);
}
}
const TopComponent = intf.toComponent({
...option,
alwaysUpdateAsParent: true
});
if (Hf.DEVELOPMENT) {
if (!Hf.isFunction(TopComponent)) {
Hf.log(`error`, `ReactAppComponentComposite.getTopComponent - Unable to initialize a React app top component.`);
}
}
return TopComponent;
}
}
});