You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.9 KiB
85 lines
2.9 KiB
|
|
export type Constructor<T> = new(...args: any[]) => T; |
|
|
|
/** |
|
* 注册widget |
|
*/ |
|
export function shortcut() { |
|
return function decorator<U>(Target: Constructor<U> & {xtype: string}): void { |
|
BI.shortcut(Target.xtype, Target); |
|
}; |
|
} |
|
|
|
/** |
|
* 注册model |
|
*/ |
|
export function model() { |
|
return function decorator<U extends {new(...args:any[]):{}} & {xtype: string, context?: ReadonlyArray<string>}>(Target: U): void { |
|
BI.model(Target.xtype, Target); |
|
}; |
|
} |
|
|
|
/** |
|
* 类注册_store属性 |
|
* @param Model model类 |
|
* @param opts 额外条件 |
|
*/ |
|
export function store<T>(Model?: Constructor<T> & {xtype: string}, opts: { props?(this: unknown): { [key: string]: unknown } } = {}) { |
|
return function classDecorator<U extends {new(...args:any[]):{}}>(constructor:U) { |
|
return class extends constructor { |
|
_store() { |
|
const props = opts.props ? opts.props.apply(this) : undefined; |
|
|
|
return BI.Models.getModel(Model.xtype, props); |
|
} |
|
}; |
|
}; |
|
} |
|
|
|
/** |
|
* Model基类 |
|
*/ |
|
export class Model<U extends {types?: {[key: string]: unknown} | {}, context?: ReadonlyArray<string>} = {}> extends Fix.Model { |
|
// @ts-ignore this['computed'][key]为空 |
|
model: Pick<{[key in keyof U['types']]: U['types'][key]}, U['context'][number]> & {[key in keyof ReturnType<this['state']>]: ReturnType<this['state']>[key]} & {[key in keyof this['computed']]: ReturnType<this['computed'][key]>}; |
|
|
|
store: this['actions']; |
|
|
|
state(): {[key: string]: unknown} | {} { |
|
return {}; |
|
} |
|
|
|
context: U['context']; |
|
|
|
actions:{[key: string]: (...args: any[]) => any}; |
|
|
|
childContext: ReadonlyArray<keyof (this['computed'] & ReturnType<this['state']>)>; |
|
|
|
// @ts-ignore this['computed'][key]为空 |
|
TYPE: Pick<{[key in keyof this['computed']]: ReturnType<this['computed'][key]>} & {[key in keyof ReturnType<this['state']>]: ReturnType<this['state']>[key]}, this['childContext'][number]>; |
|
|
|
computed: {[key: string]: () => unknown} | {}; |
|
} |
|
|
|
// union to intersection of functions |
|
type UnionToIoF<U> = |
|
(U extends any ? (k: (x: U) => void) => void : never) extends |
|
((k: infer I) => void) ? I : never |
|
|
|
// return last element from Union |
|
type UnionPop<U> = UnionToIoF<U> extends { (a: infer A): void; } ? A : never; |
|
|
|
// prepend an element to a tuple. |
|
type Prepend<U, T extends ReadonlyArray<any>> = |
|
((a: U, ...r: T) => void) extends (...r: infer R) => void ? R : never; |
|
|
|
type UnionToTupleRecursively<Union, Result extends ReadonlyArray<any>> = { |
|
1: Result; |
|
0: UnionToTupleRecursively_<Union, UnionPop<Union>, Result>; |
|
// 0: UnionToTupleRecursively<Exclude<Union, UnionPop<Union>>, Prepend<UnionPop<Union>, Result>> |
|
}[[Union] extends [never] ? 1 : 0]; |
|
|
|
type UnionToTupleRecursively_<Union, Element, Result extends ReadonlyArray<any>> = |
|
UnionToTupleRecursively<Exclude<Union, Element>, Prepend<Element, Result>>; |
|
|
|
type UnionToTuple<U> = UnionToTupleRecursively<U, []>;
|
|
|