Skip to content

Commit

Permalink
Optimizing some code paths
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Nov 19, 2016
1 parent feb4222 commit 2ec411f
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 168 deletions.
109 changes: 53 additions & 56 deletions lib/haro.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @copyright 2016
* @license BSD-3-Clause
* @link https://github.com/avoidwork/haro
* @version 3.0.6
* @version 3.0.7
*/
"use strict";

Expand Down Expand Up @@ -80,7 +80,7 @@
}

function clone (arg) {
return JSON.parse(JSON.stringify(arg));
return JSON.parse(JSON.stringify(arg, null, 0));
}

function concatURI (left, right) {
Expand Down Expand Up @@ -479,6 +479,16 @@
return defer.promise;
}

crawl (arg) {
let result = clone(arg);

(this.source || "").split(".").forEach(i => {
result = result[i];
});

return result;
}

del (key, batch = false) {
let defer = deferred(),
next;
Expand Down Expand Up @@ -598,24 +608,23 @@
}

filter (fn, raw = false) {
let result = [],
lfn;

if (!raw) {
lfn = (value, key) => {
if (fn(value, key) === true) {
result.push(tuple(key, value));
}
};
} else {
lfn = (value, key) => {
if (fn(value, key) === true) {
result.push(value);
}
};
}
let result = [];

this.forEach(lfn);
this.forEach((function () {
if (!raw) {
return (value, key) => {
if (fn(value, key) === true) {
result.push(tuple(key, value));
}
};
} else {
return (value, key) => {
if (fn(value, key) === true) {
result.push(value);
}
};
}
}()));

return !raw ? tuple.apply(tuple, result) : result;
}
Expand All @@ -629,13 +638,9 @@
}

get (key, raw = false) {
let output;

if (this.data.has(key)) {
output = !raw ? tuple(key, this.data.get(key)) : clone(this.data.get(key));
}
let output = clone(this.data.get(key) || null);

return output;
return output && !raw ? tuple(key, output) : output;
}

has (key) {
Expand All @@ -653,11 +658,11 @@
promise = this.offload([[this.id, other.id], this.toArray(null, true), other.toArray(null, true), this.key, on, type], "join");
}

promise.then(result => {
if (typeof result === "string") {
defer.reject(new Error(result));
promise.then(arg => {
if (typeof arg === "string") {
defer.reject(new Error(arg));
} else {
defer.resolve(result);
defer.resolve(arg);
}
}, defer.reject);
} else {
Expand Down Expand Up @@ -766,6 +771,7 @@
defer.resolve(true);
} else if (type === "records") {
this.data.clear();
this.indexes.clear();
this.registry.length = 0;
data.forEach(datum => {
let key = datum[this.key] || uuid();
Expand All @@ -784,6 +790,8 @@

register (key, fn) {
adapter[key] = fn;

return this;
}

reindex (index) {
Expand Down Expand Up @@ -813,7 +821,7 @@

request (input, config = {}) {
let defer = deferred(),
cfg = merge(this.config, config);
cfg = merge(clone(this.config), config);

cfg.method = cfg.method.toUpperCase();

Expand Down Expand Up @@ -911,12 +919,7 @@

if (lkey === null) {
if (this.key) {
if (this.source) {
this.source.split(".").forEach(i => {
xdata = xdata[i] || {};
});
}

xdata = this.crawl(xdata);
lkey = xdata[this.key] || ldata[this.key] || uuid();
} else {
lkey = uuid();
Expand Down Expand Up @@ -961,7 +964,7 @@

if (lkey && this.data.has(lkey)) {
method = "put";
ogdata = this.data.get(lkey);
ogdata = clone(this.data.get(lkey) || {});

if (!override) {
ldata = merge(ogdata, ldata);
Expand All @@ -986,13 +989,13 @@

this.request(luri, {
method: "patch",
body: JSON.stringify(body)
body: JSON.stringify(body, null, 0)
}).then(next, e => {
if (e[1] === 405) {
this.patch = false;
this.request(luri, {
method: method,
body: JSON.stringify(ldata)
body: JSON.stringify(ldata, null, 0)
}).then(next, defer.reject);
} else {
defer.reject(e);
Expand Down Expand Up @@ -1060,7 +1063,6 @@
lindex;

if (!this.indexes.has(index)) {
this.index.push(index);
this.reindex(index);
}

Expand All @@ -1080,11 +1082,9 @@

storage (...args) {
let defer = deferred(),
deferreds = [];

Object.keys(this.adapters).forEach(i => {
deferreds.push(this.cmd.apply(this, [i].concat(args)));
});
deferreds = Object.keys(this.adapters).map(i => {
return this.cmd.apply(this, [i].concat(args));
});

if (deferreds.length > 0) {
Promise.all(deferreds).then(() => {
Expand All @@ -1102,19 +1102,15 @@
valid = true;

this.request(this.uri).then(arg => {
let data = arg[0];
let data;

this.patch = (arg[2].Allow || arg[2].allow || "").indexOf("PATCH") > -1;

if (this.source) {
try {
this.source.split(".").forEach(i => {
data = data[i];
});
} catch (e) {
valid = false;
defer.reject(e);
}
try {
data = this.crawl(arg[0]);
} catch (e) {
valid = false;
defer.reject(e);
}

if (valid) {
Expand All @@ -1136,6 +1132,7 @@
return larg;
}, e => {
this.onerror("sync", e);

throw e;
});
}
Expand Down Expand Up @@ -1263,7 +1260,7 @@
}

factory.transform = cast;
factory.version = "3.0.6";
factory.version = "3.0.7";

// Node, AMD & window supported
if (typeof exports !== "undefined") {
Expand Down
Loading

0 comments on commit 2ec411f

Please sign in to comment.