Commit 66c28293 authored by Yaxel Perez's avatar Yaxel Perez

made the thing async

parent 40764fad
...@@ -15,19 +15,31 @@ ...@@ -15,19 +15,31 @@
"type": "indexeddb", "type": "indexeddb",
"database": randomId() "database": randomId()
}); });
this._signature_storage.put("_", {
list: []
});
} }
ListStorage.prototype.post = function () { ListStorage.prototype.list = function () {
var id = this._sub_storage.post.apply(this._sub_storage, arguments); // lazily initialize the list in _signature_storage
this._signature_storage.get('_').then(function (storage) { var ctx = this;
this._signature_storage.put('_', { list: storage.list.concat(id) }); return this._signature_storage.get('_').then(function (list) {
}).fail(function (err) { return list;
throw err; }).fail(function () {
return ctx._signature_storage.put('_', []).then(function () {
return [];
});
}); });
return id; };
ListStorage.prototype.post = function () {
var ctx = this;
return this._sub_storage.post.apply(this._sub_storage, arguments)
.then(function (id) {
return ctx.list().then(function (list) {
list.push(id);
return ctx._signature_storage.put('_', list).then(function () {
return id;
});
});
});
}; };
ListStorage.prototype.get = function () { ListStorage.prototype.get = function () {
...@@ -35,7 +47,16 @@ ...@@ -35,7 +47,16 @@
}; };
ListStorage.prototype.put = function () { ListStorage.prototype.put = function () {
return this._sub_storage.put.apply(this._sub_storage, arguments); var ctx = this;
return this._sub_storage.put.apply(this._sub_storage, arguments)
.then(function (id) {
return ctx.list().then(function (list) {
list.push(id);
return ctx._signature_storage.put('_', list).then(function () {
return id;
});
});
});
}; };
ListStorage.prototype.remove = function (id) { ListStorage.prototype.remove = function (id) {
...@@ -44,11 +65,5 @@ ...@@ -44,11 +65,5 @@
this._signature_storage.put("_", { list: updated_list }); this._signature_storage.put("_", { list: updated_list });
}; };
ListStorage.prototype.list = function () {
return this._sub_storage.get("_").then(function (storage) {
return storage.list;
});
};
jIO.addStorage("list", ListStorage); jIO.addStorage("list", ListStorage);
}(jIO)); }(jIO));
...@@ -24,7 +24,6 @@ ...@@ -24,7 +24,6 @@
/*jslint nomen: true*/ /*jslint nomen: true*/
(function (jIO) { (function (jIO) {
"use strict"; "use strict";
console.log("Nocapacity");
function NoCapacityStorage(spec) { function NoCapacityStorage(spec) {
this._sub_storage = jIO.createJIO(spec.sub_storage); this._sub_storage = jIO.createJIO(spec.sub_storage);
......
// (function (jIO, RSVP, QUnit) {
(function (jIO, RSVP, QUnit) { (function (jIO, QUnit) {
"use strict"; "use strict";
QUnit.module("ListStorage"); QUnit.module("ListStorage");
...@@ -14,7 +14,9 @@ ...@@ -14,7 +14,9 @@
QUnit.expect(0); QUnit.expect(0);
}); });
QUnit.test('list method returns ordered list of ids', function (assert) { // NOTE: list method is implicitly tested in the following two methods
QUnit.test('post method correctly records ids', function (assert) {
QUnit.stop(); QUnit.stop();
QUnit.expect(1); QUnit.expect(1);
...@@ -26,22 +28,41 @@ ...@@ -26,22 +28,41 @@
type: 'memory' type: 'memory'
} }
} }
}), });
ids = [jio.post({}), jio.post({}), jio.post({})];
RSVP.all(ids).then( jio.post({}).then(function (id1) {
function (values) { jio.post({}).then(function (id2) {
jio.list().then(function (list) { jio.list().then(function (l) {
QUnit.start(); QUnit.start();
assert.equal(values, jio.list()); assert.deepEqual(l, [id1, id2]);
}).fail(function (err) {
assert.ok(false, err);
}); });
});
}).fail(console.error);
});
QUnit.test('put method correctly records ids', function (assert) {
QUnit.stop();
QUnit.expect(1);
var jio = jIO.createJIO({
type: 'list',
sub_storage: {
type: 'uuid',
sub_storage: {
type: 'memory'
}
} }
).fail(function (err) {
QUnit.start();
assert.ok(false, err);
}); });
jio.put('test', {}).then(function (id1) {
jio.put('test2', {}).then(function (id2) {
jio.list().then(function (l) {
QUnit.start();
assert.deepEqual(l, [id1, id2]);
});
});
}).fail(console.error);
}); });
}(jIO, RSVP, QUnit)); // }(jIO, RSVP, QUnit));
}(jIO, QUnit));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment