Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfisher committed Jun 26, 2024
1 parent 3cd9aa2 commit 349f9e1
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 20 deletions.
93 changes: 90 additions & 3 deletions packages/reactive/src/__tests__/async/dynamic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { createStore,ComputedScopeRef,computed, IStore } from "../.."

describe("动态创建同步择计算属性",()=>{


test("创建同步计算属性提供默认值",()=>{
return new Promise<void>(resolve=>{
const store = createStore({
Expand All @@ -23,12 +22,12 @@ describe("动态创建同步择计算属性",()=>{
})
const obj = store.createComputed((scope:any)=>{
return scope.price * scope.count
},['price','count'])
})
expect(obj.value).toBe(6)
resolve()
})
})
test("动态创建的同步计算对象",()=>{
test("动态创建的同步计算对象-默认没有保存计算对象引用",()=>{
return new Promise<void>(resolve=>{
const store = createStore({
price:2,
Expand All @@ -38,6 +37,21 @@ describe("动态创建同步择计算属性",()=>{
return scope.price * scope.count
})
expect(obj.value).toBe(6)
expect(store.computedObjects.size).toBe(0)
expect(store.computedObjects.has(obj.id)).toBe(false)
resolve()
})
})
test("动态创建的同步计算对象,指定保存计算对象引用",()=>{
return new Promise<void>(resolve=>{
const store = createStore({
price:2,
count:3
})
const obj = store.createComputed<number>((scope:any)=>{
return scope.price * scope.count
},{id:"x",save:true})
expect(obj.value).toBe(6)
expect(store.computedObjects.size).toBe(1)
expect(store.computedObjects.has(obj.id)).toBe(true)
resolve()
Expand Down Expand Up @@ -71,4 +85,77 @@ describe("动态创建同步择计算属性",()=>{
})
})

})



describe("动态创建异步计算属性",()=>{

test("创建异步计算属性提供默认值",()=>{
return new Promise<void>(resolve=>{
const store = createStore({
price:2,
count:3
})
store.on("computed:created",()=>{
expect(obj.value.result).toBe(10)
resolve()
})
const obj = store.createComputed(async (scope:any)=>{
return scope.price * scope.count
},["price","count"],{initial:10})
})
})
test("动态创建的异步计算对象-默认没有保存计算对象引用",()=>{
return new Promise<void>(resolve=>{
const store = createStore({
price:2,
count:3
})
const obj = store.createComputed<number>(async (scope:any)=>{
return scope.price * scope.count
},["price","count"],{initial:10})
store.on("computed:created",()=>{
expect(obj.value.result).toBe(10)
expect(store.computedObjects.size).toBe(0)
expect(store.computedObjects.has(obj.id)).toBe(false)
resolve()
})
})
})
test("动态创建的异步计算对象,指定保存计算对象引用",()=>{
return new Promise<void>(resolve=>{
const store = createStore({
price:2,
count:3
})
store.on("computed:created",()=>{
expect(obj.value.result).toBe(6)
expect(store.computedObjects.size).toBe(1)
expect(store.computedObjects.has(obj.id)).toBe(true)
resolve()})
const obj = store.createComputed<number>(async (scope:any)=>{
return scope.price * scope.count
},["price","count"],{id:"x",save:true,initial:6})
})
})
test("动态异步计算属性依赖变化时自动更新",()=>{
return new Promise<void>(resolve=>{
const store = createStore({
price:2,
count:3
})
store.on("computed:done",()=>{
expect(obj.value.result).toBe(6)
// resolve()
// expect(obj.value.result).toBe(8)
// resolve()
})
const obj = store.createComputed(async (scope:any)=>{
return scope.price * scope.count
},["price","count"])
store.setState((draft)=>draft.count = 4)
})
})

})
10 changes: 10 additions & 0 deletions packages/reactive/src/__tests__/async/funcs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@
* 测试计算属性的getter的第二个参数的各项功能
*
*/


import { test,expect, describe, beforeAll } from "vitest"
import { createStore,ComputedScopeRef,computed, IStore } from "../.."



describe("异步计算控制功能",()=>{
test("执时超时",()=>{})
})
6 changes: 3 additions & 3 deletions packages/reactive/src/computed/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async function executeComputedGetter<T extends StoreDefine>(draft:any,computedRu

const { id,valuePath,getter,resultPath,dependValues } = computedRunContext;
const { timeout=0,retry=[0,0],selfReactiveable } = computedOptions
const setState = selfReactiveable ? selfReactiveable.setState : store.setState
const setState = selfReactiveable ? selfReactiveable.setState.bind(selfReactiveable) : store.setState
//
const thisDraft = draft
const scopeDraft = getComputedScope(store,computedOptions,{draft,dependValues,valuePath,computedType:"Computed"} )
Expand Down Expand Up @@ -184,7 +184,7 @@ function createComputed<T extends StoreDefine>(computedRunContext:ComputedRunCon
if(selfReactiveable){
// @ts-ignore
selfReactiveable.setState((draft)=>{
Object.assign(draft,createAsyncComputedObject(store,computedId,{result: initial}))
setVal(draft, valuePath, createAsyncComputedObject(store, computedId,{result: initial}))
})
}else{
setVal(draft, valuePath, createAsyncComputedObject(store, computedId,{result: initial}))
Expand Down Expand Up @@ -272,7 +272,7 @@ export function createAsyncComputedMutate<T extends StoreDefine,R=any>(computed

// 8. 创建计算对象实例
const computedObject = new ComputedObject<T,AsyncComputedObject<R>>(store,selfReactiveable,valuePath,computedOptions)
store.computedObjects.set(computedId,computedObject)
if(computedOptions.save) store.computedObjects.set(computedId,computedObject)
return computedObject
}

Expand Down
1 change: 1 addition & 0 deletions packages/reactive/src/computed/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function computed<R = any,ExtraAttrs extends Dict = {}>( getter: any,depe
timeout : 0,
depends : [],
immediate : true,
save : true
}

if(arguments.length==1){
Expand Down
3 changes: 1 addition & 2 deletions packages/reactive/src/computed/computedObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Reactiveable } from "../reactives/types"
import { IStore, Dict } from "../types"
import { getComputedId } from "../utils/getComputedId"
import { IStore, Dict } from "../types"
import { getVal } from "../utils/getVal"
import { ComputedOptions, RuntimeComputedOptions } from "./types"

Expand Down
6 changes: 3 additions & 3 deletions packages/reactive/src/computed/computedObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { IStore, StoreDefine } from "../store/types";
*
*/
export class ComputedObjects<T extends StoreDefine = StoreDefine> extends Map<string,ComputedObject<T>>{
private _createComputed?:ReturnType<typeof computedObjectCreator>
private _createComputed?:ReturnType<typeof computedObjectCreator<T>>
constructor(public store:IStore<T>){
super()
}
Expand Down Expand Up @@ -47,9 +47,9 @@ export class ComputedObjects<T extends StoreDefine = StoreDefine> extends Map<s
/**
* 创建一个新计算对象
*/
get create():ReturnType<typeof computedObjectCreator>{
get create():ReturnType<typeof computedObjectCreator<T>>{
if(!this._createComputed){
this._createComputed = computedObjectCreator(this.store)
this._createComputed = computedObjectCreator<T>(this.store)
}
return this._createComputed
}
Expand Down
14 changes: 7 additions & 7 deletions packages/reactive/src/computed/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { StoreDefine } from "../store/types"
import { ComputedDepends, ComputedGetter, Dict, IStore } from "../types"
import { AsyncComputedObject, ComputedDepends, ComputedGetter, Dict, IStore } from "../types"
import { AsyncComputedGetter, ComputedOptions, ComputedParams, IComputeParams } from "../computed/types"
import { computed } from "./computed"
import { installComputed } from "./install"
Expand All @@ -21,9 +21,8 @@ import { isPlainObject } from 'flex-tools/typecheck/isPlainObject';


export type ComputedObjectCreateOptions<R = any,ExtraAttrs extends Dict = {}> = ComputedOptions<R,ExtraAttrs> & {
id:string // 必须指定一个id
depends: string[] // 依赖的字段

id?:string // 必须指定一个id
depends?: string[] // 依赖的字段
}

/**
Expand Down Expand Up @@ -59,13 +58,14 @@ export type ComputedObjectCreateOptions<R = any,ExtraAttrs extends Dict = {}> =
*
*
*/
export function computedObjectCreator<T extends StoreDefine>(store:IStore<T>){
export function computedObjectCreator<T extends StoreDefine = StoreDefine>(store:IStore<T>){
function creatorComputedObject<R = any>(getter:ComputedGetter<R>,options?:ComputedObjectCreateOptions<R>):ComputedObject<T,R>
function creatorComputedObject<R = any>(getter:AsyncComputedGetter<R>,depends:ComputedDepends,options?:ComputedObjectCreateOptions<R>):ComputedObject<T,R>
function creatorComputedObject<R = any>(getter:AsyncComputedGetter<R>,depends:ComputedDepends,options?:ComputedObjectCreateOptions<R>):ComputedObject<T,AsyncComputedObject<R>>
function creatorComputedObject<R = any>(getter:any,depends:any,options?:any){

let opts =Object.assign({
id:getRndId(),
save:false
},isPlainObject(arguments[1]) ? arguments[1] : arguments[2]) as Required<ComputedOptions<R>>
opts.depends = Array.isArray(arguments[1]) ? arguments[1] : []
const isAsync = opts.async===true || isAsyncFunction(getter)
Expand All @@ -74,7 +74,7 @@ export function computedObjectCreator<T extends StoreDefine>(store:IStore<T>){
opts.selfReactiveable = new HeluxReactiveable<Dict>({
value: isAsync ? createAsyncComputedObject(store,opts.id,{}) : opts.initial
})

let computedParams:IReactiveReadHookParams
if(opts.async){
computedParams = {
Expand Down
4 changes: 3 additions & 1 deletion packages/reactive/src/computed/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export function installComputed<T extends StoreDefine,R=any>(params:IReactiveRea
}
// 当创建计算完毕后的回调
if(computedObject){
store.emit("computed:created",computedObject as any)
setTimeout(()=>{
store.emit("computed:created",computedObject as any)
})
}
return computedObject
}
2 changes: 1 addition & 1 deletion packages/reactive/src/computed/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,6 @@ export function createComputedMutate<T extends StoreDefine,R=any>(computedParam

// 5. 创建计算对象实例
const computedObject = new ComputedObject<T,R>(store,selfReactiveable,valuePath,computedOptions)
store.computedObjects.set(computedId,computedObject)
if(computedOptions.save) store.computedObjects.set(computedId,computedObject)
return computedObject
}
9 changes: 9 additions & 0 deletions packages/reactive/src/computed/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ export interface ComputedProgressbar{
*
*/
selfReactiveable?: Reactiveable
/**
*
* 当创建计算属性的computedObject是否保存到store.computedObjects中
*
* 当在hook中使用时就不需要保存到store.computedObjects中
*
*/
save?:boolean

};

export type ComputedDepends =Array<string | Array<string>>
Expand Down

0 comments on commit 349f9e1

Please sign in to comment.