Skip to content

Commit 87005aa

Browse files
committed
fix: preset exports; refactor: destroyable
1 parent 4579953 commit 87005aa

File tree

8 files changed

+122
-151
lines changed

8 files changed

+122
-151
lines changed

.changeset/chilly-poems-grin.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+
refactored unify destroy and aborts inside package entities

.changeset/ninety-eels-dream.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 preset exports

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"zshy": {
3636
"exports": {
3737
"./package.json": "./package.json",
38-
".": "./src/index.ts"
38+
".": "./src/index.ts",
39+
"./preset": "./src/preset/index.ts"
3940
}
4041
},
4142
"bugs": {

src/inifinite-query.ts

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
RefetchOptions,
1515
SetDataOptions,
1616
} from '@tanstack/query-core';
17-
import { LinkedAbortController } from 'linked-abort-controller';
1817
import {
1918
action,
2019
reaction,
@@ -40,6 +39,7 @@ import { Query } from './query';
4039
import { QueryClient } from './query-client';
4140
import { AnyQueryClient, QueryClientHooks } from './query-client.types';
4241
import { QueryFeatures } from './query.types';
42+
import { Destroyable } from './utils/destroyable';
4343

4444
const originalQueryProperties = [
4545
'data',
@@ -80,14 +80,14 @@ export class InfiniteQuery<
8080
TData = InfiniteData<TQueryFnData, TPageParam>,
8181
TQueryKey extends QueryKey = QueryKey,
8282
>
83+
extends Destroyable
8384
implements
8485
Disposable,
8586
Pick<
8687
InfiniteQueryObserverBaseResult<TData, TError>,
8788
(typeof originalQueryProperties)[number]
8889
>
8990
{
90-
protected abortController: LinkedAbortController;
9191
protected queryClient: AnyQueryClient;
9292

9393
protected _result: InfiniteQueryObserverResult<TData, TError>;
@@ -313,6 +313,9 @@ export class InfiniteQuery<
313313
config = args[0];
314314
getDynamicOptions = args[0].options;
315315
}
316+
317+
super(config.abortSignal);
318+
316319
this.cumulativeQueryKeyHashesSet = new Set();
317320

318321
const { queryKey: queryKeyOrDynamicQueryKey, ...restOptions } = config;
@@ -322,7 +325,6 @@ export class InfiniteQuery<
322325
queryClient,
323326
};
324327

325-
this.abortController = new LinkedAbortController(config.abortSignal);
326328
this.queryClient = queryClient;
327329
this._result = undefined as any;
328330
this.isResultRequsted = false;
@@ -360,7 +362,6 @@ export class InfiniteQuery<
360362

361363
observable.deep(this, '_result');
362364
observable.ref(this, 'isResultRequsted');
363-
action(this, 'handleAbort');
364365
action.bound(this, 'setData');
365366
action.bound(this, 'update');
366367
action.bound(this, 'updateResult');
@@ -491,7 +492,7 @@ export class InfiniteQuery<
491492
this.updateResult,
492493
);
493494
this.abortController.signal.addEventListener('abort', () =>
494-
this.handleAbort(),
495+
this.handleDestroy(),
495496
);
496497
}
497498

@@ -625,51 +626,11 @@ export class InfiniteQuery<
625626
return await Query.prototype.start.call(this, params);
626627
}
627628

628-
protected handleAbort() {
629-
this._observerSubscription?.();
630-
631-
this.doneListeners = [];
632-
this.errorListeners = [];
633-
634-
this.queryObserver.destroy();
635-
this.isResultRequsted = false;
636-
637-
if (this.features.resetOnDestroy) {
638-
this.reset();
639-
}
640-
641-
if (this.features.removeOnDestroy) {
642-
this.remove({
643-
safe: this.features.removeOnDestroy === 'safe',
644-
});
645-
}
646-
647-
delete this._observerSubscription;
648-
649-
this.cumulativeQueryKeyHashesSet.clear();
650-
629+
protected handleDestroy() {
630+
// @ts-expect-error
631+
Query.prototype.cleanup.call(this);
651632
this.hooks?.onInfiniteQueryDestroy?.(this);
652633
}
653-
654-
destroy() {
655-
this.abortController.abort();
656-
}
657-
658-
/**
659-
* @deprecated use `destroy`. This method will be removed in next major release
660-
*/
661-
dispose() {
662-
this.destroy();
663-
}
664-
665-
[Symbol.dispose](): void {
666-
this.destroy();
667-
}
668-
669-
// Firefox fix (Symbol.dispose is undefined in FF)
670-
[Symbol.for('Symbol.dispose')](): void {
671-
this.destroy();
672-
}
673634
}
674635

675636
/**

src/mutation.ts

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
MutationObserverBaseResult,
88
MutationStatus,
99
} from '@tanstack/query-core';
10-
import { LinkedAbortController } from 'linked-abort-controller';
1110
import { action, makeObservable, observable } from 'mobx';
1211
import { lazyObserve } from 'yummies/mobx';
1312

@@ -21,6 +20,7 @@ import {
2120
} from './mutation.types';
2221
import { QueryClient } from './query-client';
2322
import { AnyQueryClient, QueryClientHooks } from './query-client.types';
23+
import { Destroyable } from './utils/destroyable';
2424

2525
const originalMutationProperties = [
2626
'data',
@@ -44,14 +44,14 @@ export class Mutation<
4444
TError = DefaultError,
4545
TContext = unknown,
4646
>
47+
extends Destroyable
4748
implements
4849
Disposable,
4950
Pick<
5051
MutationObserverBaseResult<TData, TError, TVariables, TContext>,
5152
(typeof originalMutationProperties)[number]
5253
>
5354
{
54-
protected abortController: LinkedAbortController;
5555
protected queryClient: AnyQueryClient;
5656

5757
mutationOptions: MutationObserverOptions<TData, TError, TVariables, TContext>;
@@ -129,6 +129,8 @@ export class Mutation<
129129
constructor(
130130
protected config: MutationConfig<TData, TVariables, TError, TContext>,
131131
) {
132+
super(config.abortSignal);
133+
132134
const { queryClient, invalidateQueries, mutationFn, ...restOptions } =
133135
config;
134136

@@ -138,7 +140,6 @@ export class Mutation<
138140
Pick<QueryClient, 'mutationFeatures' | 'hooks'>
139141
>;
140142

141-
this.abortController = new LinkedAbortController(config.abortSignal);
142143
this.queryClient = queryClient;
143144
this.result = undefined as any;
144145

@@ -219,8 +220,6 @@ export class Mutation<
219220
this._observerSubscription = this.mutationObserver.subscribe(
220221
this.updateResult,
221222
);
222-
223-
this.abortController.signal.addEventListener('abort', this.handleAbort);
224223
}
225224

226225
if (invalidateQueries) {
@@ -348,7 +347,7 @@ export class Mutation<
348347
this.mutationObserver.reset();
349348
}
350349

351-
protected handleAbort = () => {
350+
protected handleDestroy() {
352351
this._observerSubscription?.();
353352

354353
this.doneListeners = [];
@@ -361,26 +360,6 @@ export class Mutation<
361360

362361
delete this._observerSubscription;
363362
this.hooks?.onMutationDestroy?.(this);
364-
};
365-
366-
destroy() {
367-
this.abortController.abort();
368-
}
369-
370-
/**
371-
* @deprecated use `destroy`. This method will be removed in next major release
372-
*/
373-
dispose() {
374-
this.destroy();
375-
}
376-
377-
[Symbol.dispose](): void {
378-
this.destroy();
379-
}
380-
381-
// Firefox fix (Symbol.dispose is undefined in FF)
382-
[Symbol.for('Symbol.dispose')](): void {
383-
this.destroy();
384363
}
385364
}
386365

0 commit comments

Comments
 (0)