Skip to content

Commit 80ac008

Browse files
committed
update
1 parent ea85497 commit 80ac008

4 files changed

Lines changed: 94 additions & 81 deletions

File tree

packages/core/src/computed/async.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ export class AsyncComputedObject<Value = any, Scope = any> extends ComputedObjec
3636
private _defaultAbortController: AbortController | null = null;
3737
private _userAbortController?: AbortController;
3838
private _firstRun: boolean = false; // 是否已经第一次运行过
39+
/**
40+
* 用于标识这是一个简单计算对象
41+
*
42+
* 不同于全功能异步计算对象
43+
*
44+
*/
45+
lite: boolean = false;
3946
get async() {
4047
return true;
4148
}

packages/core/src/computed/computedObject.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ export class ComputedObject<
4343
get val(): Value {
4444
return this.async ? (this.value as any).value : this.value;
4545
}
46+
/**
47+
* 报告计算状态
48+
* @param name
49+
* @param value
50+
*/
51+
protected _reportComputedStatus(name: "loading" | "error", value: any) {
52+
// @ts-ignore
53+
if (!this[`_${name}`]) {
54+
const reports: Record<string, any> = this.options.reports || {};
55+
const path = reports[name];
56+
if (Array.isArray(path) && path.length > 0) {
57+
// @ts-ignore
58+
this[`_${name}`] = getAbsolutePath(path, this.path);
59+
}
60+
} // @ts-ignore
61+
if (Array.isArray(this[`_${name}`])) {
62+
this.store.update((state) => {
63+
// @ts-ignore
64+
setVal(state, this[`_${name}`]!, value);
65+
});
66+
}
67+
}
4668
/**
4769
* 检查计算函数是否被禁用
4870
*

packages/core/src/computed/liteAsync.ts

Lines changed: 37 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
/**
22
*
3-
* 异步计算
4-
*
5-
*
3+
* 轻量异步计算
64
*
5+
* 计算结果直接写入到原地
76
*
7+
* 没有进度条、超时、可中止、倒计时、重试等高级功能
88
*
99
*/
10-
import { getAbsolutePath, isPathEq, setVal } from "../utils";
10+
import { isPathEq } from "../utils";
1111
import { getValueScope } from "../scope";
1212
import { ComputedObject } from "./computedObject";
1313
import { getSnap } from "../utils/getSnap";
1414
import type { StateOperate } from "../store/types";
15-
import {
16-
AsyncLiteComputedGetterArgs,
17-
LiteAsyncComputedOptions,
18-
RuntimeComputedOptions,
19-
} from "./types";
15+
import { AsyncLiteComputedGetterArgs, ComputedOptions, RuntimeComputedOptions } from "./types";
2016

2117
export class AsyncLiteComputedObject<Value = any, Scope = any> extends ComputedObject<
2218
Value,
23-
LiteAsyncComputedOptions
19+
ComputedOptions
2420
> {
2521
private _isRunning: boolean = false;
2622
private _firstRun: boolean = false; // 是否已经第一次运行过
23+
lite: boolean = true; // 标识这是一个简单计算对象
2724
get async() {
2825
return true;
2926
}
@@ -100,23 +97,6 @@ export class AsyncLiteComputedObject<Value = any, Scope = any> extends ComputedO
10097
this._isRunning = false;
10198
}
10299
}
103-
private _updateComputeContext(name: "loading" | "error", value: any) {
104-
// @ts-ignore
105-
if (!this[`_${name}`]) {
106-
// @ts-ignore
107-
const path = this.options[`${name}Path`];
108-
if (Array.isArray(path) && path.length > 0) {
109-
// @ts-ignore
110-
this[`_${name}`] = getAbsolutePath(path, this.path);
111-
}
112-
} // @ts-ignore
113-
if (Array.isArray(this[`_${name}`])) {
114-
this.store.update((state) => {
115-
// @ts-ignore
116-
setVal(state, this[`_${name}`]!, value);
117-
});
118-
}
119-
}
120100
/**
121101
* 执行计算函数
122102
*
@@ -133,41 +113,38 @@ export class AsyncLiteComputedObject<Value = any, Scope = any> extends ComputedO
133113
let computedResult: any;
134114

135115
try {
136-
try {
137-
//
138-
this._updateComputeContext("loading", true);
139-
// 执行计算函数
140-
computedResult = await this.getter.call(this, scope, getterArgs);
141-
this.store.peep(() => {
142-
this.value = computedResult; // 将结果回写入store,且不触发get事件
143-
});
144-
this._updateComputeContext("error", undefined);
145-
} catch (e: any) {
146-
hasError = e;
147-
this._updateComputeContext("error", e.message);
148-
} finally {
149-
this._updateComputeContext("loading", false);
150-
}
151-
// 计算完成后触发事件
152-
if (hasError) {
153-
this.error = hasError;
154-
this.emitStoreEvent("computed:error", {
155-
path: this.path,
156-
id: this.id,
157-
error: hasError,
158-
computedObject: this,
159-
});
160-
} else {
161-
this.emitStoreEvent("computed:done", {
162-
path: this.path,
163-
id: this.id,
164-
value: computedResult,
165-
computedObject: this,
166-
});
167-
}
168-
this.onDoneCallback(options, computedResult, scope, computedResult);
116+
//
117+
this._reportComputedStatus("loading", true);
118+
// 执行计算函数
119+
computedResult = await this.getter.call(this, scope, getterArgs);
120+
this.store.peep(() => {
121+
this.value = computedResult; // 将结果回写入store,且不触发get事件
122+
});
123+
this._reportComputedStatus("error", undefined);
124+
} catch (e: any) {
125+
hasError = e;
126+
this._reportComputedStatus("error", e.message);
169127
} finally {
128+
this._reportComputedStatus("loading", false);
129+
}
130+
// 计算完成后触发事件
131+
if (hasError) {
132+
this.error = hasError;
133+
this.emitStoreEvent("computed:error", {
134+
path: this.path,
135+
id: this.id,
136+
error: hasError,
137+
computedObject: this,
138+
});
139+
} else {
140+
this.emitStoreEvent("computed:done", {
141+
path: this.path,
142+
id: this.id,
143+
value: computedResult,
144+
computedObject: this,
145+
});
170146
}
147+
this.onDoneCallback(options, computedResult, scope, computedResult);
171148
}
172149

173150
/**

packages/core/src/computed/types.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,24 @@ export interface ComputedOptions<
263263
scope: Scope;
264264
value: any;
265265
}): void;
266+
/**
267+
* 在计算前后向指定路径的状态写入额外值
268+
*
269+
*/
270+
reports?: {
271+
/**
272+
* 在计算前后向loading指定状态写入true/false用于反馈
273+
*
274+
* 如loading=["./loading"],则在开始计算前往当前计算属性所在的对象的loading=true
275+
* 计算完成后置loading=false
276+
*
277+
*/
278+
loading?: string | string[];
279+
/**
280+
* 在计算出错时向指定路径写入错误信息
281+
*/
282+
error?: string | string[];
283+
};
266284
}
267285

268286
export type AsyncComputedValue<Value = any> = {
@@ -330,24 +348,6 @@ export type SyncComputedDescriptorBuilder<Value = any, Scope = any> = ObserverDe
330348

331349
// 简单异步计算
332350

333-
export interface LiteAsyncComputedOptions {
334-
/**
335-
* 当开始执行异步计算时用于指定在此处写入true
336-
* 执行完成后写入false
337-
*
338-
*
339-
* 支持相对路径写法,
340-
* loadingPath: ['./loading'] 代表在当前同一对象的loading写入
341-
*
342-
*/
343-
loadingPath?: string[] | string;
344-
/**
345-
* 当执行异步计算出错时在此处写入错误信息
346-
* 支持相对路径写法,
347-
*/
348-
errorPath?: string[] | string;
349-
}
350-
351351
export interface AsyncLiteComputedGetterArgs {
352352
/**
353353
* 提供一个函数用来获取当前Scope的快照
@@ -367,22 +367,24 @@ export interface AsyncLiteComputedGetterArgs {
367367
*/
368368
first?: boolean;
369369
}
370+
370371
export type AsyncLiteComputedGetter<Value, Scope = any> = (
371372
scope: Scope,
372373
args: Required<AsyncLiteComputedGetterArgs>,
373374
) => Promise<Value>;
375+
374376
export type AsyncLiteComputedDescriptor<Value = any, Scope = any> = ObserverDescriptor<
375377
"computed",
376378
Value,
377379
Scope,
378380
AsyncLiteComputedGetter<Value, Scope>,
379-
LiteAsyncComputedOptions & ComputedOptions<Value, Scope>
381+
ComputedOptions<Value, Scope>
380382
>;
381383

382384
export type AsyncLiteComputedDescriptorBuilder<
383385
Value = any,
384386
Scope = any,
385-
> = ObserverDescriptorBuilder<"computed", Value, Scope, AsyncComputedDescriptor<Value, Scope>>;
387+
> = ObserverDescriptorBuilder<"computed", Value, Scope, AsyncLiteComputedDescriptor<Value, Scope>>;
386388

387389
// 全功能异步计算
388390
export type AsyncComputedDescriptor<Value = any, Scope = any> = ObserverDescriptor<
@@ -402,17 +404,22 @@ export type AsyncComputedDescriptorBuilder<Value = any, Scope = any> = ObserverD
402404

403405
export type ComputedDescriptor<Value = any, Scope = any> =
404406
| SyncComputedDescriptor<Value, Scope>
407+
| AsyncLiteComputedDescriptor<Value, Scope>
405408
| AsyncComputedDescriptor<Value, Scope>;
409+
406410
export type ComputedDescriptorBuilder<Value = any, Scope = any> =
407411
| SyncComputedDescriptorBuilder<Value, Scope>
412+
| AsyncLiteComputedDescriptorBuilder<Value, Scope>
408413
| AsyncComputedDescriptorBuilder<Value, Scope>;
409414

410415
export type ComputedDescriptorParameter<Value = any, Scope = any> =
411416
| ComputedDescriptorBuilder<Value, Scope>
412417
| ComputedGetter<Value, Scope>
413-
| AsyncComputedGetter<Value, Scope>;
418+
| AsyncComputedGetter<Value, Scope>
419+
| AsyncLiteComputedGetter<Value, Scope>;
414420

415421
export type ComputedBuilder<Value, Scope> =
416422
| ComputedDescriptorBuilder<Value, Scope>
417423
| AsyncComputedGetter<Value, Scope>
424+
| AsyncLiteComputedGetter<Value, Scope>
418425
| ComputedGetter<Value, Scope>;

0 commit comments

Comments
 (0)