Skip to content

Commit 6f095ed

Browse files
jillesmeFredyC
authored andcommitted
Adds computedConfigurable (#2014)
1 parent 38f2c51 commit 6f095ed

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

src/api/configure.ts

+5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import {
99
export function configure(options: {
1010
enforceActions?: boolean | "strict" | "never" | "always" | "observed"
1111
computedRequiresReaction?: boolean
12+
computedConfigurable?: boolean
1213
isolateGlobalState?: boolean
1314
disableErrorBoundaries?: boolean
1415
reactionScheduler?: (f: () => void) => void
1516
}): void {
1617
const {
1718
enforceActions,
1819
computedRequiresReaction,
20+
computedConfigurable,
1921
disableErrorBoundaries,
2022
reactionScheduler
2123
} = options
@@ -52,6 +54,9 @@ export function configure(options: {
5254
if (computedRequiresReaction !== undefined) {
5355
globalState.computedRequiresReaction = !!computedRequiresReaction
5456
}
57+
if (computedConfigurable !== undefined) {
58+
globalState.computedConfigurable = !!computedConfigurable
59+
}
5560
if (disableErrorBoundaries !== undefined) {
5661
if (disableErrorBoundaries === true)
5762
console.warn(

src/core/globalstate.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,20 @@ export class MobXGlobals {
101101
*/
102102
computedRequiresReaction = false
103103

104+
/**
105+
* Allows overwriting of computed properties, useful in tests but not prod as it can cause
106+
* memory leaks. See https://github.com/mobxjs/mobx/issues/1867
107+
*/
108+
computedConfigurable = false
109+
104110
/*
105111
* Don't catch and rethrow exceptions. This is useful for inspecting the state of
106112
* the stack when an exception occurs while debugging.
107113
*/
108114
disableErrorBoundaries = false
109115

110116
/*
111-
* If true, we are already handling an exception in an action. Any errors in reactions should be supressed, as
117+
* If true, we are already handling an exception in an action. Any errors in reactions should be supressed, as
112118
* they are not the cause, see: https://github.com/mobxjs/mobx/issues/1836
113119
*/
114120
suppressReactionErrors = false

src/types/observableobject.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ export function generateComputedPropConfig(propName) {
385385
return (
386386
computedPropertyConfigs[propName] ||
387387
(computedPropertyConfigs[propName] = {
388-
configurable: false, // See https://github.com/mobxjs/mobx/issues/1867, for computeds, we don't want reconfiguration, as this will potentially leak memory!
388+
configurable: globalState.computedConfigurable,
389389
enumerable: false,
390390
get() {
391391
return getAdministrationForComputedPropOwner(this).read(propName)

test/base/strict-mode.js

+22
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,25 @@ test("#1869", function() {
249249
}).toThrow("Since strict-mode is enabled")
250250
mobx._resetGlobalState() // should preserve strict mode
251251
})
252+
253+
test("allow overwriting computed if configured", function() {
254+
try {
255+
mobx.configure({ computedConfigurable: true })
256+
const x = mobx.observable({
257+
v: 2,
258+
get multiplied() {
259+
return x * 2
260+
}
261+
})
262+
mobx.decorate(x, { multiplied: mobx.computed })
263+
264+
expect(() => {
265+
Object.defineProperty(x, "multiplied", {
266+
value: 12
267+
})
268+
}).not.toThrow()
269+
expect(x.multiplied).toBe(12)
270+
} finally {
271+
mobx.configure({ computedConfigurable: false })
272+
}
273+
})

0 commit comments

Comments
 (0)