Skip to content

Commit

Permalink
Adding loading, updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Jul 15, 2016
1 parent f04e5d7 commit b8d4939
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 52 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ Example of specifying the primary key:
var store = haro(null, {key: 'field'});
```
**loading**
_Boolean_
Indicates when an asynchronous load op is in progress; due to it's nature it's ideal to use `batch()` if you intend to
make multiple changes in a short span of time.
**logging**
_Boolean_
Expand Down
39 changes: 36 additions & 3 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 http://haro.rocks
* @version 1.9.11
* @version 1.9.12
*/
"use strict";

Expand Down Expand Up @@ -352,6 +352,7 @@ class Haro {
this.index = [];
this.indexes = new Map();
this.key = "";
this.loading = false;
this.logging = true;
this.patch = false;
this.pattern = "\\s*|\\t*";
Expand Down Expand Up @@ -383,6 +384,8 @@ class Haro {
Promise.all(args.map(fn)).then(defer.resolve, defer.reject);
}

this.loading = true;

if (del) {
fn = i => {
return this.del(i, true);
Expand Down Expand Up @@ -430,18 +433,24 @@ class Haro {
return defer.promise.then(arg => {
let larg = tuple.apply(tuple, arg);

this.loading = false;
this.onbatch(type, larg);

if (this.logging) {
console.log("Batch inserted data into", this.id);
}

return larg;
}, e => {
this.loading = false;
this.onerror("batch", e);
throw e;
});
}

clear () {
this.total = 0;
this.registry = [];
this.registry.length = 0;
this.data.clear();
this.indexes.clear();
this.versions.clear();
Expand Down Expand Up @@ -505,6 +514,10 @@ class Haro {
};

if (this.data.has(key)) {
if (!batch) {
this.loading = true;
}

if (!batch && this.uri) {
if (this.patch) {
this.request(concatURI(this.uri, null), {
Expand Down Expand Up @@ -533,10 +546,18 @@ class Haro {
}

return defer.promise.then(arg => {
if (!batch) {
this.loading = false;
}

this.ondelete(arg);

return arg;
}, e => {
if (!batch) {
this.loading = false;
}

this.onerror("delete", e);
throw e;
});
Expand Down Expand Up @@ -950,6 +971,10 @@ class Haro {
}
}

if (!batch) {
this.loading = true;
}

if (!batch && this.uri) {
luri = concatURI(this.uri, lkey);

Expand Down Expand Up @@ -987,10 +1012,18 @@ class Haro {
}

return defer.promise.then(arg => {
if (!batch) {
this.loading = false;
}

this.onset(arg);

return arg;
}, e => {
if (!batch) {
this.loading = false;
}

this.onerror("set", e);
throw e;
});
Expand Down Expand Up @@ -1259,7 +1292,7 @@ function factory (data = null, config = {}, indexes = []) {
}

factory.transform = cast;
factory.version = "1.9.11";
factory.version = "1.9.12";

// Node, AMD & window supported
if (typeof exports !== "undefined") {
Expand Down
39 changes: 36 additions & 3 deletions lib/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @copyright 2016
* @license BSD-3-Clause
* @link http://haro.rocks
* @version 1.9.11
* @version 1.9.12
*/
"use strict";

Expand Down Expand Up @@ -372,6 +372,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
this.index = [];
this.indexes = new Map();
this.key = "";
this.loading = false;
this.logging = true;
this.patch = false;
this.pattern = "\\s*|\\t*";
Expand Down Expand Up @@ -411,6 +412,8 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
Promise.all(args.map(fn)).then(defer.resolve, defer.reject);
}

this.loading = true;

if (del) {
fn = function fn(i) {
return _this2.del(i, true);
Expand Down Expand Up @@ -458,10 +461,16 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
return defer.promise.then(function (arg) {
var larg = tuple.apply(tuple, arg);

_this2.loading = false;
_this2.onbatch(type, larg);

if (_this2.logging) {
console.log("Batch inserted data into", _this2.id);
}

return larg;
}, function (e) {
_this2.loading = false;
_this2.onerror("batch", e);
throw e;
});
Expand All @@ -470,7 +479,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
key: "clear",
value: function clear() {
this.total = 0;
this.registry = [];
this.registry.length = 0;
this.data.clear();
this.indexes.clear();
this.versions.clear();
Expand Down Expand Up @@ -544,6 +553,10 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
};

if (this.data.has(key)) {
if (!batch) {
this.loading = true;
}

if (!batch && this.uri) {
if (this.patch) {
this.request(concatURI(this.uri, null), {
Expand Down Expand Up @@ -572,10 +585,18 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}

return defer.promise.then(function (arg) {
if (!batch) {
_this3.loading = false;
}

_this3.ondelete(arg);

return arg;
}, function (e) {
if (!batch) {
_this3.loading = false;
}

_this3.onerror("delete", e);
throw e;
});
Expand Down Expand Up @@ -1072,6 +1093,10 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}
}

if (!batch) {
this.loading = true;
}

if (!batch && this.uri) {
luri = concatURI(this.uri, lkey);

Expand Down Expand Up @@ -1109,10 +1134,18 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}

return defer.promise.then(function (arg) {
if (!batch) {
_this10.loading = false;
}

_this10.onset(arg);

return arg;
}, function (e) {
if (!batch) {
_this10.loading = false;
}

_this10.onerror("set", e);
throw e;
});
Expand Down Expand Up @@ -1415,7 +1448,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}

factory.transform = cast;
factory.version = "1.9.11";
factory.version = "1.9.12";

// 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": "1.9.11",
"version": "1.9.12",
"description": "Harō is a modern immutable DataStore using Maps, Sets, Promises, & Tuples",
"main": "lib/haro.js",
"scripts": {
Expand Down
35 changes: 34 additions & 1 deletion src/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Haro {
this.index = [];
this.indexes = new Map();
this.key = "";
this.loading = false;
this.logging = true;
this.patch = false;
this.pattern = "\\s*|\\t*";
Expand Down Expand Up @@ -46,6 +47,8 @@ class Haro {
Promise.all(args.map(fn)).then(defer.resolve, defer.reject);
}

this.loading = true;

if (del) {
fn = i => {
return this.del(i, true);
Expand Down Expand Up @@ -93,18 +96,24 @@ class Haro {
return defer.promise.then(arg => {
let larg = tuple.apply(tuple, arg);

this.loading = false;
this.onbatch(type, larg);

if (this.logging) {
console.log("Batch inserted data into", this.id);
}

return larg;
}, e => {
this.loading = false;
this.onerror("batch", e);
throw e;
});
}

clear () {
this.total = 0;
this.registry = [];
this.registry.length = 0;
this.data.clear();
this.indexes.clear();
this.versions.clear();
Expand Down Expand Up @@ -168,6 +177,10 @@ class Haro {
};

if (this.data.has(key)) {
if (!batch) {
this.loading = true;
}

if (!batch && this.uri) {
if (this.patch) {
this.request(concatURI(this.uri, null), {
Expand Down Expand Up @@ -196,10 +209,18 @@ class Haro {
}

return defer.promise.then(arg => {
if (!batch) {
this.loading = false;
}

this.ondelete(arg);

return arg;
}, e => {
if (!batch) {
this.loading = false;
}

this.onerror("delete", e);
throw e;
});
Expand Down Expand Up @@ -613,6 +634,10 @@ class Haro {
}
}

if (!batch) {
this.loading = true;
}

if (!batch && this.uri) {
luri = concatURI(this.uri, lkey);

Expand Down Expand Up @@ -650,10 +675,18 @@ class Haro {
}

return defer.promise.then(arg => {
if (!batch) {
this.loading = false;
}

this.onset(arg);

return arg;
}, e => {
if (!batch) {
this.loading = false;
}

this.onerror("set", e);
throw e;
});
Expand Down
Loading

0 comments on commit b8d4939

Please sign in to comment.