UNPKG

@ima/core

Version:

IMA.js framework for isomorphic javascript application

173 lines (172 loc) 5.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: Object.getOwnPropertyDescriptor(all, name).get }); } _export(exports, { get _getRoot () { return _getRoot; }, get _isClient () { return _isClient; }, get bootClientApp () { return bootClientApp; }, get createImaApp () { return createImaApp; }, get getClientBootConfig () { return getClientBootConfig; }, get getInitialImaConfigFunctions () { return getInitialImaConfigFunctions; }, get getInitialPluginConfig () { return getInitialPluginConfig; }, get onLoad () { return onLoad; }, get reviveClientApp () { return reviveClientApp; }, get routeClientApp () { 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, $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, $Root: $IMA.$Root, $LanguagePartPath: $IMA.$LanguagePartPath } }; return { ...bootConfig, ...initialAppConfigFunctions, ...getInitialPluginConfig(), ...getInitialImaConfigFunctions() }; } async function bootClientApp(app, bootConfig) { await app.bootstrap.run(bootConfig); const cache = app.oc.get('$Cache'); cache.deserialize($IMA.Cache || {}); return app; } function routeClientApp(app, routerRoot) { const router = app.oc.get('$Router'); return router.listen(routerRoot).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, routerRoot) { 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 = await bootClientApp(app, bootConfig); await (0, _task.autoYield)(); return routeClientApp(app, routerRoot).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