Skip to content

Commit

Permalink
Renaming the utility transform() to cast() to avoid Class issue…
Browse files Browse the repository at this point in the history
…s, creating `onmessage()` with mimic messaging
  • Loading branch information
avoidwork committed Sep 28, 2015
1 parent 7dbb64a commit 91b6ef4
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 29 deletions.
55 changes: 46 additions & 9 deletions lib/haro.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,32 @@ const regex = {
const webWorker = typeof Blob !== "undefined" && typeof Worker !== "undefined";
let adapter = {};

function transform (input) {
function cast (input) {
let result;

switch (true) {
case input instanceof Map:
result = {};
input.forEach((value, key) => {
result[key] = transform(value);
result[key] = cast(value);
});
break;
case input instanceof Set:
result = [];
input.forEach(i => {
result.push(transform(i));
result.push(cast(i));
});
break;
case input instanceof Array:
result = new Set();
input.forEach(i => {
result.add(transform(i));
result.add(cast(i));
});
break;
case input instanceof Object:
result = new Map();
Object.keys(input).forEach(i => {
result.set(i, transform(input[i]));
result.set(i, cast(input[i]));
});
break;
default:
Expand Down Expand Up @@ -129,7 +129,7 @@ function createIndexes (args, indexes, key, delimiter, pattern) {
}
});

return transform(result);
return cast(result);
}

function iterate (obj, fn) {
Expand Down Expand Up @@ -165,6 +165,20 @@ function merge (a, b) {
return c;
}

function onmessage (ev) {
let records = ev.data.records,
indices = ev.data.indices,
cmd = ev.data.cmd || "index",
result;

if (cmd === "index") {
// temp, sending the input back
result = {records: records, indices: indices};
}

postMessage(result);
}

function patch (ogdata = {}, data = {}, key = "", overwrite = false) {
let result = [];

Expand Down Expand Up @@ -965,7 +979,7 @@ class Haro {
}

transform (input, fn) {
return typeof fn === "function" ? fn(input) : transform(input);
return typeof fn === "function" ? fn(input) : cast(input);
}

unload (type = "mongo", key = undefined) {
Expand Down Expand Up @@ -993,6 +1007,28 @@ class Haro {
values () {
return this.data.values();
}

useWorker (defer) {
let obj;

if (this.worker) {
obj = new Worker(this.worker);

obj.onerror = function (err) {
defer.reject(err);
obj.terminate();
};

obj.onmessage = function (ev) {
defer.resolve(ev.data);
obj.terminate();
};
} else {
defer.reject(new Error("Worker not supported"));
}

return obj;
}
}

function factory (data = null, config = {}, indexes = []) {
Expand All @@ -1008,7 +1044,8 @@ function factory (data = null, config = {}, indexes = []) {
merge.toString(),
setIndexValue.toString(),
setIndex.toString(),
transform.toString()
cast.toString(),
"onmessage = " + onmessage.toString() + ";"
];

try {
Expand All @@ -1021,7 +1058,7 @@ function factory (data = null, config = {}, indexes = []) {
return obj;
}

factory.transform = transform;
factory.transform = cast;
factory.version = "1.7.0";

// Node, AMD & window supported
Expand Down
55 changes: 46 additions & 9 deletions lib/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,32 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
var webWorker = typeof Blob !== "undefined" && typeof Worker !== "undefined";
var adapter = {};

function _transform(input) {
function cast(input) {
var result = undefined;

switch (true) {
case input instanceof Map:
result = {};
input.forEach(function (value, key) {
result[key] = _transform(value);
result[key] = cast(value);
});
break;
case input instanceof Set:
result = [];
input.forEach(function (i) {
result.push(_transform(i));
result.push(cast(i));
});
break;
case input instanceof Array:
result = new Set();
input.forEach(function (i) {
result.add(_transform(i));
result.add(cast(i));
});
break;
case input instanceof Object:
result = new Map();
Object.keys(input).forEach(function (i) {
result.set(i, _transform(input[i]));
result.set(i, cast(input[i]));
});
break;
default:
Expand Down Expand Up @@ -133,7 +133,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}
});

return _transform(result);
return cast(result);
}

function iterate(obj, fn) {
Expand Down Expand Up @@ -169,6 +169,20 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
return c;
}

function onmessage(ev) {
var records = ev.data.records,
indices = ev.data.indices,
cmd = ev.data.cmd || "index",
result = undefined;

if (cmd === "index") {
// temp, sending the input back
result = { records: records, indices: indices };
}

postMessage(result);
}

function patch() {
var ogdata = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var data = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
Expand Down Expand Up @@ -1084,7 +1098,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}, {
key: "transform",
value: function transform(input, fn) {
return typeof fn === "function" ? fn(input) : _transform(input);
return typeof fn === "function" ? fn(input) : cast(input);
}
}, {
key: "unload",
Expand Down Expand Up @@ -1120,6 +1134,29 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
value: function values() {
return this.data.values();
}
}, {
key: "useWorker",
value: function useWorker(defer) {
var obj = undefined;

if (this.worker) {
obj = new Worker(this.worker);

obj.onerror = function (err) {
defer.reject(err);
obj.terminate();
};

obj.onmessage = function (ev) {
defer.resolve(ev.data);
obj.terminate();
};
} else {
defer.reject(new Error("Worker not supported"));
}

return obj;
}
}]);

return Haro;
Expand All @@ -1134,7 +1171,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
functions = undefined;

if (webWorker) {
functions = [clone.toString(), createIndexes.toString(), keyIndex.toString(), iterate.toString(), merge.toString(), setIndexValue.toString(), setIndex.toString(), _transform.toString()];
functions = [clone.toString(), createIndexes.toString(), keyIndex.toString(), iterate.toString(), merge.toString(), setIndexValue.toString(), setIndex.toString(), cast.toString(), "onmessage = " + onmessage.toString() + ";"];

try {
obj.worker = global.URL.createObjectURL(blob(functions.join("\n")));
Expand All @@ -1146,7 +1183,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
return obj;
}

factory.transform = _transform;
factory.transform = cast;
factory.version = "1.7.0";

// Node, AMD & window supported
Expand Down
Loading

0 comments on commit 91b6ef4

Please sign in to comment.