@ima/core
Version:
IMA.js framework for isomorphic javascript application
175 lines (174 loc) • 5.38 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
_getRoot: function() {
return _getRoot;
},
_isClient: function() {
return _isClient;
},
bootClientApp: function() {
return bootClientApp;
},
createImaApp: function() {
return createImaApp;
},
getClientBootConfig: function() {
return getClientBootConfig;
},
getInitialImaConfigFunctions: function() {
return getInitialImaConfigFunctions;
},
getInitialPluginConfig: function() {
return getInitialPluginConfig;
},
onLoad: function() {
return onLoad;
},
reviveClientApp: function() {
return reviveClientApp;
},
routeClientApp: function() {
return routeClientApp;
}
});
const _task = require("@esmj/task");
const _Bootstrap = require("./Bootstrap");
const _bind = require("./config/bind");
const _services = require("./config/services");
const _GenericError = require("./error/GenericError");
const _Namespace = require("./Namespace");
const _ObjectContainer = require("./oc/ObjectContainer");
const _pluginLoader = require("./pluginLoader");
function getInitialImaConfigFunctions() {
return {
initBindIma: _bind.initBind,
initServicesIma: _services.initServices
};
}
function getInitialPluginConfig() {
return {
plugins: _pluginLoader.pluginLoader.getPlugins()
};
}
function _getRoot() {
return _isClient() ? window : global;
}
function _isClient() {
return typeof window !== 'undefined' && window !== null;
}
function createImaApp() {
const oc = new _ObjectContainer.ObjectContainer(_Namespace.ns);
const bootstrap = new _Bootstrap.Bootstrap(oc);
_pluginLoader.pluginLoader.init(bootstrap);
return {
oc,
bootstrap
};
}
function getClientBootConfig(initialAppConfigFunctions) {
const root = _getRoot();
if ($Debug && _isClient() && !$IMA?.Test) {
if ($IMA.$Protocol !== root.location.protocol) {
throw new _GenericError.GenericError(`Your client's protocol is not same as server's protocol. ` + `For right setting protocol on the server site set ` + `'X-Forwarded-Proto' header.`);
}
if ($IMA.$Host !== root.location.host) {
throw new _GenericError.GenericError(`Your client's host is not same as server's host. For right ` + `setting host on the server site set 'X-Forwarded-Host' ` + `header.`);
}
}
const bootConfig = {
services: {
response: null,
request: null,
$IMA: $IMA,
dictionary: {
$Language: $IMA.$Language,
dictionary: $IMA.i18n
},
router: {
$Protocol: $IMA.$Protocol,
$Host: $IMA.$Host,
$Path: $IMA.$Path,
$Root: $IMA.$Root,
$LanguagePartPath: $IMA.$LanguagePartPath
}
},
settings: {
$Debug: $IMA.$Debug,
$Env: $IMA.$Env,
$Version: $IMA.$Version,
$App: $IMA.$App,
// @ts-expect-error This is intentional for integration testing.
$Resources: $IMA.$Resources,
$Protocol: $IMA.$Protocol,
$Language: $IMA.$Language,
$Host: $IMA.$Host,
$Path: $IMA.$Path,
$Root: $IMA.$Root,
$LanguagePartPath: $IMA.$LanguagePartPath
}
};
return {
...bootConfig,
...initialAppConfigFunctions,
...getInitialPluginConfig(),
...getInitialImaConfigFunctions()
};
}
function bootClientApp(app, bootConfig) {
app.bootstrap.run(bootConfig);
const cache = app.oc.get('$Cache');
cache.deserialize($IMA.Cache || {});
return app;
}
function routeClientApp(app) {
const router = app.oc.get('$Router');
return router.listen().route(router.getPath()).catch((error)=>{
if (typeof $IMA.fatalErrorHandler === 'function') {
$IMA.fatalErrorHandler(error);
} else {
console.warn('Define function config.$IMA.fatalErrorHandler in ' + 'services.js.');
}
});
}
async function reviveClientApp(initialAppConfigFunctions) {
await (0, _task.autoYield)();
const root = _getRoot();
root.$Debug = !!root.$IMA.$Debug;
let app = createImaApp();
await (0, _task.autoYield)();
const bootConfig = getClientBootConfig(initialAppConfigFunctions);
await (0, _task.autoYield)();
app = bootClientApp(app, bootConfig);
await (0, _task.autoYield)();
return routeClientApp(app).then((pageInfo)=>{
return Object.assign({}, pageInfo || {}, {
app,
bootConfig
});
});
}
function onLoad() {
if (!_isClient()) {
return Promise.reject(null);
}
if (document.readyState !== 'loading') {
return (0, _task.nextFrameYield)();
}
return new Promise((resolve)=>{
document.addEventListener('DOMContentLoaded', ()=>{
return (0, _task.nextFrameYield)().then(resolve);
}, {
once: true
});
});
}
//# sourceMappingURL=boot.js.map