-
-
Notifications
You must be signed in to change notification settings - Fork 70
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
d51fd26
commit c59a8ba
Showing
13 changed files
with
191 additions
and
72 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
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 |
---|---|---|
@@ -1,25 +1,39 @@ | ||
import { isFn } from '@helux/utils'; | ||
import type { Atom, DeriveFn, DeriveFnItem, ICreateDeriveLogicOptions, PlainObject } from '../types/base'; | ||
import type { Atom, DeriveFn, ICreateDeriveLogicOptions, IDeriveFnItem, IDeriveTaskOptions, PlainObject } from '../types/base'; | ||
import { initDeriveFn } from './common/derived'; | ||
|
||
export function createDeriveLogic<T extends any = any>(fn: DeriveFn<T> | DeriveFnItem, options?: ICreateDeriveLogicOptions) { | ||
export function createDeriveLogic<T extends any = any>(fn: DeriveFn<T> | IDeriveFnItem, options?: ICreateDeriveLogicOptions) { | ||
const fnItem = isFn(fn) ? { fn } : fn || {}; | ||
const fnCtx = initDeriveFn({ ...(options || {}), ...fnItem }); | ||
return fnCtx; | ||
} | ||
|
||
/** | ||
* 创建派生结果 | ||
* 创建派生结果,自动装箱为 { val: T } | ||
*/ | ||
export function derive<T = any>(deriveFn: DeriveFn<T> | DeriveFnItem): Atom<T> { | ||
export function derive<T = any>(deriveFn: DeriveFn<T> | IDeriveFnItem): Atom<T> { | ||
const fnCtx = createDeriveLogic<T>(deriveFn, { forAtom: true }); | ||
return fnCtx.proxyResult as Atom<T>; | ||
} | ||
|
||
/** | ||
* 创建一个返回值为 dict 数据的派生结果,区别于 derive 接口,此接口无自动装箱行为 | ||
*/ | ||
export function deriveDict<T = PlainObject>(deriveFn: DeriveFn<T> | DeriveFnItem): T { | ||
export function deriveDict<T = PlainObject>(deriveFn: DeriveFn<T> | IDeriveFnItem): T { | ||
const fnCtx = createDeriveLogic<T>(deriveFn); | ||
return fnCtx.proxyResult as T; | ||
} | ||
|
||
/** | ||
* 采用柯里化方式生成一个 deriveFnItem ,方便自动推导 deps 返回类型给 IDeriveFnParams | ||
*/ | ||
export function defineDeriveTask(deps?: () => any[]) { | ||
return (options: IDeriveTaskOptions): IDeriveFnItem => ({ ...options, deps }); | ||
} | ||
|
||
/** | ||
* 辅助给直接透传给 defineFullDerive 的某个 deriveFnItem 标记类型 | ||
*/ | ||
export function defineDeriveFnItem<F extends IDeriveFnItem>(fnItem: F): F { | ||
return fnItem; | ||
} |
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
Oops, something went wrong.