Skip to content

Commit

Permalink
feat(vector): calc flexible for different vector implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed Aug 20, 2018
1 parent eab990c commit 8bdf2d8
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
/* eslint class-methods-use-this: 0 */
/* eslint no-param-reassign: 0 */

const X = Symbol.for('x');
const Y = Symbol.for('y');
const Z = Symbol.for('z');
const DEFAULT = Symbol.for('default');

let inProgress = DEFAULT;
let inVector;

const v3ValueOf = new Map();
v3ValueOf[X] = function getX() {
return this[inProgress];
};
v3ValueOf[Y] = function getY() {
return this[inProgress];
};
v3ValueOf[Z] = function getZ() {
v3ValueOf[X] = function getValueOf() {
if (!inVector) {
inVector = this;
}
return this[inProgress];
};
v3ValueOf[Y] = v3ValueOf[X];
v3ValueOf[Z] = v3ValueOf[X];
v3ValueOf[DEFAULT] = function getDefault() {
return this.length;
};
Expand All @@ -28,18 +30,27 @@ function innerCalc(alg, result) {
throw new Error('something wrong');
}
try {
const res = result;

inProgress = X;
res[inProgress] = alg();
const x = alg();

if (!result && !inVector) {
return x;
}
inProgress = Y;
res[inProgress] = alg();
const y = alg();
inProgress = Z;
res[inProgress] = alg();
const z = alg();

return res;
if (!result) {
return inVector.createVector(x, y, z);
}
result[X] = x;
result[Y] = y;
result[Z] = z;
return result;
} finally {
inProgress = DEFAULT;
inVector = undefined;
}
}

Expand Down Expand Up @@ -200,7 +211,7 @@ export class Victor extends AVector {
}

export function calc(alg) {
return innerCalc(alg, new Vector());
return innerCalc(alg);
}

export default Vector;

0 comments on commit 8bdf2d8

Please sign in to comment.