Skip to content

Commit

Permalink
Refactoring some signatures to minimize assignments, fixing `transmit…
Browse files Browse the repository at this point in the history
…()` retry when a delete patch fails
  • Loading branch information
avoidwork committed Jan 23, 2017
1 parent 1c1dae3 commit 6cf027c
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 245 deletions.
117 changes: 26 additions & 91 deletions lib/haro.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @copyright 2017
* @license BSD-3-Clause
* @link https://github.com/avoidwork/haro
* @version 3.1.5
* @version 3.1.6
*/
"use strict";

Expand Down Expand Up @@ -516,15 +516,7 @@
}

dump (type = "records") {
let result;

if (type === "records") {
result = this.toArray(null, false);
} else {
result = this.transform(this.indexes);
}

return result;
return type === "records" ? this.toArray(null, false) : this.transform(this.indexes);
}

entries () {
Expand All @@ -537,9 +529,7 @@
result = [];

if (this.indexes.has(key)) {
(this.indexes.get(key).get(value) || new Set()).forEach(i => {
result.push(this.get(i, raw));
});
(this.indexes.get(key).get(value) || new Set()).forEach(i => result.push(this.get(i, raw)));
}

return raw ? result : this.list(...result);
Expand Down Expand Up @@ -570,9 +560,7 @@
}

forEach (fn, ctx) {
this.data.forEach((value, key) => {
fn(clone(value), clone(key));
}, ctx);
this.data.forEach((value, key) => fn(clone(value), clone(key)), ctx);

return this;
}
Expand Down Expand Up @@ -653,9 +641,7 @@
map (fn, raw = false) {
const result = [];

this.forEach((value, key) => {
result.push(fn(value, key));
});
this.forEach((value, key) => result.push(fn(value, key)));

return raw ? result : this.list(...result);
}
Expand Down Expand Up @@ -752,23 +738,15 @@
reindex (index) {
if (!index) {
this.indexes.clear();
this.index.forEach(i => {
this.indexes.set(i, new Map());
});
this.forEach((data, key) => {
this.index.forEach(i => {
setIndex(this.index, this.indexes, this.delimiter, key, data, i, this.pattern);
});
});
this.index.forEach(i => this.indexes.set(i, new Map()));
this.forEach((data, key) => this.index.forEach(i => setIndex(this.index, this.indexes, this.delimiter, key, data, i, this.pattern)));
} else {
if (this.index.indexOf(index) === -1) {
this.index.push(index);
}

this.indexes.set(index, new Map());
this.forEach((data, key) => {
setIndex(this.index, this.indexes, this.delimiter, key, data, index, this.pattern);
});
this.forEach((data, key) => setIndex(this.index, this.indexes, this.delimiter, key, data, index, this.pattern));
}

return this;
Expand All @@ -785,8 +763,9 @@
}

fetch(input, cfg).then(res => {
let status = res.status,
headers;
const status = res.status;

let headers;

if (res.headers._headers) {
headers = {};
Expand All @@ -799,12 +778,8 @@

res[res.headers.get("content-type").indexOf("application/json") > -1 ? "json" : "text"]().then(arg => {
defer[status < 200 || status >= 400 ? "reject" : "resolve"](this.list(this.onrequest(arg, status, headers), status, headers));
}, e => {
defer.reject(this.list(e.message, status, headers));
});
}, e => {
defer.reject(this.list(e.message, 0, {}));
});
}, e => defer.reject(this.list(e.message, status, headers)));
}, e => defer.reject(this.list(e.message, 0, {})));

return defer.promise;
}
Expand All @@ -831,16 +806,8 @@
rgex = value && typeof value.test === "function",
seen = new Set();

let indexes;

if (value) {
if (index) {
indexes = Array.isArray(index) ? index : [index];
} else {
indexes = this.index;
}

indexes.forEach(i => {
each(index ? Array.isArray(index) ? index : [index] : this.index, i => {
let idx = this.indexes.get(i);

if (idx) {
Expand Down Expand Up @@ -970,17 +937,7 @@
}

sort (fn, frozen = true) {
let result;

if (frozen) {
result = Object.freeze(this.limit(0, this.total, true).sort(fn).map(i => {
return Object.freeze(i);
}));
} else {
result = this.limit(0, this.total, true).sort(fn);
}

return result;
return frozen ? Object.freeze(this.limit(0, this.total, true).sort(fn).map(i => Object.freeze(i))) : this.limit(0, this.total, true).sort(fn);
}

sortBy (index, raw = false) {
Expand All @@ -994,15 +951,8 @@
}

lindex = this.indexes.get(index);
lindex.forEach((idx, key) => {
keys.push(key);
});

each(keys.sort(), i => {
lindex.get(i).forEach(key => {
result.push(this.get(key, raw));
});
});
lindex.forEach((idx, key) => keys.push(key));
each(keys.sort(), i => lindex.get(i).forEach(key => result.push(this.get(key, raw))));

return raw ? result : this.list(...result);
}
Expand All @@ -1012,9 +962,7 @@
deferreds = Object.keys(this.adapters).map(i => this.cmd.apply(this, [i].concat(args)));

if (deferreds.length > 0) {
Promise.all(deferreds).then(() => {
defer.resolve(true);
}, defer.reject);
Promise.all(deferreds).then(() => defer.resolve(true), defer.reject);
} else {
defer.resolve(false);
}
Expand All @@ -1033,11 +981,7 @@
this.patch = (arg[2].Allow || arg[2].allow || "").indexOf("PATCH") > -1;

try {
if (this.source) {
data = this.crawl(arg[0]);
} else {
data = arg[0];
}
data = this.source ? this.crawl(arg[0]) : arg[0];
} catch (e) {
valid = false;
defer.reject(e);
Expand Down Expand Up @@ -1069,26 +1013,20 @@
let result;

if (data) {
result = data.map(i => {
return frozen ? i[1] : clone(i[1]);
});
result = data.map(i => frozen ? i[1] : clone(i[1]));
} else {
result = this.limit(0, this.total, true);

if (frozen) {
each(result, i => {
Object.freeze(i);
});
each(result, i => Object.freeze(i));
}
}

return frozen ? Object.freeze(result) : result;
}

toObject (data, frozen = true) {
let result;

result = !data ? toObjekt(this, frozen) : data.reduce((a, b) => {
const result = !data ? toObjekt(this, frozen) : data.reduce((a, b) => {
const obj = clone(b[1]);

if (frozen) {
Expand All @@ -1100,11 +1038,7 @@
return a;
}, {});

if (frozen) {
Object.freeze(result);
}

return result;
return frozen ? Object.freeze(result) : result;
}

transform (input, fn) {
Expand All @@ -1131,7 +1065,7 @@
this.request(uri, {method: "patch", body: JSON.stringify(body, null, 0)}).then(defer.resolve, e => {
if (e[1] === 405) {
this.patch = false;
this.request(uri, {method: method, body: JSON.stringify(data, null, 0)}).then(defer.resolve, defer.reject);
this.request(!data ? concatURI(this.uri, key) : uri, {method: method, body: JSON.stringify(data, null, 0)}).then(defer.resolve, defer.reject);
} else {
defer.reject(e);
}
Expand Down Expand Up @@ -1174,6 +1108,7 @@

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

obj.onerror = err => {
defer.reject(err);
obj.terminate();
Expand Down Expand Up @@ -1221,7 +1156,7 @@
}

factory.transform = cast;
factory.version = "3.1.5";
factory.version = "3.1.6";

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

0 comments on commit 6cf027c

Please sign in to comment.