Skip to content

Commit

Permalink
fix(operator): replace Map with explicit if statements
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed Aug 24, 2018
1 parent 598ca29 commit a818f29
Showing 1 changed file with 44 additions and 46 deletions.
90 changes: 44 additions & 46 deletions src/operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,9 @@ const DEFAULT = Symbol.for('default');
let inProgress = DEFAULT;
let inVector;

const v3ValueOf = new Map();
v3ValueOf[X] = function () {
if (!inVector) {
inVector = this;
}
return this[inProgress];
};
v3ValueOf[Y] = function () {
return this[inProgress];
};
v3ValueOf[Z] = v3ValueOf[Y];
v3ValueOf[DEFAULT] = function (org) {
return org.call(this);
};

let resultCacheIndex = -1;
let handlingCache = false;
const resultCache = [];
const v3resultCache = new Map();
v3resultCache[X] = function (org, args) {
if (handlingCache) {
return org.apply(this, args);
}
try {
handlingCache = true;

resultCacheIndex += 1;
const res = org.apply(this, args);
resultCache[resultCacheIndex] = res;
return res;
} finally {
handlingCache = false;
}
};
v3resultCache[Y] = function () {
resultCacheIndex += 1;
return resultCache[resultCacheIndex];
};
v3resultCache[Z] = v3resultCache[Y];
v3resultCache[DEFAULT] = function (org, args) {
return org.apply(this, args);
};

export function operatorCalc(alg, result) {
if (typeof alg !== 'function') {
Expand Down Expand Up @@ -97,15 +58,54 @@ export function cachedValueOf(Vector) {
const name = 'valueOf';
const org = Vector.prototype[name];
Vector.prototype[name] = function () {
return v3ValueOf[inProgress].call(this, org);
if (inProgress === X) {
if (!inVector) {
inVector = this;
}
return this.x;
}
if (inProgress === Y) {
return this.y;
}
if (inProgress === Z) {
return this.z;
}
return org.call(this);
};
}

function bindCache(org) {
return function (...args) {
if (inProgress === X) {
if (handlingCache) {
return org.apply(this, args);
}
try {
handlingCache = true;

resultCacheIndex += 1;
const res = org.apply(this, args);
resultCache[resultCacheIndex] = res;
return res;
} finally {
handlingCache = false;
}
}
if (inProgress === Y) {
resultCacheIndex += 1;
return resultCache[resultCacheIndex];
}
if (inProgress === Z) {
resultCacheIndex += 1;
return resultCache[resultCacheIndex];
}
return org.apply(this, args);
};
}

export function cachedMethod(Vector, name) {
const org = Vector.prototype[name];
Vector.prototype[name] = function (...args) {
return v3resultCache[inProgress].call(this, org, args);
};
Vector.prototype[name] = bindCache(org);
}

export function cachedGetter(Vector, name) {
Expand All @@ -115,8 +115,6 @@ export function cachedGetter(Vector, name) {
};

Object.defineProperty(Vector.prototype, name, {
get() {
return v3resultCache[inProgress].call(this, org);
}
get: bindCache(org)
});
}

0 comments on commit a818f29

Please sign in to comment.