Skip to content

Commit

Permalink
requiresReaction always takes precedence over global `computedRequi…
Browse files Browse the repository at this point in the history
…resReaction` (#3214)
  • Loading branch information
urugator committed Dec 15, 2021
1 parent 9b90e25 commit 87e5a03
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-rabbits-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx": patch
---

`requiresReaction` always takes precedence over global `computedRequiresReaction`
49 changes: 41 additions & 8 deletions packages/mobx/__tests__/v5/base/observables.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const {
const utils = require("../../v5/utils/test-utils")
const { MAX_SPLICE_SIZE } = require("../../../src/internal")

const voidObserver = function () {}
const voidObserver = function () { }

function buffer() {
const b = []
Expand Down Expand Up @@ -2100,7 +2100,7 @@ test("extendObservable should not accept complex objects as second argument", ()
})

test("observable ignores class instances #2579", () => {
class C {}
class C { }
const c = new C()
expect(observable(c)).toBe(c)
})
Expand All @@ -2121,9 +2121,9 @@ test("configure({ safeDescriptors: false })", () => {

class Clazz {
observable = 0
action() {}
get computed() {}
*flow() {}
action() { }
get computed() { }
*flow() { }
constructor() {
mobx.makeObservable(this, {
observable: mobx.observable,
Expand All @@ -2139,9 +2139,9 @@ test("configure({ safeDescriptors: false })", () => {

const plain = mobx.observable({
observable: 0,
action() {},
get computed() {},
*flow() {}
action() { },
get computed() { },
*flow() { }
})

checkDescriptors(plain)
Expand Down Expand Up @@ -2249,3 +2249,36 @@ test("ObservableArray.splice", () => {
expect(ar.length).toEqual(MAX_SPLICE_SIZE + 10)
expect(del.length).toEqual(MAX_SPLICE_SIZE + 1)
})

describe("`requiresReaction` takes precedence over global `computedRequiresReaction`", () => {
let warnMsg = "[mobx] Computed value 'TestComputed' is being read outside a reactive context. Doing a full recompute.";
let consoleWarnSpy;
beforeEach(() => {
consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation()
})
afterEach(() => {
consoleWarnSpy.mockRestore()
mobx._resetGlobalState();
})

test('`undefined`', () => {
mobx.configure({ computedRequiresReaction: true })
const c = mobx.computed(() => { }, { name: 'TestComputed' });
c.get();
expect(consoleWarnSpy).toHaveBeenLastCalledWith(warnMsg);
})

test('`true` over `false`', () => {
mobx.configure({ computedRequiresReaction: false })
const c = mobx.computed(() => { }, { name: 'TestComputed', requiresReaction: true });
c.get();
expect(consoleWarnSpy).toHaveBeenLastCalledWith(warnMsg);
})

test('`false` over `true`', () => {
mobx.configure({ computedRequiresReaction: true })
const c = mobx.computed(() => { }, { name: 'TestComputed', requiresReaction: false });
c.get();
expect(consoleWarnSpy).not.toHaveBeenCalled();
})
})
6 changes: 3 additions & 3 deletions packages/mobx/src/core/computedvalue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class ComputedValue<T> implements IObservable, IComputedValue<T>, IDeriva
isTracing_: TraceMode = TraceMode.NONE
scope_: Object | undefined
private equals_: IEqualsComparer<any>
private requiresReaction_: boolean
private requiresReaction_: boolean | undefined
keepAlive_: boolean

/**
Expand Down Expand Up @@ -129,7 +129,7 @@ export class ComputedValue<T> implements IObservable, IComputedValue<T>, IDeriva
? comparer.structural
: comparer.default)
this.scope_ = options.context
this.requiresReaction_ = !!options.requiresReaction
this.requiresReaction_ = options.requiresReaction
this.keepAlive_ = !!options.keepAlive
}

Expand Down Expand Up @@ -293,7 +293,7 @@ export class ComputedValue<T> implements IObservable, IComputedValue<T>, IDeriva
`[mobx.trace] Computed value '${this.name_}' is being read outside a reactive context. Doing a full recompute.`
)
}
if (globalState.computedRequiresReaction || this.requiresReaction_) {
if (typeof this.requiresReaction_ === "boolean" ? this.requiresReaction_ : globalState.computedRequiresReaction) {
console.warn(
`[mobx] Computed value '${this.name_}' is being read outside a reactive context. Doing a full recompute.`
)
Expand Down

0 comments on commit 87e5a03

Please sign in to comment.