11/**
22 *
3- * 异步计算
4- *
5- *
3+ * 轻量异步计算
64 *
5+ * 计算结果直接写入到原地
76 *
7+ * 没有进度条、超时、可中止、倒计时、重试等高级功能
88 *
99 */
10- import { getAbsolutePath , isPathEq , setVal } from "../utils" ;
10+ import { isPathEq } from "../utils" ;
1111import { getValueScope } from "../scope" ;
1212import { ComputedObject } from "./computedObject" ;
1313import { getSnap } from "../utils/getSnap" ;
1414import type { StateOperate } from "../store/types" ;
15- import {
16- AsyncLiteComputedGetterArgs ,
17- LiteAsyncComputedOptions ,
18- RuntimeComputedOptions ,
19- } from "./types" ;
15+ import { AsyncLiteComputedGetterArgs , ComputedOptions , RuntimeComputedOptions } from "./types" ;
2016
2117export 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 /**
0 commit comments