Skip to content

Commit e362a27

Browse files
authored
v4 (#77)
1 parent 1aaf520 commit e362a27

File tree

11 files changed

+4652
-10775
lines changed

11 files changed

+4652
-10775
lines changed

.github/workflows/unit-tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Setup Node.js
1919
uses: actions/setup-node@v2
2020
with:
21-
node-version: 14
21+
node-version: 18
2222

2323
- name: Checkout repository
2424
uses: actions/checkout@v2

packages/mst-query/package-lock.json

+4,355-10,630
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/mst-query/package.json

+32-21
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
{
22
"name": "mst-query",
3-
"version": "3.4.0-canary.16",
3+
"version": "3.4.0-canary.21",
44
"description": "Query library for mobx-state-tree",
55
"source": "src/index.ts",
6-
"main": "dist/mst-query.js",
7-
"exports": "./dist/mst-query.modern.js",
8-
"module": "dist/mst-query.module.js",
9-
"unpkg": "dist/mst-query.umd.js",
6+
"type": "module",
7+
"main": "dist/index.cjs",
8+
"module": "dist/index.js",
109
"types": "dist/index.d.ts",
10+
"exports": {
11+
"import": {
12+
"types": "./dist/index.d.ts",
13+
"import": "./dist/index.js"
14+
},
15+
"require": {
16+
"types": "./dist/index.d.cts",
17+
"require": "./dist/index.cjs"
18+
}
19+
},
1120
"scripts": {
12-
"build": "microbundle --tsconfig tsconfig.build.json --no-compress --external mobx,mobx-state-tree,react --jsx React.createElement",
21+
"build": "tsup src/index.ts --format cjs,esm --dts --clean --sourcemap",
1322
"watch": "vitest",
1423
"test": "vitest run"
1524
},
@@ -23,25 +32,27 @@
2332
"dist"
2433
],
2534
"devDependencies": {
26-
"@testing-library/react": "^13.4.0",
27-
"@types/react": "^18.3.3",
28-
"jsdom": "^20.0.1",
29-
"microbundle": "^0.15.0",
30-
"mobx": "6.6.1",
31-
"mobx-react": "7.5.2",
32-
"mobx-state-tree": "5.1.5",
33-
"prettier": "^2.2.1",
34-
"react": "^18.3.1",
35-
"react-dom": "^18.3.1",
36-
"typescript": "^4.6.4",
37-
"vitest": "^0.24.3"
35+
"@testing-library/react": "16.0.1",
36+
"@types/react": "18.3.3",
37+
"jsdom": "25.0.1",
38+
"mobx": "6.13.5",
39+
"mobx-react": "9.1.1",
40+
"mobx-state-tree": "6.0.1",
41+
"prettier": "3.3.3",
42+
"react": "18.3.1",
43+
"react-dom": "18.3.1",
44+
"tsup": "8.3.0",
45+
"typescript": "5.6.3",
46+
"vitest": "2.1.3"
3847
},
3948
"peerDependencies": {
40-
"mobx": "^6.0.0",
41-
"mobx-state-tree": "^5.0.0"
49+
"mobx": ">=6.0.0 <7.0.0",
50+
"mobx-state-tree": ">=5.0.0 <7.0.0",
51+
"react": ">=18.0.0 <20.0.0",
52+
"react-dom": ">=18.0.0 <20.0.0"
4253
},
4354
"dependencies": {
44-
"@wry/equality": "^0.4.0"
55+
"@wry/equality": "0.5.7"
4556
},
4657
"volta": {
4758
"node": "18.12.1"

packages/mst-query/src/MstQueryHandler.ts

+17-37
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import equal from '@wry/equality';
1+
import { equal } from '@wry/equality';
22
import { makeObservable, observable, action } from 'mobx';
33
import {
44
addDisposer,
@@ -15,7 +15,7 @@ import {
1515
recordPatches,
1616
unprotect,
1717
} from 'mobx-state-tree';
18-
import { MutationReturnType, QueryReturnType } from './create';
18+
import { MutationReturnType } from './create';
1919
import { merge } from './merge';
2020
import { QueryClient, EndpointType } from './QueryClient';
2121

@@ -35,7 +35,6 @@ type QueryHookOptions = {
3535

3636
type NotifyOptions = {
3737
onMutate?: boolean;
38-
onQueryMore?: boolean;
3938
};
4039

4140
type OnResponseOptions = {
@@ -114,7 +113,7 @@ function subscribe(target: any, options: any) {
114113

115114
export function onMutate<T extends Instance<MutationReturnType>>(
116115
target: T,
117-
callback: (data: T['data'], self: T) => void
116+
callback: (data: T['data'], self: T) => void,
118117
) {
119118
subscribe(target, {
120119
onMutate: (data: any, self: any) => {
@@ -126,20 +125,6 @@ export function onMutate<T extends Instance<MutationReturnType>>(
126125
});
127126
}
128127

129-
export function onQueryMore<T extends Instance<QueryReturnType>>(
130-
target: T,
131-
callback: (data: T['data'], self: T) => void
132-
) {
133-
subscribe(target, {
134-
onQueryMore: (data: any, self: any) => {
135-
const root = getRoot(self);
136-
unprotect(root);
137-
callback(data, self);
138-
protect(root);
139-
},
140-
});
141-
}
142-
143128
export class MstQueryHandler {
144129
isLoading = false;
145130
isRefetching = false;
@@ -148,9 +133,9 @@ export class MstQueryHandler {
148133
error: any = null;
149134
queryObservers = [] as any[];
150135

151-
result: any;
152136
options: {
153137
endpoint: EndpointType;
138+
onQueryMore?: (options: any) => void;
154139
meta?: { [key: string]: any };
155140
};
156141

@@ -184,7 +169,6 @@ export class MstQueryHandler {
184169
error: observable,
185170
hydrate: action.bound,
186171
setData: action.bound,
187-
setResult: action.bound,
188172
setError: action.bound,
189173
run: action.bound,
190174
query: action.bound,
@@ -224,9 +208,10 @@ export class MstQueryHandler {
224208
meta: options.meta ?? {},
225209
signal: this.abortController.signal,
226210
setData: this.model.setData,
211+
query: this.model,
227212
};
228213

229-
return endpoint(opts, this.model).then((result: any) => {
214+
return endpoint(opts).then((result: any) => {
230215
if (abortController?.signal.aborted || this.isDisposed) {
231216
throw new DisposedError();
232217
}
@@ -285,7 +270,7 @@ export class MstQueryHandler {
285270
query(options: any = {}): Promise<() => any> {
286271
return this.run(options).then(
287272
(result) => this.onSuccess(result),
288-
(err) => this.onError(err)
273+
(err) => this.onError(err),
289274
);
290275
}
291276

@@ -299,7 +284,7 @@ export class MstQueryHandler {
299284
}
300285
return this.run(options).then(
301286
(result) => this.onSuccess(result, { updateRecorder }),
302-
(err) => this.onError(err, { updateRecorder })
287+
(err) => this.onError(err, { updateRecorder }),
303288
);
304289
}
305290

@@ -312,7 +297,7 @@ export class MstQueryHandler {
312297

313298
return this.run(options).then(
314299
(result) => this.onSuccess(result, { shouldUpdate: false }),
315-
(err) => this.onError(err, { shouldUpdate: false })
300+
(err) => this.onError(err, { shouldUpdate: false }),
316301
);
317302
}
318303

@@ -325,7 +310,7 @@ export class MstQueryHandler {
325310

326311
return this.run(options).then(
327312
(result) => this.onSuccess(result),
328-
(err) => this.onError(err)
313+
(err) => this.onError(err),
329314
);
330315
}
331316

@@ -352,8 +337,6 @@ export class MstQueryHandler {
352337
this.markedAsStale = false;
353338
}
354339

355-
this.setResult(result);
356-
357340
let data;
358341
if (shouldUpdate) {
359342
data = this.setData(result);
@@ -376,7 +359,12 @@ export class MstQueryHandler {
376359

377360
if (this.isFetchingMore) {
378361
this.isFetchingMore = false;
379-
this.notify({ onQueryMore: true }, data, this.model);
362+
this.options.onQueryMore?.({
363+
data,
364+
pagination: this.model.variables.pagination,
365+
request: this.model.variables.request,
366+
query: this.model,
367+
});
380368
}
381369

382370
if (!this.isFetched) {
@@ -458,18 +446,10 @@ export class MstQueryHandler {
458446
}
459447
}
460448

461-
setResult(result: any) {
462-
this.result = result;
463-
}
464-
465449
setError(error: any) {
466450
this.error = error;
467451
}
468452

469-
setOptions(options: any) {
470-
this.options = { ...this.options, ...options };
471-
}
472-
473453
setVariables(variables: any) {
474454
let request = variables.request ?? EmptyRequest;
475455
let pagination = variables.pagination ?? EmptyPagination;
@@ -509,7 +489,7 @@ export class MstQueryHandler {
509489
this.model.data = merge(
510490
data,
511491
this.type.properties.data,
512-
this.queryClient.config.env
492+
this.queryClient.config.env,
513493
);
514494
}
515495
});

packages/mst-query/src/QueryClient.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export type EndpointType = (
88
meta: { [key: string]: any };
99
signal: AbortSignal;
1010
setData: (data: any) => any;
11+
query: any
1112
},
12-
model: any
1313
) => Promise<any>;
1414

1515
type QueryClientConfig<T extends IAnyModelType> = {

0 commit comments

Comments
 (0)