Skip to content

Commit

Permalink
Adding before* handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Feb 5, 2017
1 parent f60219f commit 627ecec
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 7 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,36 @@ const store = haro(null, {
});
```

**beforeBatch**
_Function_

Event listener for before a batch operation, receives `type`, `data`.

**beforeClear**
_Function_

Event listener for before clearing the data store.

**beforeDelete**
_Function_

Event listener for before a record is deleted, receives `key`, `batch`.

**beforeRequest**
_Function_

Event listener for transforming an API response, receives `uri`, `config`.

**beforeSet**
_Function_

Event listener for before a record is set, receives `key`, `data`.

**beforeSync**
_Function_

Event listener for synchronizing with an API, receives `uri`, `clear`.

**config**
_Object_

Expand Down
25 changes: 23 additions & 2 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.1.16
* @version 3.2.0
*/
"use strict";

Expand Down Expand Up @@ -393,6 +393,7 @@
const defer = deferred(),
fn = type === "del" ? i => this.del(i, true) : i => this.set(null, i, true, true, lload);

this.beforeBatch(args, type);
Promise.all(args.map(fn)).then(defer.resolve, defer.reject);

return defer.promise.then(arg => {
Expand All @@ -409,7 +410,20 @@
});
}

beforeBatch () {}

beforeClear () {}

beforeDelete () {}

beforeRequest () {}

beforeSet () {}

beforeSync () {}

clear () {
this.beforeClear();
this.total = 0;
this.registry.length = 0;
this.data.clear();
Expand Down Expand Up @@ -452,6 +466,8 @@

let index;

this.beforeDelete(key, batch);

if (og) {
index = this.registry.indexOf(key);

Expand Down Expand Up @@ -761,6 +777,8 @@
delete cfg.body;
}

this.beforeRequest(input, cfg);

fetch(input, cfg).then(res => {
const status = res.status,
headers = {};
Expand Down Expand Up @@ -843,6 +861,8 @@
key = this.key && x[this.key] !== undefined ? x[this.key] : uuid();
}

this.beforeSet(key, data, batch, override, lload, retry);

if (!this.data.has(key)) {
this.registry[this.total] = key;
++this.total;
Expand Down Expand Up @@ -925,6 +945,7 @@
const defer = deferred();

this.uri = uri;
this.beforeSync(uri, clear);

if (this.uri) {
this.sync(clear).then(defer.resolve, defer.reject);
Expand Down Expand Up @@ -1155,7 +1176,7 @@
}

factory.transform = cast;
factory.version = "3.1.16";
factory.version = "3.2.0";

// Node, AMD & window supported
if (typeof exports !== "undefined") {
Expand Down
31 changes: 29 additions & 2 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.1.16
* @version 3.2.0
*/
"use strict";

Expand Down Expand Up @@ -440,6 +440,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
return _this2.set(null, i, true, true, lload);
};

this.beforeBatch(args, type);
Promise.all(args.map(fn)).then(defer.resolve, defer.reject);

return defer.promise.then(function (arg) {
Expand All @@ -455,9 +456,28 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
throw e;
});
}
}, {
key: "beforeBatch",
value: function beforeBatch() {}
}, {
key: "beforeClear",
value: function beforeClear() {}
}, {
key: "beforeDelete",
value: function beforeDelete() {}
}, {
key: "beforeRequest",
value: function beforeRequest() {}
}, {
key: "beforeSet",
value: function beforeSet() {}
}, {
key: "beforeSync",
value: function beforeSync() {}
}, {
key: "clear",
value: function clear() {
this.beforeClear();
this.total = 0;
this.registry.length = 0;
this.data.clear();
Expand Down Expand Up @@ -511,6 +531,8 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons

var index = void 0;

this.beforeDelete(key, batch);

if (og) {
index = this.registry.indexOf(key);

Expand Down Expand Up @@ -912,6 +934,8 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
delete cfg.body;
}

this.beforeRequest(input, cfg);

fetch(input, cfg).then(function (res) {
var status = res.status,
headers = {};
Expand Down Expand Up @@ -1039,6 +1063,8 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
key = this.key && x[this.key] !== undefined ? x[this.key] : uuid();
}

this.beforeSet(key, data, batch, override, lload, retry);

if (!this.data.has(key)) {
this.registry[this.total] = key;
++this.total;
Expand Down Expand Up @@ -1124,6 +1150,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
var defer = deferred();

this.uri = uri;
this.beforeSync(uri, clear);

if (this.uri) {
this.sync(clear).then(defer.resolve, defer.reject);
Expand Down Expand Up @@ -1407,7 +1434,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}

factory.transform = cast;
factory.version = "3.1.16";
factory.version = "3.2.0";

// 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.16",
"version": "3.2.0",
"description": "Harō is a modern immutable DataStore",
"main": "lib/haro.es6.js",
"scripts": {
Expand Down
21 changes: 21 additions & 0 deletions src/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
const defer = deferred(),
fn = type === "del" ? i => this.del(i, true) : i => this.set(null, i, true, true, lload);

this.beforeBatch(args, type);
Promise.all(args.map(fn)).then(defer.resolve, defer.reject);

return defer.promise.then(arg => {
Expand All @@ -57,7 +58,20 @@
});
}

beforeBatch () {}

beforeClear () {}

beforeDelete () {}

beforeRequest () {}

beforeSet () {}

beforeSync () {}

clear () {
this.beforeClear();
this.total = 0;
this.registry.length = 0;
this.data.clear();
Expand Down Expand Up @@ -100,6 +114,8 @@

let index;

this.beforeDelete(key, batch);

if (og) {
index = this.registry.indexOf(key);

Expand Down Expand Up @@ -409,6 +425,8 @@
delete cfg.body;
}

this.beforeRequest(input, cfg);

fetch(input, cfg).then(res => {
const status = res.status,
headers = {};
Expand Down Expand Up @@ -491,6 +509,8 @@
key = this.key && x[this.key] !== undefined ? x[this.key] : uuid();
}

this.beforeSet(key, data, batch, override, lload, retry);

if (!this.data.has(key)) {
this.registry[this.total] = key;
++this.total;
Expand Down Expand Up @@ -573,6 +593,7 @@
const defer = deferred();

this.uri = uri;
this.beforeSync(uri, clear);

if (this.uri) {
this.sync(clear).then(defer.resolve, defer.reject);
Expand Down

0 comments on commit 627ecec

Please sign in to comment.