-
Notifications
You must be signed in to change notification settings - Fork 11
/
AppGlobal.ts
43 lines (38 loc) · 1.24 KB
/
AppGlobal.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import ServerContext from './ServerSideRendering/ServerContext';
import { LoadedModuleList } from './Loaders/ComponentLoader';
import IEpiserverContext from './Core/IEpiserverContext';
import IServiceContainer from './Core/IServiceContainer';
declare let global: any;
declare let window: any;
const fallback : any = {};
/**
* The global variable scope, as defined by the Episerver SPA
*/
export type GlobalContext = {
__INITIAL__DATA__ ?: ServerContext
EpiserverSpa ?: {
Context: IEpiserverContext
serviceContainer: IServiceContainer
}
PreLoad ?: LoadedModuleList
addEventListener ?: (event: string, handler: any, context: boolean) => void
epi ?: {
isServerSideRendering ?: boolean
}
}
/**
* Get the global variable for the current environment, this method will
* return:
* - When running in NodeJS the global variable
* - When running in a Browser the window variable
* - If unknown: a fallback object
*/
export const getGlobal : <T extends unknown = GlobalContext>() => T = () =>
{
let ctx: any = null;
if (!ctx) try { ctx = window; } catch (e) { /* Ignore */ }
if (!ctx) try { ctx = global; } catch (e) { /* Ignore */ };
ctx = ctx || fallback;
return ctx;
}
export default getGlobal;