Skip to content

Commit

Permalink
Refactoring batch() such that it is no longer a vehicle for over th…
Browse files Browse the repository at this point in the history
…e wire i/o - it'll only do local async ops
  • Loading branch information
avoidwork committed Jan 23, 2017
1 parent b86ddab commit 1c1dae3
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 156 deletions.
56 changes: 5 additions & 51 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.4
* @version 3.1.5
*/
"use strict";

Expand Down Expand Up @@ -390,57 +390,11 @@
}
}

batch (args, type, lload = false) {
batch (args, type = "set", lload = false) {
const defer = deferred(),
del = type === "del";
fn = type === "del" ? i => this.del(i, true) : i => this.set(null, i, true, true, lload);

let data, fn, hash;

function next () {
Promise.all(args.map(fn)).then(defer.resolve, defer.reject);
}

if (del) {
fn = i => {
return this.del(i, true);
};
} else {
fn = i => {
return this.set(null, i, true, true, lload);
};
}

if (this.patch) {
if (del) {
data = patch(this.limit(0, this.total, true).map(i => {
return i[this.key];
}), args, this.key, true);
} else {
data = [];
hash = {};
each(args, i => {
let key = i[this.key];

if (key) {
hash[key] = i;
} else {
data.push({op: "add", path: "/", value: i});
}
});
data = data.concat(patch(this.toObject(undefined, false), hash, this.key, true));
}

if (data.length > 0) {
this.request(concatURI(this.uri, null), {
method: "patch",
body: JSON.stringify(data)
}).then(next, defer.reject);
} else {
defer.resolve();
}
} else {
next();
}
Promise.all(args.map(fn)).then(defer.resolve, defer.reject);

return defer.promise.then(arg => {
this.onbatch(type, arg);
Expand Down Expand Up @@ -1267,7 +1221,7 @@
}

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

// Node, AMD & window supported
if (typeof exports !== "undefined") {
Expand Down
63 changes: 10 additions & 53 deletions lib/haro.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.4
* @version 3.1.5
*/
"use strict";

Expand Down Expand Up @@ -428,63 +428,20 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons

_createClass(Haro, [{
key: "batch",
value: function batch(args, type) {
value: function batch(args) {
var _this2 = this;

var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "set";
var lload = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;

var defer = deferred(),
del = type === "del";
fn = type === "del" ? function (i) {
return _this2.del(i, true);
} : function (i) {
return _this2.set(null, i, true, true, lload);
};

var data = void 0,
fn = void 0,
hash = void 0;

function next() {
Promise.all(args.map(fn)).then(defer.resolve, defer.reject);
}

if (del) {
fn = function fn(i) {
return _this2.del(i, true);
};
} else {
fn = function fn(i) {
return _this2.set(null, i, true, true, lload);
};
}

if (this.patch) {
if (del) {
data = patch(this.limit(0, this.total, true).map(function (i) {
return i[_this2.key];
}), args, this.key, true);
} else {
data = [];
hash = {};
each(args, function (i) {
var key = i[_this2.key];

if (key) {
hash[key] = i;
} else {
data.push({ op: "add", path: "/", value: i });
}
});
data = data.concat(patch(this.toObject(undefined, false), hash, this.key, true));
}

if (data.length > 0) {
this.request(concatURI(this.uri, null), {
method: "patch",
body: JSON.stringify(data)
}).then(next, defer.reject);
} else {
defer.resolve();
}
} else {
next();
}
Promise.all(args.map(fn)).then(defer.resolve, defer.reject);

return defer.promise.then(function (arg) {
_this2.onbatch(type, arg);
Expand Down Expand Up @@ -1462,7 +1419,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}

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

// Node, AMD & window supported
if (typeof exports !== "undefined") {
Expand Down
2 changes: 1 addition & 1 deletion lib/haro.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/haro.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "haro",
"version": "3.1.4",
"version": "3.1.5",
"description": "Harō is a modern immutable DataStore",
"main": "lib/haro.es6.js",
"scripts": {
Expand Down
52 changes: 3 additions & 49 deletions src/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,57 +37,11 @@
}
}

batch (args, type, lload = false) {
batch (args, type = "set", lload = false) {
const defer = deferred(),
del = type === "del";
fn = type === "del" ? i => this.del(i, true) : i => this.set(null, i, true, true, lload);

let data, fn, hash;

function next () {
Promise.all(args.map(fn)).then(defer.resolve, defer.reject);
}

if (del) {
fn = i => {
return this.del(i, true);
};
} else {
fn = i => {
return this.set(null, i, true, true, lload);
};
}

if (this.patch) {
if (del) {
data = patch(this.limit(0, this.total, true).map(i => {
return i[this.key];
}), args, this.key, true);
} else {
data = [];
hash = {};
each(args, i => {
let key = i[this.key];

if (key) {
hash[key] = i;
} else {
data.push({op: "add", path: "/", value: i});
}
});
data = data.concat(patch(this.toObject(undefined, false), hash, this.key, true));
}

if (data.length > 0) {
this.request(concatURI(this.uri, null), {
method: "patch",
body: JSON.stringify(data)
}).then(next, defer.reject);
} else {
defer.resolve();
}
} else {
next();
}
Promise.all(args.map(fn)).then(defer.resolve, defer.reject);

return defer.promise.then(arg => {
this.onbatch(type, arg);
Expand Down

0 comments on commit 1c1dae3

Please sign in to comment.