UNPKG

2.19 kBPlain TextView Raw
1import { globalState, isolateGlobalState, setReactionScheduler } from "../internal"
2
3const NEVER = "never"
4const ALWAYS = "always"
5const OBSERVED = "observed"
6// const IF_AVAILABLE = "ifavailable"
7
8export function configure(options: {
9 enforceActions?: "never" | "always" | "observed"
10 computedRequiresReaction?: boolean
11 /**
12 * Warn if you try to create to derivation / reactive context without accessing any observable.
13 */
14 reactionRequiresObservable?: boolean
15 /**
16 * Warn if observables are accessed outside a reactive context
17 */
18 observableRequiresReaction?: boolean
19 isolateGlobalState?: boolean
20 disableErrorBoundaries?: boolean
21 safeDescriptors?: boolean
22 reactionScheduler?: (f: () => void) => void
23 useProxies?: "always" | "never" | "ifavailable"
24}): void {
25 if (options.isolateGlobalState === true) {
26 isolateGlobalState()
27 }
28 const { useProxies, enforceActions } = options
29 if (useProxies !== undefined) {
30 globalState.useProxies =
31 useProxies === ALWAYS
32 ? true
33 : useProxies === NEVER
34 ? false
35 : typeof Proxy !== "undefined"
36 }
37 if (useProxies === "ifavailable") globalState.verifyProxies = true
38 if (enforceActions !== undefined) {
39 const ea = enforceActions === ALWAYS ? ALWAYS : enforceActions === OBSERVED
40 globalState.enforceActions = ea
41 globalState.allowStateChanges = ea === true || ea === ALWAYS ? false : true
42 }
43 ;[
44 "computedRequiresReaction",
45 "reactionRequiresObservable",
46 "observableRequiresReaction",
47 "disableErrorBoundaries",
48 "safeDescriptors"
49 ].forEach(key => {
50 if (key in options) globalState[key] = !!options[key]
51 })
52 globalState.allowStateReads = !globalState.observableRequiresReaction
53 if (__DEV__ && globalState.disableErrorBoundaries === true) {
54 console.warn(
55 "WARNING: Debug feature only. MobX will NOT recover from errors when `disableErrorBoundaries` is enabled."
56 )
57 }
58 if (options.reactionScheduler) {
59 setReactionScheduler(options.reactionScheduler)
60 }
61}