-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuperMethod.js
144 lines (103 loc) · 3.62 KB
/
superMethod.js
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
import getCaller from "./getCaller";
function getMethodFromComponent(methodName, parentComponentOptions, methodStat) {
const componentMethod = getMethodsMethod(methodName, parentComponentOptions, methodStat);
if (componentMethod) {
return componentMethod;
}
const mixinsMethod = getMixinsMethod(methodName, parentComponentOptions, methodStat);
if (mixinsMethod) {
return mixinsMethod;
}
return getExtendsMethod(methodName, parentComponentOptions, methodStat);
}
function getExtendsMethod(methodName, parentComponentOptions, methodStat) {
const extendsComponentOptions = parentComponentOptions.extends;
if (!extendsComponentOptions) {
return false;
}
return getMethodFromComponent(methodName, extendsComponentOptions, methodStat);
}
function getMethodsMethod(methodName, componentOptions, methodStat) {
const methods = componentOptions.methods;
if (!methods) {
return false;
}
const method = methods[methodName];
if (!method) {
return false;
}
// First $super() caller is method in mixin/extends - skip it and continue search base method
if (methodStat.origFunction === method) {
return false;
}
if (methodStat.skipTimes) {
methodStat.skipTimes--;
return false;
// return getParentMethod(componentOptions, methodName, methodStat);
}
return method;
}
function getMixinsMethod(methodName, parentComponentOptions, methodStat) {
const mixins = parentComponentOptions.mixins;
if (!mixins) {
return false;
}
const reversedMixins = Array.from(mixins).reverse();
for (const mixinComponentOptions of reversedMixins) {
const mixinMethod = getMethodFromComponent(methodName, mixinComponentOptions, methodStat);
if (mixinMethod) {
return mixinMethod;
}
}
return false;
}
function getCallerMethodName() {
return getCaller(3).split('.')[1];
}
function getParentMethod(componentOptions, methodName, methodStat) {
const mixinsMethod = getMixinsMethod(methodName, componentOptions, methodStat);
if (mixinsMethod) {
return mixinsMethod;
}
const extendsMethod = getExtendsMethod(methodName, componentOptions, methodStat);
if (extendsMethod) {
return extendsMethod;
}
return false;
}
export default function () {
const methodName = getCallerMethodName();
const componentOptions = this.$options;
// first call for component instance
if (!this.__super) {
this.__super = {};
}
const superGlobals = this.__super;
// first call for method
if (!superGlobals.hasOwnProperty(methodName)) {
superGlobals[methodName] = {
origFunction: componentOptions.methods[methodName],
callLevel: -1,
skipTimes: -1,
levelsCache: {},
};
}
const methodStat = superGlobals[methodName];
methodStat.callLevel++;
// methodStat.skipTimes = methodStat.callLevel;
methodStat.skipTimes = methodStat.callLevel;
const parentMethod = (methodStat.levelsCache[methodStat.callLevel])
? methodStat.levelsCache[methodStat.callLevel]
: getParentMethod(componentOptions, methodName, methodStat);
if (!parentMethod) {
throw Error('Super method not found!');
}
methodStat.levelsCache[methodStat.callLevel] = parentMethod;
const oldMethod = this[methodName];
// don't use apply to not method name in stack
this[methodName] = parentMethod;
const result = this[methodName](...arguments);
this[methodName] = oldMethod;
methodStat.callLevel--;
return result;
}