UNPKG

@ima/core

Version:

IMA.js framework for isomorphic javascript application

133 lines (132 loc) 4.32 kB
import { autoYield, nextFrameYield } from '@esmj/task'; import { Bootstrap } from './Bootstrap'; import { initBind as initBindIma } from './config/bind'; import { initServices as initServicesIma } from './config/services'; import { GenericError } from './error/GenericError'; import { ns } from './Namespace'; import { ObjectContainer } from './oc/ObjectContainer'; import { pluginLoader } from './pluginLoader'; export function getInitialImaConfigFunctions() { return { initBindIma, initServicesIma }; } export function getInitialPluginConfig() { return { plugins: pluginLoader.getPlugins() }; } export function _getRoot() { return _isClient() ? window : global; } export function _isClient() { return typeof window !== 'undefined' && window !== null; } export function createImaApp() { const oc = new ObjectContainer(ns); const bootstrap = new Bootstrap(oc); pluginLoader.init(bootstrap); return { oc, bootstrap }; } export function getClientBootConfig(initialAppConfigFunctions) { const root = _getRoot(); if ($Debug && _isClient() && !$IMA?.Test) { if ($IMA.$Protocol !== root.location.protocol) { throw new 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(`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() }; } export function bootClientApp(app, bootConfig) { app.bootstrap.run(bootConfig); const cache = app.oc.get('$Cache'); cache.deserialize($IMA.Cache || {}); return app; } export 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.'); } }); } export async function reviveClientApp(initialAppConfigFunctions) { await autoYield(); const root = _getRoot(); root.$Debug = !!root.$IMA.$Debug; let app = createImaApp(); await autoYield(); const bootConfig = getClientBootConfig(initialAppConfigFunctions); await autoYield(); app = bootClientApp(app, bootConfig); await autoYield(); return routeClientApp(app).then((pageInfo)=>{ return Object.assign({}, pageInfo || {}, { app, bootConfig }); }); } export function onLoad() { if (!_isClient()) { return Promise.reject(null); } if (document.readyState !== 'loading') { return nextFrameYield(); } return new Promise((resolve)=>{ document.addEventListener('DOMContentLoaded', ()=>{ return nextFrameYield().then(resolve); }, { once: true }); }); } //# sourceMappingURL=boot.js.map