-
Notifications
You must be signed in to change notification settings - Fork 434
/
Copy pathhelpers.ts
95 lines (80 loc) · 2.56 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { ComponentOptions, ShallowUnwrapRef, Ref } from 'vue'
import { Vue, VueBase, VueConstructor, VueMixin } from './vue'
export function Options<V extends Vue>(
options: ComponentOptions & ThisType<V>
): <VC extends VueConstructor<VueBase>>(target: VC) => VC {
return (Component) => {
Component.__o = options
return Component
}
}
export interface VueDecorator {
// Class decorator
(Ctor: VueConstructor<VueBase>): void
// Property decorator
(target: VueBase, key: string): void
// Parameter decorator
(target: VueBase, key: string, index: number): void
}
export function createDecorator(
factory: (options: ComponentOptions, key: string, index: number) => void
): VueDecorator {
return (
target: VueBase | VueConstructor<VueBase>,
key?: any,
index?: any
) => {
const Ctor =
typeof target === 'function'
? target
: (target.constructor as VueConstructor)
if (!Ctor.__d) {
Ctor.__d = []
;(Ctor.__d as any).__n = Ctor.name
} else if ((Ctor.__d as any).__n !== Ctor.name){
Ctor.__d = [].concat(Ctor.__d as [])
;(Ctor.__d as any).__n = Ctor.name
}
if (typeof index !== 'number') {
index = undefined
}
Ctor.__d.push((options) => factory(options, key, index))
}
}
export type UnionToIntersection<U> = (
U extends any ? (k: U) => void : never
) extends (k: infer I) => void
? I
: never
export type ExtractInstance<T> = T extends VueMixin<infer V> ? V : never
export type MixedVueBase<Mixins extends VueMixin[]> = Mixins extends (infer T)[]
? VueConstructor<UnionToIntersection<ExtractInstance<T>> & Vue>
: never
export function mixins<T extends VueMixin[]>(...Ctors: T): MixedVueBase<T>
export function mixins(...Ctors: VueMixin[]): VueConstructor {
return class MixedVue extends Vue {
static __b: ComponentOptions = {
mixins: Ctors.map((Ctor) => Ctor.__vccOpts),
}
constructor(...args: any[]) {
super(...args)
Ctors.forEach((Ctor) => {
const data = new (Ctor as VueConstructor)(...args)
Object.keys(data).forEach((key) => {
;(this as any)[key] = (data as any)[key]
})
})
}
}
}
export type UnwrapSetupValue<T> = T extends Ref<infer R>
? R
: ShallowUnwrapRef<T>
export type UnwrapPromise<T> = T extends Promise<infer R> ? R : T
export function setup<R>(setupFn: () => R): UnwrapSetupValue<UnwrapPromise<R>> {
// Hack to delay the invocation of setup function.
// Will be called after dealing with class properties.
return {
__s: setupFn,
} as UnwrapSetupValue<UnwrapPromise<R>>
}