Skip to content

Commit 6029956

Browse files
committed
fix: perf improvenment (redundand observable result update call)
1 parent cd2e6e1 commit 6029956

File tree

4 files changed

+52
-48
lines changed

4 files changed

+52
-48
lines changed

.changeset/sweet-facts-relax.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx-tanstack-query": patch
3+
---
4+
5+
fix redundant updateResult observable call after initialize mutation/query

src/inifinite-query.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -360,24 +360,6 @@ export class InfiniteQuery<
360360
};
361361
this.hooks = qc.hooks;
362362

363-
observable.deep(this, '_result');
364-
observable.ref(this, 'isResultRequsted');
365-
action.bound(this, 'setData');
366-
action.bound(this, 'update');
367-
action.bound(this, 'updateResult');
368-
this.refetch = this.refetch.bind(this);
369-
this.start = this.start.bind(this);
370-
371-
originalQueryProperties.forEach((property) => {
372-
if (!this[property]) {
373-
Object.defineProperty(this, property, {
374-
get: () => this.result[property],
375-
});
376-
}
377-
});
378-
379-
makeObservable(this);
380-
381363
const isQueryKeyDynamic = typeof queryKeyOrDynamicQueryKey === 'function';
382364

383365
if (!isQueryKeyDynamic) {
@@ -434,6 +416,24 @@ export class InfiniteQuery<
434416
// @ts-expect-error
435417
this.updateResult(this.queryObserver.getOptimisticResult(this.options));
436418

419+
observable.deep(this, '_result');
420+
observable.ref(this, 'isResultRequsted');
421+
action.bound(this, 'setData');
422+
action.bound(this, 'update');
423+
action.bound(this, 'updateResult');
424+
this.refetch = this.refetch.bind(this);
425+
this.start = this.start.bind(this);
426+
427+
originalQueryProperties.forEach((property) => {
428+
if (!this[property]) {
429+
Object.defineProperty(this, property, {
430+
get: () => this.result[property],
431+
});
432+
}
433+
});
434+
435+
makeObservable(this);
436+
437437
if (this.features.lazy) {
438438
const cleanup = lazyObserve({
439439
context: this,

src/mutation.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,6 @@ export class Mutation<
182182
this.errorListeners = [];
183183
this.doneListeners = [];
184184

185-
observable.deep(this, 'result');
186-
action.bound(this, 'updateResult');
187-
this.mutate = this.mutate.bind(this);
188-
this.start = this.start.bind(this);
189-
190-
originalMutationProperties.forEach((property) => {
191-
Object.defineProperty(this, property, {
192-
get: () => this.result[property],
193-
});
194-
});
195-
196-
makeObservable(this);
197-
198185
this.mutationOptions = this.queryClient.defaultMutationOptions(restOptions);
199186

200187
this.mutationObserver = new MutationObserver<
@@ -215,6 +202,19 @@ export class Mutation<
215202

216203
this.updateResult(this.mutationObserver.getCurrentResult());
217204

205+
observable.deep(this, 'result');
206+
action.bound(this, 'updateResult');
207+
this.mutate = this.mutate.bind(this);
208+
this.start = this.start.bind(this);
209+
210+
originalMutationProperties.forEach((property) => {
211+
Object.defineProperty(this, property, {
212+
get: () => this.result[property],
213+
});
214+
});
215+
216+
makeObservable(this);
217+
218218
if (this.features.lazy) {
219219
const cleanup = lazyObserve({
220220
context: this,

src/query.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
when,
2323
} from 'mobx';
2424
import { lazyObserve } from 'yummies/mobx';
25-
2625
import { enableHolder } from './constants.js';
2726
import type {
2827
QueryConfig,
@@ -358,22 +357,6 @@ export class Query<
358357
};
359358
this.hooks = qc.hooks;
360359

361-
observable.deep(this, '_result');
362-
observable.ref(this, 'isResultRequsted');
363-
action.bound(this, 'setData');
364-
action.bound(this, 'update');
365-
action.bound(this, 'updateResult');
366-
this.refetch = this.refetch.bind(this);
367-
this.start = this.start.bind(this);
368-
369-
originalQueryProperties.forEach((property) => {
370-
Object.defineProperty(this, property, {
371-
get: () => this.result[property],
372-
});
373-
});
374-
375-
makeObservable(this);
376-
377360
const isQueryKeyDynamic = typeof queryKeyOrDynamicQueryKey === 'function';
378361

379362
if (!isQueryKeyDynamic) {
@@ -426,6 +409,22 @@ export class Query<
426409

427410
this.updateResult(this.queryObserver.getOptimisticResult(this.options));
428411

412+
observable.deep(this, '_result');
413+
observable.ref(this, 'isResultRequsted');
414+
action.bound(this, 'setData');
415+
action.bound(this, 'update');
416+
action.bound(this, 'updateResult');
417+
this.refetch = this.refetch.bind(this);
418+
this.start = this.start.bind(this);
419+
420+
originalQueryProperties.forEach((property) => {
421+
Object.defineProperty(this, property, {
422+
get: () => this.result[property],
423+
});
424+
});
425+
426+
makeObservable(this);
427+
429428
if (this.features.lazy) {
430429
const cleanup = lazyObserve({
431430
context: this,

0 commit comments

Comments
 (0)