import type { Draft, Fn, IllegalOperatError, Result } from '../../../mod.js'; import { state_tag } from '../../Constant/mod.js'; export interface State { readonly _tag: typeof state_tag; /** State的数据 */ readonly struct: { main: M; effect: S; }; /** ### `map` : 更新main数据 */ map: (f: Fn) => State; /** ### `chain` : 更新,返回一个全新的State数据 */ chain: (f: Fn<[M, S], [O, R]>) => State; /** ### `ap` : 更新effect数据 */ ap: (f: Fn) => State; /** ### `rep` : 替换effect数据 */ rep: (effect: R) => State; /** ### `draft` : 使用{@link Immut}以 *不可变数据结构* 的形式更新数据 + 产生的新数据和旧数据是相互独立的,不会相互影响 @example Uasge ``` */ draft: (f: Fn, R>) => Result : R extends [infer A, infer B] ? State : never, IllegalOperatError | TypeError>; /** ### `unwrap` : 获取main数据*/ unwrap: () => M; /** ### `effect` : 获取effect数据*/ effect: () => S; } /** ## `State` : 主数据与(状态/副作用)数据进行隔离的数据结构 @example Usage1 : 用于简化多参数函数的调用 ```ts //正常情况下函数传入多个参数的情况 function main(info: INFO, is_man: boolean) { if_then(is_man, () => { assertEquals(info, { name: 'jiojio', age: 18 }) }) } //在main函数中,不变的是info信息,而is_man是变化的,所以可以使用State-effect来存储is_man的状态 function main_change(state: State) { if_then(state.effect(), () => { assertEquals(state.unwrap(), info) }) } ``` @example Usage2 : 配合`pipe`函数存储过程数据 ```ts //pipe函数虽然让函数调用变得十分优雅,但是有一个问题就是只能返回一个值,但是有时候我们需要返回多个值(譬如不想多次计算的计算结果,这些数据属于状态数据,对于结果没有影响但是为了提高效率要用得到),这时候就可以使用State将主要数数据和副作用数据进分隔开 function step1(info: INFO) { info.age += 1 const comput_freq = 1 return state(info, comput_freq) } function step2(state: State) { if_then(state.unwrap().name == 'jiojio', () => { main_change(state.rep(true)) }) // 更新数据,进行数据隔离 const as2 = state.draft(([main, effect]) => { //要更新数据必须先取出value值,然后进行操作 main.value.age += 1 //更新副作用数据 effect.value = 2 }) // 旧数据 assert(state.unwrap().age === 19) // 新数据 assert(as2.unwrap().age === 20) assert(as2.effect() === 2) } pipe(info, step1, step2) ``` @category TypeClass */ export declare function State(main: M, effect: S): State; //# sourceMappingURL=state.d.ts.map