Skip to content

Commit

Permalink
Fleshing out the interface
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed May 31, 2015
1 parent 8cc5b81 commit a4d5a48
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 86 deletions.
41 changes: 33 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,37 +56,62 @@ URI of an API the DataStore is wired to, in a feedback loop (do not modify, use
**batch( array, type )**
_Promise_

The first argument must be an `Array`, and the second argument must be `del` or `set`
The first argument must be an `Array`, and the second argument must be `del` or `set`.

**clear()**
_self_

Removes all key/value pairs from the DataStore.

**del( key )**
_Promise_

Deletes the record
Deletes the record.

**entries()**
_MapIterator_

Returns returns a new Iterator object that contains an array of [key, value] for each element in the Map object in insertion order.

**forEach(fn[, thisArg])**
_Undefined_

Calls callbackFn once for each key-value pair present in the Map object, in insertion order. If a thisArg parameter is provided to forEach, it will be used as the this value for each callback.

**get( key )**
_Tuple_

Gets the record as a double `Tuple` with the shape `[key, data]`
Gets the record as a double `Tuple` with the shape `[key, data]`.

**keys()**
__Iterator__

Returns a new Iterator object that contains the keys for each element in the Map object in insertion order.Returns a new Iterator object that contains the keys for each element in the Map object in insertion order.

**range( start, end )**
**limit( start, offset )**
_Tuple_

Gets a `Tuple` of double `Tuples` with the shape `[key, data]` for the corresponding range of records
Returns a `Tuple` of double `Tuples` with the shape `[key, data]` for the corresponding range of records.

**request( input, config )**
_Promise_

Returns a `Promise` for a `fetch()` with a coerced response body (JSON or text) as the `resolve()` argument
Returns a `Promise` for a `fetch()` with a coerced response body (JSON or text) as the `resolve()` argument.

**set( key, data, batch=false )**
_Promise_

Returns a `Promise` for setting/amending a record in the DataStore, if `key` is `false` a version 4 `UUID` will be generated
Returns a `Promise` for setting/amending a record in the DataStore, if `key` is `false` a version 4 `UUID` will be generated.

**setUri( uri )**
_Promise_

Returns a `Promise` for wiring the DataStore to an API, with the retrieved record set as the `resolve()` argument
Returns a `Promise` for wiring the DataStore to an API, with the retrieved record set as the `resolve()` argument.

**values()**
_Iterator_

Returns a new Iterator object that contains the values for each element in the Map object in insertion order.

### Requirements
- `Map`
Expand Down
82 changes: 60 additions & 22 deletions lib/haro.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Haro {
"content-type": "application/json"
}
};
this.registry = [];
this.key = "";
this.source = "";
this.total = 0;
Expand Down Expand Up @@ -93,24 +94,44 @@ class Haro {
return defer.promise;
}

clear () {
this.total = 0;
this.registry = [];
this.data.clear();

return this;
}

del ( key, batch=false ) {
let defer = deferred();
let defer = deferred(),
index;

let next = () => {
index = this.registry.indexOf( key );

if ( index > -1 ) {
if ( index === 0 ) {
this.registry.shift();
} else if ( index === ( this.registry.length - 1 ) ) {
this.registry.pop();
} else {
this.registry.splice( index, 1 );
}

this.data.delete( key );
--this.total;
}

defer.resolve();
};

if ( this.data.has( key ) ) {
if ( !batch && this.uri ) {
this.request( this.uri.replace( /\?.*/, "" ) + "/" + key, {method: "delete"} ).then( () => {
if ( this.data.has( key ) ) {
this.data.delete( key );
--this.total;
}
defer.resolve();
}, function ( e ) {
this.request( this.uri.replace( /\?.*/, "" ) + "/" + key, {method: "delete"} ).then( next, function ( e ) {
defer.reject( e.message || e );
});
} else {
this.data.delete( key );
--this.total;
defer.resolve();
next()
}
} else {
defer.reject( new Error( "Record not found" ) );
Expand All @@ -119,6 +140,14 @@ class Haro {
return defer.promise;
}

entries () {
return this.data.entries();
}

forEach ( fn, ctx ) {
return this.data.forEach( fn, ctx );
}

get ( key ) {
let output;

Expand All @@ -129,21 +158,24 @@ class Haro {
return output;
}

range ( start=0, end=0 ) {
let i, n, output;
keys () {
return this.data.keys();
}

limit ( start=0, offset=1 ) {
let i = start,
nth = start + offset,
list = [];

if ( start === end || end > start ) {
if ( i === nth || i > nth || nth > this.total ) {
throw new Error( "Invalid range" );
} else {
i = start - 1;
n = [];
do {
n.push( this.data[ i ] );
} while ( ++i <= end && i < this.total );
output = tuple.apply( tuple, n );
}

return output;
do {
list.push( this.get( this.registry[ i ] ) );
} while ( ++i < nth );

return tuple.apply( tuple, list );
}

request ( input, config={} ) {
Expand Down Expand Up @@ -174,13 +206,15 @@ class Haro {
if ( !batch && this.uri ) {
this.request( this.uri.replace( /\?.*/, "" ) + "/" + key, { method: method, body: JSON.stringify( ldata ) } ).then( () => {
this.data.set( key, ldata );
this.registry.push( key );
++this.total;
defer.resolve( this.get( key ) );
}, function ( e ) {
defer.reject( e.message || e );
} );
} else {
this.data.set( key, ldata );
this.registry.push( key );
++this.total;
defer.resolve( this.get( key ) );
}
Expand Down Expand Up @@ -221,6 +255,10 @@ class Haro {

return defer.promise;
}

values () {
return this.data.values();
}
}

function factory ( data=null, config={} ) {
Expand Down
93 changes: 67 additions & 26 deletions lib/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
"content-type": "application/json"
}
};
this.registry = [];
this.key = "";
this.source = "";
this.total = 0;
Expand Down Expand Up @@ -106,37 +107,68 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons

return defer.promise;
}
}, {
key: "clear",
value: function clear() {
this.total = 0;
this.registry = [];
this.data.clear();

return this;
}
}, {
key: "del",
value: function del(key) {
var _this2 = this;

var batch = arguments[1] === undefined ? false : arguments[1];

var defer = deferred();
var defer = deferred(),
index = undefined;

var next = function next() {
index = _this2.registry.indexOf(key);

if (index > -1) {
if (index === 0) {
_this2.registry.shift();
} else if (index === _this2.registry.length - 1) {
_this2.registry.pop();
} else {
_this2.registry.splice(index, 1);
}

_this2.data["delete"](key);
--_this2.total;
}

defer.resolve();
};

if (this.data.has(key)) {
if (!batch && this.uri) {
this.request(this.uri.replace(/\?.*/, "") + "/" + key, { method: "delete" }).then(function () {
if (_this2.data.has(key)) {
_this2.data["delete"](key);
--_this2.total;
}
defer.resolve();
}, function (e) {
this.request(this.uri.replace(/\?.*/, "") + "/" + key, { method: "delete" }).then(next, function (e) {
defer.reject(e.message || e);
});
} else {
this.data["delete"](key);
--this.total;
defer.resolve();
next();
}
} else {
defer.reject(new Error("Record not found"));
}

return defer.promise;
}
}, {
key: "entries",
value: function entries() {
return this.data.entries();
}
}, {
key: "forEach",
value: function forEach(fn, ctx) {
return this.data.forEach(fn, ctx);
}
}, {
key: "get",
value: function get(key) {
Expand All @@ -149,27 +181,29 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
return output;
}
}, {
key: "range",
value: function range() {
key: "keys",
value: function keys() {
return this.data.keys();
}
}, {
key: "limit",
value: function limit() {
var start = arguments[0] === undefined ? 0 : arguments[0];
var end = arguments[1] === undefined ? 0 : arguments[1];
var offset = arguments[1] === undefined ? 1 : arguments[1];

var i = undefined,
n = undefined,
output = undefined;
var i = start,
nth = start + offset,
list = [];

if (start === end || end > start) {
if (i === nth || i > nth || nth > this.total) {
throw new Error("Invalid range");
} else {
i = start - 1;
n = [];
do {
n.push(this.data[i]);
} while (++i <= end && i < this.total);
output = tuple.apply(tuple, n);
}

return output;
do {
list.push(this.get(this.registry[i]));
} while (++i < nth);

return tuple.apply(tuple, list);
}
}, {
key: "request",
Expand Down Expand Up @@ -209,13 +243,15 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
if (!batch && this.uri) {
this.request(this.uri.replace(/\?.*/, "") + "/" + key, { method: method, body: JSON.stringify(ldata) }).then(function () {
_this3.data.set(key, ldata);
_this3.registry.push(key);
++_this3.total;
defer.resolve(_this3.get(key));
}, function (e) {
defer.reject(e.message || e);
});
} else {
this.data.set(key, ldata);
this.registry.push(key);
++this.total;
defer.resolve(this.get(key));
}
Expand Down Expand Up @@ -259,6 +295,11 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons

return defer.promise;
}
}, {
key: "values",
value: function values() {
return this.data.values();
}
}]);

return Haro;
Expand Down
2 changes: 1 addition & 1 deletion lib/haro.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a4d5a48

Please sign in to comment.