Skip to content

Commit 3ec51de

Browse files
committed
feat: add dynamicOptionsComparer query feature
1 parent 4ece1aa commit 3ec51de

File tree

8 files changed

+436
-117
lines changed

8 files changed

+436
-117
lines changed

.changeset/some-tigers-drive.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+
added `dynamicOptionsComparer` query feature

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"check": "npm run lint:check && npm run ts:check",
1010
"prebuild": "npm run clean && npm run check",
1111
"build:watch": "pnpm build && nodemon --watch src --ext ts,tsx --exec \"pnpm build\"",
12-
"build": "vite build && tsx scripts/prepare-dist",
12+
"build": "vite build",
1313
"pub": "pnpm build && sborshik publish --useDistDir",
1414
"pub:patch": "PUBLISH_VERSION=patch pnpm pub",
1515
"pub:minor": "PUBLISH_VERSION=minor pnpm pub",
@@ -67,7 +67,7 @@
6767
"lefthook": "^1.11.13",
6868
"nodemon": "^3.1.0",
6969
"rimraf": "^6.0.1",
70-
"sborshik": "^1.0.25",
70+
"sborshik": "^1.0.31",
7171
"terser": "^5.44.0",
7272
"tsx": "^4.20.6",
7373
"typescript": "^5.8.3",

pnpm-lock.yaml

Lines changed: 408 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/inifinite-query.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -467,28 +467,17 @@ export class InfiniteQuery<
467467

468468
config.abortSignal?.addEventListener('abort', cleanup);
469469
} else {
470-
if (isQueryKeyDynamic) {
471-
reaction(
472-
queryKeyOrDynamicQueryKey,
473-
(queryKey) => this.update({ queryKey }),
474-
{
475-
signal: this.abortController.signal,
476-
delay: this.config.dynamicOptionsUpdateDelay,
477-
},
478-
);
479-
}
480-
if (getDynamicOptions) {
481-
reaction(() => getDynamicOptions(this), this.update, {
482-
signal: this.abortController.signal,
470+
if (getAllDynamicOptions) {
471+
reaction(getAllDynamicOptions, this.update, {
483472
delay: this.config.dynamicOptionsUpdateDelay,
473+
signal: config.abortSignal,
474+
fireImmediately: true,
475+
equals: this.features.dynamicOptionsComparer,
484476
});
485477
}
486478
this._observerSubscription = this.queryObserver.subscribe(
487479
this.updateResult,
488480
);
489-
this.abortController.signal.addEventListener('abort', () =>
490-
this.handleDestroy(),
491-
);
492481
}
493482

494483
if (config.onDone) {

src/query.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ export class Query<
443443
delay: this.features.dynamicOptionsUpdateDelay,
444444
signal: config.abortSignal,
445445
fireImmediately: true,
446+
equals: this.features.dynamicOptionsComparer,
446447
});
447448
}
448449
}
@@ -459,20 +460,12 @@ export class Query<
459460

460461
config.abortSignal?.addEventListener('abort', cleanup);
461462
} else {
462-
if (isQueryKeyDynamic) {
463-
reaction(
464-
queryKeyOrDynamicQueryKey,
465-
(queryKey) => this.update({ queryKey }),
466-
{
467-
signal: this.abortController.signal,
468-
delay: this.features.dynamicOptionsUpdateDelay,
469-
},
470-
);
471-
}
472-
if (getDynamicOptions) {
473-
reaction(() => getDynamicOptions(this), this.update, {
474-
signal: this.abortController.signal,
463+
if (getAllDynamicOptions) {
464+
reaction(getAllDynamicOptions, this.update, {
475465
delay: this.features.dynamicOptionsUpdateDelay,
466+
signal: config.abortSignal,
467+
fireImmediately: true,
468+
equals: this.features.dynamicOptionsComparer,
476469
});
477470
}
478471
this._observerSubscription = this.queryObserver.subscribe(

src/query.types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
QueryKey,
77
QueryObserverOptions,
88
} from '@tanstack/query-core';
9-
9+
import type { IReactionOptions } from 'mobx';
1010
import type { Query } from './query.js';
1111
import type { AnyQueryClient } from './query-client.types.js';
1212

@@ -173,6 +173,8 @@ export interface QueryFeatures {
173173
* [**Documentation**](https://js2me.github.io/mobx-tanstack-query/api/Query.html#autoremovepreviousquery-queryfeature)
174174
*/
175175
autoRemovePreviousQuery?: boolean;
176+
177+
dynamicOptionsComparer?: IReactionOptions<any, any>['equals'];
176178
}
177179

178180
/**

vite.config.ts

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,4 @@
1-
import { defineConfig } from 'vite';
2-
import { resolve } from 'path';
3-
import dts from 'vite-plugin-dts';
4-
5-
import tsconfig from "./tsconfig.json";
6-
import packageJson from "./package.json";
7-
import yummiesPackageJson from "./node_modules/yummies/package.json";
8-
9-
const entries = Object.entries(tsconfig.compilerOptions.paths).map(([libName, paths]) => {
10-
return {
11-
libName,
12-
entryName: paths[0].replace('/index.ts', '').replace('.ts', '').replace('./src/', ''),
13-
entryPath: resolve(__dirname, paths[0]),
14-
}
15-
});
16-
17-
export default defineConfig({
18-
appType: 'spa',
19-
build: {
20-
minify: 'terser',
21-
sourcemap: true,
22-
lib: {
23-
entry: Object.assign({}, ...entries.map(entry => ({ [entry.entryName === './src' ? 'index' : entry.entryName]: entry.entryPath }))),
24-
formats: ['es', 'cjs'],
25-
},
26-
rollupOptions :{
27-
external: [...entries.map(entry => entry.libName), ...Object.keys(packageJson.peerDependencies), ...Object.keys(packageJson.dependencies), ...Object.keys(yummiesPackageJson.exports).map(key=> key.replace('./', 'yummies/'))],
28-
output: {
29-
chunkFileNames: '~[name]-[hash].js',
30-
}
31-
}
32-
},
33-
resolve: {},
34-
plugins: [dts()]
35-
})
1+
import { defineLibViteConfig } from "sborshik/vite";
2+
import { ConfigsManager } from "sborshik/utils";
3+
4+
export default defineLibViteConfig(ConfigsManager.create())

vitest.config.ts

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,4 @@
1-
import { defineConfig } from "vitest/config";
2-
import react from "@vitejs/plugin-react-swc";
1+
import { defineLibVitestConfig } from "sborshik/vite";
2+
import { ConfigsManager } from "sborshik/utils";
33

4-
import tsconfig from "./tsconfig.json";
5-
import { resolve } from "path";
6-
7-
const entries = Object.entries(tsconfig.compilerOptions.paths).map(([libName, paths]) => {
8-
const entryName = paths[0].replace('/index.ts', '').replace('.ts', '').replace('./src/', '');
9-
10-
return {
11-
libName,
12-
entryName: entryName === './src' ? 'index' : entryName,
13-
entryPath: resolve(__dirname, paths[0]),
14-
}
15-
})
16-
17-
export default defineConfig({
18-
// @ts-ignore
19-
plugins: [react()],
20-
test: {
21-
globals: true,
22-
environment: "jsdom",
23-
coverage: {
24-
provider: 'istanbul', // or 'v8'
25-
include: ['src'],
26-
exclude: ['src/preset'],
27-
reporter: [
28-
'text',
29-
'text-summary',
30-
'html'
31-
],
32-
reportsDirectory: './coverage'
33-
},
34-
},
35-
resolve: {
36-
alias: Object.assign({}, ...entries.map(entry => {
37-
return {
38-
[entry.libName]: entry.entryPath,
39-
}
40-
})),
41-
},
42-
});
4+
export default defineLibVitestConfig(ConfigsManager.create());

0 commit comments

Comments
 (0)