-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.ts
179 lines (165 loc) · 5.29 KB
/
context.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { getCurrentInstance, type VueInstance, isVue3 } from './vue';
import { bindTarget } from './setup-reference';
import { TargetName, Target } from './types';
import {
SETUP_NAME,
SETUP_OPTIONS_NAME,
SETUP_PROPERTY_DESCRIPTOR,
} from './config';
import { createDefineProperty } from './property-descriptors';
import { DefineConstructor } from './define';
import { SETUP_SETUP_DEFINE, SETUP_USE } from './config';
let currentTarget: Target | null = null;
let currentName: TargetName | null = null;
export function getCurrentHookContext(): { target: object; name: TargetName } {
if (currentTarget === null || currentName === null) {
throw new Error('Can only be obtained in hook functions');
}
return { target: currentTarget, name: currentName };
}
export function setCurrentHookTarget(target: Target | null) {
currentTarget = target;
}
export function setCurrentHookName(name: TargetName | null) {
currentName = name;
}
const WHITE_LIST: string[] = [SETUP_SETUP_DEFINE, '$vm', '$emit', '$props'];
function use(vm: VueInstance, _This: any) {
let use: Map<any, InstanceType<DefineConstructor>>;
if (vm[SETUP_USE]) {
use = vm[SETUP_USE];
} else {
use = new Map();
Object.defineProperty(vm, SETUP_USE, {
get() {
return use;
},
});
}
let app = use.get(_This)!;
if (app) {
return app;
}
app = new _This() as InstanceType<DefineConstructor>;
use.set(_This, app);
return app;
}
function proxyVue3Props(app: InstanceType<DefineConstructor>, vm: VueInstance) {
interface Item {
ssrRender?: Function;
render?: Function;
}
if (!vm.$) {
return;
}
const item = vm.$ as Item;
const render = item.ssrRender || item.render;
if (app[SETUP_SETUP_DEFINE] && render) {
const keys = Object.keys(app.$defaultProps);
if (!keys.length) return;
const proxyRender = (...args: any[]) => {
const props = vm.$props;
const arr: { key: string; pd: PropertyDescriptor }[] = [];
const dpp = createDefineProperty(props);
// Set default Props
keys.forEach((key) => {
const value = app[key];
if (props[key] !== value) {
const pd = Object.getOwnPropertyDescriptor(props, key);
if (!pd) return;
dpp(key, { ...pd, value });
arr.push({
key,
pd,
});
}
});
const res = render.apply(vm, args);
arr.forEach((item) => {
dpp(item.key, item.pd);
});
// Resume default props
return res;
};
if (item.ssrRender) {
item.ssrRender = proxyRender;
} else if (item.render) {
item.render = proxyRender;
}
}
}
function initInject(app: InstanceType<DefineConstructor>, vm: VueInstance) {
if (isVue3) {
proxyVue3Props(app, vm);
}
const names = Object.getOwnPropertyNames(app);
const defineProperty = createDefineProperty(vm);
const propertyDescriptor = app.constructor[
SETUP_PROPERTY_DESCRIPTOR
] as Map<string, PropertyDescriptor>;
names.forEach((name) => {
if (propertyDescriptor.has(name) || WHITE_LIST.includes(name)) return;
defineProperty(name, {
get() {
return app[name];
},
set(val) {
app[name] = val;
},
});
});
propertyDescriptor.forEach((value, name) => {
if (WHITE_LIST.includes(name)) return;
defineProperty(name, {
get() {
return app[name];
},
set(val) {
app[name] = val;
},
});
});
}
export type DefaultProps = Record<string, any>;
export type DefaultEmit = (...args: any[]) => void;
export class Context<T extends {} = {}, E extends DefaultEmit = DefaultEmit> {
public static [SETUP_NAME] = false;
public static [SETUP_OPTIONS_NAME] = new Map();
public static [SETUP_PROPERTY_DESCRIPTOR] = new Map<
string,
PropertyDescriptor
>();
public static use<T extends new (...args: any) => any>(this: T) {
const vm = getCurrentInstance();
if (!vm) {
throw Error('Please run in the setup function');
}
return use(vm, this) as InstanceType<T>;
}
public static inject<T extends new (...args: any) => any>(this: T) {
const _This = this;
const options: any = {
created() {
const vm = this as any as VueInstance;
const app = use(vm, _This);
initInject(app, vm);
},
};
return options as {
data: () => Omit<InstanceType<T>, `$${string}`>;
created(): void;
};
}
public $vm: VueInstance;
public $emit: E;
public constructor() {
const vm = getCurrentInstance();
this.$vm = vm ?? ({ $props: {}, $emit: emit } as any);
this.$emit = this.$vm.$emit.bind(this.$vm) as E;
bindTarget(this);
}
public get $props() {
return (this.$vm.$props ?? {}) as T;
}
}
function emit() {}