Skip to content

Commit a818f29

Browse files
committed
fix(operator): replace Map with explicit if statements
1 parent 598ca29 commit a818f29

File tree

1 file changed

+44
-46
lines changed

1 file changed

+44
-46
lines changed

src/operator.js

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,9 @@ const DEFAULT = Symbol.for('default');
1010
let inProgress = DEFAULT;
1111
let inVector;
1212

13-
const v3ValueOf = new Map();
14-
v3ValueOf[X] = function () {
15-
if (!inVector) {
16-
inVector = this;
17-
}
18-
return this[inProgress];
19-
};
20-
v3ValueOf[Y] = function () {
21-
return this[inProgress];
22-
};
23-
v3ValueOf[Z] = v3ValueOf[Y];
24-
v3ValueOf[DEFAULT] = function (org) {
25-
return org.call(this);
26-
};
27-
2813
let resultCacheIndex = -1;
2914
let handlingCache = false;
3015
const resultCache = [];
31-
const v3resultCache = new Map();
32-
v3resultCache[X] = function (org, args) {
33-
if (handlingCache) {
34-
return org.apply(this, args);
35-
}
36-
try {
37-
handlingCache = true;
38-
39-
resultCacheIndex += 1;
40-
const res = org.apply(this, args);
41-
resultCache[resultCacheIndex] = res;
42-
return res;
43-
} finally {
44-
handlingCache = false;
45-
}
46-
};
47-
v3resultCache[Y] = function () {
48-
resultCacheIndex += 1;
49-
return resultCache[resultCacheIndex];
50-
};
51-
v3resultCache[Z] = v3resultCache[Y];
52-
v3resultCache[DEFAULT] = function (org, args) {
53-
return org.apply(this, args);
54-
};
5516

5617
export function operatorCalc(alg, result) {
5718
if (typeof alg !== 'function') {
@@ -97,15 +58,54 @@ export function cachedValueOf(Vector) {
9758
const name = 'valueOf';
9859
const org = Vector.prototype[name];
9960
Vector.prototype[name] = function () {
100-
return v3ValueOf[inProgress].call(this, org);
61+
if (inProgress === X) {
62+
if (!inVector) {
63+
inVector = this;
64+
}
65+
return this.x;
66+
}
67+
if (inProgress === Y) {
68+
return this.y;
69+
}
70+
if (inProgress === Z) {
71+
return this.z;
72+
}
73+
return org.call(this);
74+
};
75+
}
76+
77+
function bindCache(org) {
78+
return function (...args) {
79+
if (inProgress === X) {
80+
if (handlingCache) {
81+
return org.apply(this, args);
82+
}
83+
try {
84+
handlingCache = true;
85+
86+
resultCacheIndex += 1;
87+
const res = org.apply(this, args);
88+
resultCache[resultCacheIndex] = res;
89+
return res;
90+
} finally {
91+
handlingCache = false;
92+
}
93+
}
94+
if (inProgress === Y) {
95+
resultCacheIndex += 1;
96+
return resultCache[resultCacheIndex];
97+
}
98+
if (inProgress === Z) {
99+
resultCacheIndex += 1;
100+
return resultCache[resultCacheIndex];
101+
}
102+
return org.apply(this, args);
101103
};
102104
}
103105

104106
export function cachedMethod(Vector, name) {
105107
const org = Vector.prototype[name];
106-
Vector.prototype[name] = function (...args) {
107-
return v3resultCache[inProgress].call(this, org, args);
108-
};
108+
Vector.prototype[name] = bindCache(org);
109109
}
110110

111111
export function cachedGetter(Vector, name) {
@@ -115,8 +115,6 @@ export function cachedGetter(Vector, name) {
115115
};
116116

117117
Object.defineProperty(Vector.prototype, name, {
118-
get() {
119-
return v3resultCache[inProgress].call(this, org);
120-
}
118+
get: bindCache(org)
121119
});
122120
}

0 commit comments

Comments
 (0)