Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #3109: spy: computed shouldn't report update unless the value changed #3146

Merged
merged 3 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/yellow-apples-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx": patch
---

fix #3109: spy: computed shouldn't report update unless the value changed
48 changes: 44 additions & 4 deletions packages/mobx/__tests__/v5/base/spy.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,51 @@ test("bound actions report correct object (discussions/3140)", () => {
const appState = new AppState();
const { actionBound, autoActionBound } = appState;

mobx.spy((event) => {
let events = [];
const disposeSpy = mobx.spy((event) => {
if (event.type !== 'action') return;
expect(event.object).toBe(appState);
events.push(event);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored so that spy is also disposed in case of test failure.

});

actionBound();
autoActionBound();
try {
actionBound();
expect(events.pop().object).toBe(appState)
autoActionBound();
expect(events.pop().object).toBe(appState)
} finally {
disposeSpy();
}
})

test("computed shouldn't report update unless the value changed #3109", () => {
const number = mobx.observable({
value: 0,
get isEven() {
return (this.value % 2) === 0;
}
})

const events = [];
const disposeSpy = mobx.spy(event => {
if (event.observableKind === 'computed' && event.type === 'update') {
events.push(event);
};
});

const disposeAutorun = mobx.autorun(() => number.isEven);

try {
expect(events.pop()).toMatchObject({ oldValue: { cause: null }, newValue: true });
number.value++; // 1
expect(events.pop()).toMatchObject({ oldValue: true, newValue: false });
number.value++; // 2
expect(events.pop()).toMatchObject({ oldValue: false, newValue: true });
number.value += 2; // 4
expect(events.pop()).toBe(undefined);
number.value += 2; // 6
expect(events.pop()).toBe(undefined);
} finally {
disposeSpy();
disposeAutorun();
}
})
22 changes: 11 additions & 11 deletions packages/mobx/src/core/computedvalue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,6 @@ export class ComputedValue<T> implements IObservable, IComputedValue<T>, IDeriva
/* see #1208 */ this.dependenciesState_ === IDerivationState_.NOT_TRACKING_
const newValue = this.computeValue_(true)

if (__DEV__ && isSpyEnabled()) {
spyReport({
observableKind: "computed",
debugObjectName: this.name_,
object: this.scope_,
type: "update",
oldValue: this.value_,
newValue
} as IComputedDidChange)
}

const changed =
wasSuspended ||
isCaughtException(oldValue) ||
Expand All @@ -223,6 +212,17 @@ export class ComputedValue<T> implements IObservable, IComputedValue<T>, IDeriva

if (changed) {
this.value_ = newValue

if (__DEV__ && isSpyEnabled()) {
spyReport({
observableKind: "computed",
debugObjectName: this.name_,
object: this.scope_,
type: "update",
oldValue,
newValue
} as IComputedDidChange)
}
}

return changed
Expand Down