Skip to content

Commit

Permalink
Adds computedConfigurable (mobxjs#2014)
Browse files Browse the repository at this point in the history
  • Loading branch information
jillesme authored and alexeygt committed Aug 15, 2019
1 parent a1ca495 commit 93527a2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/api/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import {
export function configure(options: {
enforceActions?: boolean | "strict" | "never" | "always" | "observed"
computedRequiresReaction?: boolean
computedConfigurable?: boolean
isolateGlobalState?: boolean
disableErrorBoundaries?: boolean
reactionScheduler?: (f: () => void) => void
}): void {
const {
enforceActions,
computedRequiresReaction,
computedConfigurable,
disableErrorBoundaries,
reactionScheduler
} = options
Expand Down Expand Up @@ -52,6 +54,9 @@ export function configure(options: {
if (computedRequiresReaction !== undefined) {
globalState.computedRequiresReaction = !!computedRequiresReaction
}
if (computedConfigurable !== undefined) {
globalState.computedConfigurable = !!computedConfigurable
}
if (disableErrorBoundaries !== undefined) {
if (disableErrorBoundaries === true)
console.warn(
Expand Down
8 changes: 7 additions & 1 deletion src/core/globalstate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,20 @@ export class MobXGlobals {
*/
computedRequiresReaction = false

/**
* Allows overwriting of computed properties, useful in tests but not prod as it can cause
* memory leaks. See https://github.com/mobxjs/mobx/issues/1867
*/
computedConfigurable = false

/*
* Don't catch and rethrow exceptions. This is useful for inspecting the state of
* the stack when an exception occurs while debugging.
*/
disableErrorBoundaries = false

/*
* If true, we are already handling an exception in an action. Any errors in reactions should be supressed, as
* If true, we are already handling an exception in an action. Any errors in reactions should be supressed, as
* they are not the cause, see: https://github.com/mobxjs/mobx/issues/1836
*/
suppressReactionErrors = false
Expand Down
2 changes: 1 addition & 1 deletion src/types/observableobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ export function generateComputedPropConfig(propName) {
return (
computedPropertyConfigs[propName] ||
(computedPropertyConfigs[propName] = {
configurable: false, // See https://github.com/mobxjs/mobx/issues/1867, for computeds, we don't want reconfiguration, as this will potentially leak memory!
configurable: globalState.computedConfigurable,
enumerable: false,
get() {
return getAdministrationForComputedPropOwner(this).read(propName)
Expand Down
22 changes: 22 additions & 0 deletions test/base/strict-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,25 @@ test("#1869", function() {
}).toThrow("Since strict-mode is enabled")
mobx._resetGlobalState() // should preserve strict mode
})

test("allow overwriting computed if configured", function() {
try {
mobx.configure({ computedConfigurable: true })
const x = mobx.observable({
v: 2,
get multiplied() {
return x * 2
}
})
mobx.decorate(x, { multiplied: mobx.computed })

expect(() => {
Object.defineProperty(x, "multiplied", {
value: 12
})
}).not.toThrow()
expect(x.multiplied).toBe(12)
} finally {
mobx.configure({ computedConfigurable: false })
}
})

0 comments on commit 93527a2

Please sign in to comment.