Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfisher committed Jun 30, 2024
1 parent 71ace44 commit ce5e1e1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
35 changes: 34 additions & 1 deletion packages/reactive/src/__tests__/async/funcs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,42 @@

import { test,expect, describe, beforeAll } from "vitest"
import { createStore,ComputedScopeRef,computed, IStore } from "../.."
import { delay } from "flex-tools/async/delay"



describe("异步计算控制功能",()=>{
test("执时超时",()=>{})
// 注意:重入时仅会被忽略而不是产生错误
test("控制计算函数的执行的不允许重入执行",()=>{
let cancelCount:number =0
let calcCount:number = 0
return new Promise<void>((resolve)=>{
const store = createStore({
price:2,
count:3,
total:computed(async (scope)=>{
calcCount++
await delay(1000)
return scope.price * scope.count
},['price','count'],{ noReentry:true})
})
store.on("computed:cancel",()=>{
cancelCount++
if(cancelCount===9){
expect(calcCount).toBe(1)
resolve()
}
})
store.on("computed:created",()=>{
// 连接执行多次依赖更新,但是由于noReentry=false,所以只会执行一次,其它的会被忽略
setTimeout(()=>{
for(let i=0;i<10;i++){
store.setState((draft)=>draft.count += i)
}
})
})
store.state.total
})

})
})
9 changes: 6 additions & 3 deletions packages/reactive/src/computed/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ async function executeComputedGetter<T extends StoreDefine>(draft:any,computedRu


function createComputed<T extends StoreDefine>(computedRunContext:ComputedRunContext,computedOptions:ComputedOptions,store:IStore<T>){
const { valuePath, id:computedId,deps,desc:computedDesc,isComputedRunning: isMutateRunning } = computedRunContext
const { valuePath, id:computedId,deps,desc:computedDesc } = computedRunContext
const { selfReactiveable: selfReactiveable,initial,noReentry } = computedOptions

store.reactiveable.createAsyncComputed({
Expand All @@ -201,8 +201,11 @@ function createComputed<T extends StoreDefine>(computedRunContext:ComputedRunCon
store.options.log(`Run async computed for : ${computedDesc}`);

const finalComputedOptions = Object.assign({},computedOptions,options) as Required<ComputedOptions>
if(noReentry && isMutateRunning && store.options.debug) {
store.options.log(`Reentry async computed: ${computedDesc}`,'warn');
if(noReentry && computedRunContext.isComputedRunning) {
if( store.options.debug){
store.options.log(`Reentry async computed: ${computedDesc}`,'warn');
}
store.emit("computed:cancel",{path:valuePath,id:computedId,reason:"reentry"});
return
}
computedRunContext.isComputedRunning=true
Expand Down
3 changes: 2 additions & 1 deletion packages/reactive/src/store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ export interface StoreOptions<T extends StoreDefine= StoreDefine>{
export type StoreEvents = {
created : undefined; // 响应对象创建后
'computed:created' : ComputedObject // 当计算对象创建时
'computed:done' : {path:string[],id:string,value:any} // 当计算函数执行成功后
'computed:done' : {path:string[],id:string,value:any} // 当计算函数执行成功后
'computed:error' : {path:string[],id:string,error:any} // 当计算函数执行出错时
'computed:cancel' : {path:string[],id:string,reason:string} // 当计算函数被取消时
};


Expand Down

0 comments on commit ce5e1e1

Please sign in to comment.