-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
103 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"mobx": patch | ||
--- | ||
|
||
Introduce warningSeverity configuration option. This allows consumers to promote specific warnings to exceptions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { onBecomeUnobserved, configure, computed, _resetGlobalState } from "../../../src/mobx" | ||
|
||
describe("Configuring warning severity", () => { | ||
let consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation() | ||
|
||
beforeEach(() => _resetGlobalState()) | ||
|
||
it("Should default to console.warn when not configured", () => { | ||
const a = computed(() => console.log("b"), { requiresReaction: true }) | ||
onBecomeUnobserved(a, () => console.log("c")) | ||
a.get() | ||
|
||
expect(consoleWarnSpy).toHaveBeenCalled() | ||
}) | ||
|
||
it("Should throw an exception when configured with 'throw'", () => { | ||
configure({ warningSeverity: { computedRequiresReaction: "throw" } }) | ||
|
||
const a = computed(() => console.log("b"), { requiresReaction: true }) | ||
onBecomeUnobserved(a, () => console.log("c")) | ||
|
||
expect(() => a.get()).toThrow( | ||
`[mobx] Computed value 'ComputedValue@2' is being read outside a reactive context. Doing a full recompute.` | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { globalState } from "./internal" | ||
|
||
const warnings = { | ||
computedRequiresReaction: (name: string) => | ||
`[mobx] Computed value '${name}' is being read outside a reactive context. Doing a full recompute.`, | ||
|
||
enforceActionsStrict: (name: string) => | ||
`[MobX] Since strict-mode is enabled, changing (observed) observable values without using an action is not ` + | ||
`allowed. Tried to modify: ${name}`, | ||
|
||
enforceActionsNonStrict: (name: string) => | ||
`[MobX] Side effects like changing state are not allowed at this point. Are you trying to modify state from, ` + | ||
`for example, a computed value or the render function of a React component? You can wrap side effects in ` + | ||
`'runInAction' (or decorate functions with 'action') if needed. Tried to modify: ${name}`, | ||
|
||
observableRequiresReaction: (name: string) => | ||
`[mobx] Observable '${name}' being read outside a reactive context.`, | ||
|
||
derivationWithoutDependencies: (name: string) => | ||
`[mobx] Derivation '${name}' is created/updated without reading any observable value.` | ||
} | ||
type Warnings = typeof warnings | ||
export type WarningSeverity = { [k in keyof Warnings]?: "warn" | "throw" } | ||
|
||
export function warn<K extends keyof Warnings>(warning: K, ...args: Parameters<Warnings[K]>) { | ||
const message = (warnings[warning] as (...args: any[]) => string).call(null, args) | ||
|
||
if (globalState.warningSeverity[warning] === "throw") { | ||
throw new Error(message) | ||
} else { | ||
console.warn(message) | ||
} | ||
} |