-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c128370
commit c5e6921
Showing
8 changed files
with
206 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { ISharedCtx, sharex } from "helux" | ||
import { CreateComputedOptions, IReactiveable } from "./types"; | ||
import { ComputedDepends, ComputedOptions, ComputedState, Dict, RequiredComputedState, RuntimeComputedOptions, StateGetter, StateSetter, StoreDefine } from "../types"; | ||
import { setVal } from "../utils"; | ||
|
||
|
||
export class HeluxReactiveable<T extends ComputedState<Dict> =ComputedState<Dict>> implements IReactiveable<T>{ | ||
private _stateCtx:ISharedCtx<T> | ||
constructor(state:any){ | ||
this._stateCtx = sharex<T>(state) | ||
} | ||
|
||
get state(){ | ||
return this._stateCtx.state | ||
} | ||
/** | ||
* const [ state ] = useState() | ||
* const [ firstName ] = useState<string>((state)=>state.firstName) | ||
* const [ fullName ] = useState<string>((state)=>state.firstName+state.lastName) | ||
* const [ fullName,updateFullName ] = useState<string,string[]>( | ||
* (state)=>state.firstName+state.lastName, | ||
* (state,value)=>{ | ||
* state.firstName = value[0] | ||
* state.lastName = value[1] | ||
* } | ||
* ) | ||
* updateFullName(['zhang','fisher]) | ||
* | ||
* @param getter | ||
* @param setter | ||
* @returns | ||
*/ | ||
useState<Value=ComputedState<T>,SetValue=Value>( | ||
getter?:StateGetter<RequiredComputedState<T>,Value>, | ||
setter?:StateSetter<RequiredComputedState<T>,SetValue> | ||
):[Value,(value:SetValue)=>void]{ | ||
const [ state,setState ] = this._stateCtx.useState() | ||
const value = (typeof(getter)==='function' ? getter(state as RequiredComputedState<T>) : state) as Value | ||
const updateValue = (typeof(setter)=='function' ? | ||
(value:SetValue)=>{ | ||
//@ts-ignore | ||
setState((draft:any)=>{ | ||
setter.call(draft,draft as any,value) | ||
}) | ||
} : setState ) as (value:SetValue)=>void | ||
|
||
return [ value, updateValue ] | ||
} | ||
|
||
setState(fn:(draft:T)=>void):void{ | ||
//@ts-ignore | ||
this._stateCtx.setState(fn) | ||
} | ||
/** | ||
* 创建计算属性 | ||
* @param fn | ||
* @param depents | ||
* @param options | ||
*/ | ||
createComputed(params: CreateComputedOptions<T>): string { | ||
const {initial,onComputed,depends,options} = params | ||
this._stateCtx.mutate({ | ||
deps: (state: any) =>{ | ||
return depends | ||
}, | ||
fn: (draft, params) => { | ||
if (params.isFirstCall) { | ||
// @ts-ignore | ||
initial(draft, params) | ||
} | ||
}, | ||
// 此函数在依赖变化时执行,用来异步计算 | ||
// extraArgs是在调用run方法时传入的额外参数,可用来覆盖计算参数 | ||
task: async ({ draft, setState, input, extraArgs }) => { | ||
// @ts-ignore | ||
return onComputed({draft,setState,input,options:Object.assign({},extraArgs)}) | ||
}, | ||
immediate : options.immediate, | ||
desc : options.id, | ||
checkDeadCycle: false, | ||
}); | ||
return options.id as string | ||
} | ||
runComputed(id:string,options?:RuntimeComputedOptions):void{ | ||
const params = {desc:id,extraArgs:options} | ||
this._stateCtx.runMutateTask(params) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./types" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* | ||
* 对sharex的简单封装 | ||
* | ||
* @param options | ||
* @returns | ||
*/ | ||
|
||
import { ComputedDepends, ComputedOptions, ComputedState, Dict, RequiredComputedState, RuntimeComputedOptions, StateGetter, StateSetter } from "../types" | ||
|
||
|
||
export type CreateComputedOptions<T extends ComputedState<Dict> =ComputedState<Dict>>= { | ||
depends?:ComputedDepends // 依赖的计算属性 | ||
initial(state:T,params:any):void // 首次计算时执行 | ||
getter():void // 计算函数 | ||
// 当依赖变化时执行 | ||
onComputed(params:{ | ||
draft:T, | ||
setState:(state:T)=>void, | ||
input:any, | ||
options?: ComputedOptions | ||
}):void // 当依赖变化时执行 | ||
options:ComputedOptions // 计算属性的选项 | ||
} | ||
|
||
export interface IReactiveable<T extends ComputedState<Dict> =ComputedState<Dict>>{ | ||
get state():T | ||
useState<Value=T,SetValue=Value>( | ||
getter?:StateGetter<RequiredComputedState<T>,Value>, | ||
setter?:StateSetter<RequiredComputedState<T>,SetValue> | ||
):[Value,(value:SetValue)=>void] | ||
/** | ||
* | ||
* @param fn | ||
*/ | ||
setState(fn:(draft:T)=>void):void | ||
/** | ||
* 创建计算属性 | ||
*/ | ||
createComputed(params:CreateComputedOptions<T>):string | ||
/** | ||
* 手动运行计算函数 | ||
* @param id | ||
* @param options | ||
*/ | ||
runComputed(id:string,options?:RuntimeComputedOptions):void | ||
/** | ||
* 当读取响应对象时的回调 | ||
* @param params | ||
*/ | ||
onRead(params:{valuePath:string[],value:any,parent:string[], replaceValue:(newValue:any)=>void}):void | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters