Skip to content

Commit

Permalink
Removing try/catch statements from factory(), sync() & blob()
Browse files Browse the repository at this point in the history
… & relying on `Promise` for error handling
  • Loading branch information
avoidwork committed Mar 20, 2017
1 parent 7068821 commit ffaaec5
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 106 deletions.
46 changes: 11 additions & 35 deletions lib/haro.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @author Jason Mulligan <jason.mulligan@avoidwork.com>
* @copyright 2017
* @license BSD-3-Clause
* @version 3.4.1
* @version 3.4.2
*/
"use strict";

Expand Down Expand Up @@ -86,19 +86,7 @@
}

function blob (arg) {
let obj;

try {
obj = new Blob([arg], {type: "application/javascript"});
} catch (e) {
if (!global.BlobBuilder) {
global.BlobBuilder = global.MSBlobBuilder || global.WebKitBlobBuilder || global.MozBlobBuilder;
}

obj = new global.BlobBuilder().append(arg).getBlob();
}

return obj;
return new Blob([arg], {type: "application/javascript"});
}

function clone (arg) {
Expand Down Expand Up @@ -965,30 +953,22 @@
sync (clear = false) {
const defer = deferred();

let valid = true;

this.beforeSync(this.uri, clear);

this.request(this.uri).then(arg => {
let data;
const data = this.source ? this.crawl(arg[0]) : arg[0];

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

try {
data = this.source ? this.crawl(arg[0]) : arg[0];
} catch (e) {
valid = false;
defer.reject(e);
if (clear) {
this.clear();
}

if (valid) {
if (clear) {
this.clear();
}

this.batch(data, "set").then(defer.resolve, defer.reject);
}
this.batch(data, "set").then(defer.resolve, defer.reject);
}, e => {
defer.reject(e[0] || e);
}).catch(e => {
defer.reject(e[0] || e);
});

return defer.promise.then(arg => {
Expand Down Expand Up @@ -1141,11 +1121,7 @@
(!node ? "" : "self.") + "onmessage = " + onmessage.toString() + ";"
];

try {
obj.worker = !node ? global.URL.createObjectURL(blob(functions.join("\n"))) : new Function(functions.join("\n"));
} catch (e) {
obj.worker = null;
}
obj.worker = !node ? global.URL.createObjectURL(blob(functions.join("\n"))) : new Function(functions.join("\n"));
}

if (data) {
Expand All @@ -1156,7 +1132,7 @@
}

factory.transform = cast;
factory.version = "3.4.1";
factory.version = "3.4.2";

// Node, AMD & window supported
if (typeof exports !== "undefined") {
Expand Down
46 changes: 11 additions & 35 deletions lib/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @author Jason Mulligan <jason.mulligan@avoidwork.com>
* @copyright 2017
* @license BSD-3-Clause
* @version 3.4.1
* @version 3.4.2
*/
"use strict";

Expand Down Expand Up @@ -100,19 +100,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}

function blob(arg) {
var obj = void 0;

try {
obj = new Blob([arg], { type: "application/javascript" });
} catch (e) {
if (!global.BlobBuilder) {
global.BlobBuilder = global.MSBlobBuilder || global.WebKitBlobBuilder || global.MozBlobBuilder;
}

obj = new global.BlobBuilder().append(arg).getBlob();
}

return obj;
return new Blob([arg], { type: "application/javascript" });
}

function clone(arg) {
Expand Down Expand Up @@ -1233,30 +1221,22 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons

var defer = deferred();

var valid = true;

this.beforeSync(this.uri, clear);

this.request(this.uri).then(function (arg) {
var data = void 0;
var data = _this15.source ? _this15.crawl(arg[0]) : arg[0];

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

try {
data = _this15.source ? _this15.crawl(arg[0]) : arg[0];
} catch (e) {
valid = false;
defer.reject(e);
if (clear) {
_this15.clear();
}

if (valid) {
if (clear) {
_this15.clear();
}

_this15.batch(data, "set").then(defer.resolve, defer.reject);
}
_this15.batch(data, "set").then(defer.resolve, defer.reject);
}, function (e) {
defer.reject(e[0] || e);
}).catch(function (e) {
defer.reject(e[0] || e);
});

return defer.promise.then(function (arg) {
Expand Down Expand Up @@ -1429,11 +1409,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
if (webWorker) {
functions = [cast.toString(), clone.toString(), createIndexes.toString(), each.toString(), has.toString(), iterate.toString(), joinData.toString(), keyIndex.toString(), setIndexValue.toString(), setIndex.toString(), (!node ? "" : "self.") + "onmessage = " + onmessage.toString() + ";"];

try {
obj.worker = !node ? global.URL.createObjectURL(blob(functions.join("\n"))) : new Function(functions.join("\n"));
} catch (e) {
obj.worker = null;
}
obj.worker = !node ? global.URL.createObjectURL(blob(functions.join("\n"))) : new Function(functions.join("\n"));
}

if (data) {
Expand All @@ -1444,7 +1420,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}

factory.transform = cast;
factory.version = "3.4.1";
factory.version = "3.4.2";

// 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.4.1",
"version": "3.4.2",
"description": "Harō is a modern immutable DataStore",
"main": "lib/haro.es6.js",
"scripts": {
Expand Down
6 changes: 1 addition & 5 deletions src/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
(!node ? "" : "self.") + "onmessage = " + onmessage.toString() + ";"
];

try {
obj.worker = !node ? global.URL.createObjectURL(blob(functions.join("\n"))) : new Function(functions.join("\n"));
} catch (e) {
obj.worker = null;
}
obj.worker = !node ? global.URL.createObjectURL(blob(functions.join("\n"))) : new Function(functions.join("\n"));
}

if (data) {
Expand Down
22 changes: 7 additions & 15 deletions src/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,30 +628,22 @@
sync (clear = false) {
const defer = deferred();

let valid = true;

this.beforeSync(this.uri, clear);

this.request(this.uri).then(arg => {
let data;
const data = this.source ? this.crawl(arg[0]) : arg[0];

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

try {
data = this.source ? this.crawl(arg[0]) : arg[0];
} catch (e) {
valid = false;
defer.reject(e);
if (clear) {
this.clear();
}

if (valid) {
if (clear) {
this.clear();
}

this.batch(data, "set").then(defer.resolve, defer.reject);
}
this.batch(data, "set").then(defer.resolve, defer.reject);
}, e => {
defer.reject(e[0] || e);
}).catch(e => {
defer.reject(e[0] || e);
});

return defer.promise.then(arg => {
Expand Down
14 changes: 1 addition & 13 deletions src/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,7 @@
}

function blob (arg) {
let obj;

try {
obj = new Blob([arg], {type: "application/javascript"});
} catch (e) {
if (!global.BlobBuilder) {
global.BlobBuilder = global.MSBlobBuilder || global.WebKitBlobBuilder || global.MozBlobBuilder;
}

obj = new global.BlobBuilder().append(arg).getBlob();
}

return obj;
return new Blob([arg], {type: "application/javascript"});
}

function clone (arg) {
Expand Down

0 comments on commit ffaaec5

Please sign in to comment.