UNPKG

2.23 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") {
38 globalState.verifyProxies = true
39 }
40 if (enforceActions !== undefined) {
41 const ea = enforceActions === ALWAYS ? ALWAYS : enforceActions === OBSERVED
42 globalState.enforceActions = ea
43 globalState.allowStateChanges = ea === true || ea === ALWAYS ? false : true
44 }
45 ;[
46 "computedRequiresReaction",
47 "reactionRequiresObservable",
48 "observableRequiresReaction",
49 "disableErrorBoundaries",
50 "safeDescriptors"
51 ].forEach(key => {
52 if (key in options) {
53 globalState[key] = !!options[key]
54 }
55 })
56 globalState.allowStateReads = !globalState.observableRequiresReaction
57 if (__DEV__ && globalState.disableErrorBoundaries === true) {
58 console.warn(
59 "WARNING: Debug feature only. MobX will NOT recover from errors when `disableErrorBoundaries` is enabled."
60 )
61 }
62 if (options.reactionScheduler) {
63 setReactionScheduler(options.reactionScheduler)
64 }
65}