Skip to content

Commit 5b0af46

Browse files
committed
fix: start method; feat: bound query/mutation methods
1 parent 25494c2 commit 5b0af46

File tree

9 files changed

+116
-142
lines changed

9 files changed

+116
-142
lines changed

.changeset/big-impalas-burn.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+
fixed query `start()` method (duplicate request calls) and ignoring updating query params

.changeset/cuddly-suits-fail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx-tanstack-query": minor
3+
---
4+
5+
make `mutate` method in Mutation as bounded method

.changeset/heavy-pumas-feel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx-tanstack-query": minor
3+
---
4+
5+
make `start` as bounded method for queries

.changeset/huge-flowers-agree.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx-tanstack-query": minor
3+
---
4+
5+
make `refetch` as bounded method for queries

.changeset/olive-eagles-tan.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx-tanstack-query": minor
3+
---
4+
5+
make `start` method in Mutation as bounded method

src/inifinite-query.ts

Lines changed: 31 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
DefaultError,
44
FetchNextPageOptions,
55
FetchPreviousPageOptions,
6-
hashKey,
76
InfiniteQueryObserver,
87
QueryKey,
98
InfiniteQueryObserverResult,
@@ -25,7 +24,6 @@ import {
2524
} from 'mobx';
2625
import { lazyObserve } from 'yummies/mobx';
2726

28-
import { enableHolder } from './constants';
2927
import {
3028
InfiniteQueryConfig,
3129
InfiniteQueryDoneListener,
@@ -37,6 +35,7 @@ import {
3735
InfiniteQueryStartParams,
3836
InfiniteQueryUpdateOptionsAllVariants,
3937
} from './inifinite-query.types';
38+
import { Query } from './query';
4039
import { AnyQueryClient, QueryClientHooks } from './query-client.types';
4140
import { QueryFeatures } from './query.types';
4241

@@ -348,6 +347,8 @@ export class InfiniteQuery<
348347
action.bound(this, 'setData');
349348
action.bound(this, 'update');
350349
action.bound(this, 'updateResult');
350+
this.refetch = this.refetch.bind(this);
351+
this.start = this.start.bind(this);
351352

352353
originalQueryProperties.forEach((property) => {
353354
if (this[property]) return;
@@ -466,7 +467,9 @@ export class InfiniteQuery<
466467
this._observerSubscription = this.queryObserver.subscribe(
467468
this.updateResult,
468469
);
469-
this.abortController.signal.addEventListener('abort', this.handleAbort);
470+
this.abortController.signal.addEventListener('abort', () =>
471+
this.handleAbort(),
472+
);
470473
}
471474

472475
if (config.onDone) {
@@ -490,11 +493,8 @@ export class InfiniteQuery<
490493
TQueryKey
491494
>,
492495
) {
493-
if (options.queryKeyHashFn) {
494-
return options.queryKeyHashFn(queryKey);
495-
}
496-
497-
return hashKey(queryKey);
496+
// @ts-ignore
497+
return Query.prototype.createQueryHash.call(this, queryKey, options);
498498
}
499499

500500
setData(
@@ -531,70 +531,23 @@ export class InfiniteQuery<
531531
TQueryKey
532532
>,
533533
) {
534-
if (this.abortController.signal.aborted) {
535-
return;
536-
}
537-
538-
const nextOptions = {
539-
...this.options,
540-
...optionsUpdate,
541-
} as InfiniteQueryOptions<
542-
TQueryFnData,
543-
TError,
544-
TPageParam,
545-
TData,
546-
TQueryKey
547-
>;
548-
549-
this.processOptions(nextOptions);
550-
551-
this.options = nextOptions;
552-
553-
// @ts-expect-error
554-
this.queryObserver.setOptions(this.options);
555-
556-
if (this.isLazy) {
557-
this.updateResult(this.queryObserver.getCurrentResult());
558-
}
534+
return Query.prototype.update.call(this, optionsUpdate);
559535
}
560536

561537
private isEnableHolded = false;
562538

563-
private processOptions = (
539+
private processOptions(
564540
options: InfiniteQueryOptions<
565541
TQueryFnData,
566542
TError,
567543
TPageParam,
568544
TData,
569545
TQueryKey
570546
>,
571-
) => {
572-
options.queryHash = this.createQueryHash(options.queryKey, options);
573-
574-
// If the on-demand query mode is enabled (when using the result property)
575-
// then, if the user does not request the result, the queries should not be executed
576-
// to do this, we hold the original value of the enabled option
577-
// and set enabled to false until the user requests the result (this.isResultRequsted)
578-
if (this.isEnabledOnResultDemand) {
579-
if (this.isEnableHolded && options.enabled !== enableHolder) {
580-
this.holdedEnabledOption = options.enabled;
581-
}
582-
583-
if (this.isResultRequsted) {
584-
if (this.isEnableHolded) {
585-
options.enabled =
586-
this.holdedEnabledOption === enableHolder
587-
? undefined
588-
: this.holdedEnabledOption;
589-
this.isEnableHolded = false;
590-
}
591-
} else {
592-
this.isEnableHolded = true;
593-
this.holdedEnabledOption = options.enabled;
594-
options.enabled = enableHolder;
595-
}
596-
}
597-
};
547+
) {
548+
// @ts-ignore works the same
549+
return Query.prototype.processOptions.call(this, options);
550+
}
598551

599552
public get result() {
600553
if (this.isEnabledOnResultDemand && !this.isResultRequsted) {
@@ -610,47 +563,20 @@ export class InfiniteQuery<
610563
* Modify this result so it matches the tanstack query result.
611564
*/
612565
private updateResult(result: InfiniteQueryObserverResult<TData, TError>) {
613-
this._result = result || {};
614-
615-
if (result.isSuccess && !result.error && result.fetchStatus === 'idle') {
616-
this.doneListeners.forEach((fn) => fn(result.data!, void 0));
617-
} else if (result.error) {
618-
this.errorListeners.forEach((fn) => fn(result.error!, void 0));
619-
}
566+
// @ts-ignore
567+
return Query.prototype.updateResult.call(this, result);
620568
}
621569

622570
async refetch(options?: RefetchOptions) {
623-
const result = await this.queryObserver.refetch(options);
624-
const query = this.queryObserver.getCurrentQuery();
625-
626-
if (
627-
query.state.error &&
628-
(options?.throwOnError ||
629-
this.options.throwOnError === true ||
630-
(typeof this.options.throwOnError === 'function' &&
631-
// @ts-expect-error
632-
this.options.throwOnError(query.state.error, query)))
633-
) {
634-
throw query.state.error;
635-
}
636-
637-
return result;
571+
return await Query.prototype.refetch.call(this, options);
638572
}
639573

640574
async reset(params?: InfiniteQueryResetParams) {
641-
await this.queryClient.resetQueries({
642-
queryKey: this.options.queryKey,
643-
exact: true,
644-
...params,
645-
} as any);
575+
return await Query.prototype.reset.call(this, params);
646576
}
647577

648578
async invalidate(options?: InfiniteQueryInvalidateParams) {
649-
await this.queryClient.invalidateQueries({
650-
exact: true,
651-
queryKey: this.options.queryKey,
652-
...options,
653-
} as any);
579+
return await Query.prototype.invalidate.call(this, options);
654580
}
655581

656582
onDone(doneListener: InfiniteQueryDoneListener<TData>): void {
@@ -661,22 +587,19 @@ export class InfiniteQuery<
661587
this.errorListeners.push(errorListener);
662588
}
663589

664-
async start({
665-
cancelRefetch,
666-
...params
667-
}: InfiniteQueryStartParams<
668-
TQueryFnData,
669-
TError,
670-
TPageParam,
671-
TData,
672-
TQueryKey
673-
> = {}) {
674-
this.update({ ...params });
675-
676-
return await this.refetch({ cancelRefetch });
590+
async start(
591+
params: InfiniteQueryStartParams<
592+
TQueryFnData,
593+
TError,
594+
TPageParam,
595+
TData,
596+
TQueryKey
597+
> = {},
598+
) {
599+
return await Query.prototype.start.call(this, params);
677600
}
678601

679-
protected handleAbort = () => {
602+
protected handleAbort() {
680603
this._observerSubscription?.();
681604

682605
this.doneListeners = [];
@@ -700,7 +623,7 @@ export class InfiniteQuery<
700623

701624
delete this._observerSubscription;
702625
this.hooks?.onInfiniteQueryDestroy?.(this);
703-
};
626+
}
704627

705628
destroy() {
706629
this.abortController.abort();

src/mutation.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ export class Mutation<
173173

174174
observable.deep(this, 'result');
175175
action.bound(this, 'updateResult');
176+
this.mutate = this.mutate.bind(this);
177+
this.start = this.start.bind(this);
176178

177179
originalMutationProperties.forEach((property) => {
178180
if (property === 'error' && transformError) {

0 commit comments

Comments
 (0)