Skip to content

Commit cb9b887

Browse files
authored
[optimize] Filter type supports Range fields (Number or Date) (#15)
1 parent d633176 commit cb9b887

File tree

9 files changed

+215
-243
lines changed

9 files changed

+215
-243
lines changed

ReadMe.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ import { Repository, RepositoryModel } from './Repository';
157157
export type User = components['schemas']['public-user'];
158158

159159
export class MultipleRepository extends Stream<Repository>(RepositoryModel) {
160+
declare baseURI: string;
160161
client = client;
161162

162163
async *getOrgRepos() {
@@ -214,7 +215,8 @@ Here is an example:
214215
## Wrapper
215216

216217
1. [Strapi v4](https://github.com/idea2app/MobX-RESTful/blob/main/wrapper/Strapi)
217-
2. [Lark/FeiShu](https://github.com/idea2app/MobX-Lark)
218+
2. [GitHub](https://github.com/idea2app/MobX-GitHub)
219+
3. [Lark/FeiShu](https://github.com/idea2app/MobX-Lark)
218220

219221
## Component
220222

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mobx-restful",
3-
"version": "1.0.0-rc.5",
3+
"version": "1.0.0",
44
"license": "LGPL-3.0",
55
"author": "[email protected]",
66
"description": "MobX SDK for RESTful API",
@@ -24,11 +24,12 @@
2424
},
2525
"source": "source/index.ts",
2626
"types": "dist/index.d.ts",
27+
"module": "dist/index.esm.js",
2728
"main": "dist/index.js",
2829
"dependencies": {
2930
"@swc/helpers": "^0.5.11",
3031
"idb-keyval": "^6.2.1",
31-
"koajax": "^1.1.0",
32+
"koajax": "^1.1.2",
3233
"native-file-system-adapter": "^3.0.1",
3334
"regenerator-runtime": "^0.14.1",
3435
"web-streams-polyfill": "^4.0.0",
@@ -44,7 +45,7 @@
4445
"@parcel/transformer-typescript-tsc": "~2.12.0",
4546
"@parcel/transformer-typescript-types": "~2.12.0",
4647
"@types/jest": "^29.5.12",
47-
"@types/node": "^18.19.33",
48+
"@types/node": "^18.19.34",
4849
"dotenv": "^16.4.5",
4950
"husky": "^9.0.11",
5051
"jest": "^29.7.0",
@@ -53,10 +54,10 @@
5354
"mobx": "^6.12.3",
5455
"open-cli": "^8.0.0",
5556
"parcel": "~2.12.0",
56-
"prettier": "^3.2.5",
57+
"prettier": "^3.3.1",
5758
"ts-jest": "^29.1.4",
5859
"typedoc": "^0.25.13",
59-
"typedoc-plugin-mdn-links": "^3.1.27",
60+
"typedoc-plugin-mdn-links": "^3.1.28",
6061
"typescript": "~5.5.0-beta"
6162
},
6263
"prettier": {

pnpm-lock.yaml

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

source/Downloader/HTTP.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ export class HTTPDownloadTask extends DownloadTask {
2121
const { path } = this;
2222
const suggestedName = DownloadTask.nameOf(path);
2323

24-
try {
25-
this.fsHandle ||= await (
26-
await import('native-file-system-adapter')
27-
).showSaveFilePicker({ suggestedName });
28-
} catch {
29-
return;
30-
}
24+
this.fsHandle ||= await (
25+
await import('native-file-system-adapter')
26+
).showSaveFilePicker({ suggestedName });
27+
3128
const writer = await (
3229
this.fsHandle as FileSystemFileHandle
3330
).createWritable({

source/Downloader/Task.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {
44
HTTPClient,
55
TransferProgress
66
} from 'koajax';
7-
import { computed, observable, reaction } from 'mobx';
7+
import { computed, observable } from 'mobx';
88
import type { FileSystemHandle } from 'native-file-system-adapter';
99
import type { ReadableStream } from 'web-streams-polyfill';
1010
import { ByteSize } from 'web-utility';
@@ -97,16 +97,27 @@ export abstract class DownloadTask implements Partial<TransferProgress> {
9797
async start(options = this.options) {
9898
this.options = options;
9999

100+
const started = Promise.withResolvers<void>();
101+
100102
const [innerStream, outerStream] = (
101103
await import('web-streams-polyfill')
102104
).ReadableStream.from<Partial<TransferProgress>>(
103105
this.loadStream(options)
104106
).tee();
105107

106108
(async () => {
107-
for await (const chunk of innerStream) console.table(chunk);
109+
try {
110+
for await (const chunk of innerStream) {
111+
started.resolve();
112+
console.table(chunk);
113+
}
114+
} catch (error) {
115+
started.reject(error);
116+
}
108117
})();
109118

119+
await started.promise;
120+
110121
return (this.stream = outerStream);
111122
}
112123

@@ -119,11 +130,4 @@ export abstract class DownloadTask implements Partial<TransferProgress> {
119130

120131
return destroy(this, this.id);
121132
}
122-
123-
onFinished(callback: (task: DownloadTask) => any) {
124-
return reaction(
125-
() => this.percent === 100,
126-
() => callback(this)
127-
);
128-
}
129133
}

source/Downloader/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,14 @@ export class Downloader {
4848
}
4949

5050
createTask(path: string, name?: string) {
51-
const { tasks } = this,
52-
task = Downloader.createTask(path, name);
51+
const { tasks } = this;
52+
const sameTask = tasks.find(task => task.path === path);
53+
54+
if (sameTask) return sameTask;
55+
56+
const task = Downloader.createTask(path, name);
5357

54-
if (!tasks.find(task => task.path === path))
55-
this.tasks = [...tasks, task];
58+
this.tasks = [...tasks, task];
5659

5760
return task;
5861
}

source/List.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ export abstract class ListModel<
7171

7272
@computed
7373
get noMore() {
74-
const { totalCount, allItems } = this;
75-
76-
return allItems.length >= totalCount;
74+
return this.pageIndex * this.pageSize >= this.totalCount;
7775
}
7876

7977
@observable
@@ -135,7 +133,7 @@ export abstract class ListModel<
135133
* @protected
136134
*/
137135
async loadNewPage(pageIndex: number, pageSize: number, filter: F) {
138-
const { pageData, totalCount = Infinity } = await this.loadPage(
136+
const { pageData, totalCount } = await this.loadPage(
139137
pageIndex,
140138
pageSize,
141139
filter
@@ -146,7 +144,12 @@ export abstract class ListModel<
146144
list[pageIndex - 1] = pageData;
147145
this.pageList = list;
148146

149-
this.totalCount ||= totalCount;
147+
this.totalCount =
148+
totalCount != null
149+
? isNaN(totalCount) || totalCount < 0
150+
? Infinity
151+
: totalCount
152+
: Infinity;
150153

151154
return { pageData, totalCount };
152155
}
@@ -318,8 +321,6 @@ export function Stream<
318321
M extends AbstractClass<ListModel<D, F>> = AbstractClass<ListModel<D, F>>
319322
>(Super: M) {
320323
abstract class StreamListMixin extends Super {
321-
baseURI = '';
322-
323324
stream?: AsyncGenerator<D, void, any>;
324325
abstract openStream(filter: F): AsyncGenerator<D, void, any>;
325326

test/List.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ describe('List model', () => {
129129
class MultipleRepositoryModel extends Stream<Repository>(
130130
RepositoryModel
131131
) {
132+
declare baseURI: string;
132133
client = client;
133134

134135
async *getOrgRepos() {

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"downlevelIteration": true,
88
"useDefineForClassFields": true,
99
"skipLibCheck": true,
10-
"lib": ["ES2023", "DOM", "DOM.Iterable"],
10+
"lib": ["ESNext", "DOM", "DOM.Iterable"],
1111
"declaration": true,
1212
"outDir": "dist"
1313
},

0 commit comments

Comments
 (0)