-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
index.ts
29 lines (22 loc) · 1.01 KB
/
index.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
/**
* This file is consumed by fabric.
* The `./node` and `./browser` files define the env variable that is used by this module.
* The `./node` module sets the env at import time.
* The `./browser` module is defined to be the default env and doesn't set the env at all.
* This is done in order to support isomorphic usage for browser and node applications
* since window and document aren't defined at time of import in SSR, we can't set env so we avoid it by deferring to the default env.
*/
import { TFabricEnv } from './types';
import { getEnv as getBrowserEnv } from './browser';
import type { DOMWindow } from 'jsdom';
let env: TFabricEnv;
export const setEnv = (value: TFabricEnv) => {
env = value;
};
export const getEnv = () => env || getBrowserEnv();
export const getDocument = (): Document => getEnv().document;
export const getWindow = (): Window | DOMWindow => getEnv().window;
export const setEnvForTests = (window: Window | DOMWindow) => {
env.document = window.document;
env.window = window;
};