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

made the thing async

parent 40764fad
......@@ -15,19 +15,31 @@
"type": "indexeddb",
"database": randomId()
});
this._signature_storage.put("_", {
list: []
});
}
ListStorage.prototype.post = function () {
var id = this._sub_storage.post.apply(this._sub_storage, arguments);
this._signature_storage.get('_').then(function (storage) {
this._signature_storage.put('_', { list: storage.list.concat(id) });
}).fail(function (err) {
throw err;
ListStorage.prototype.list = function () {
// lazily initialize the list in _signature_storage
var ctx = this;
return this._signature_storage.get('_').then(function (list) {
return list;
}).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 () {
......@@ -35,7 +47,16 @@
};
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) {
......@@ -44,11 +65,5 @@
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));
......@@ -24,7 +24,6 @@
/*jslint nomen: true*/
(function (jIO) {
"use strict";
console.log("Nocapacity");
function NoCapacityStorage(spec) {
this._sub_storage = jIO.createJIO(spec.sub_storage);
......
(function (jIO, RSVP, QUnit) {
// (function (jIO, RSVP, QUnit) {
(function (jIO, QUnit) {
"use strict";
QUnit.module("ListStorage");
......@@ -14,7 +14,9 @@
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.expect(1);
......@@ -26,22 +28,41 @@
type: 'memory'
}
}
}),
ids = [jio.post({}), jio.post({}), jio.post({})];
});
RSVP.all(ids).then(
function (values) {
jio.list().then(function (list) {
jio.post({}).then(function (id1) {
jio.post({}).then(function (id2) {
jio.list().then(function (l) {
QUnit.start();
assert.equal(values, jio.list());
}).fail(function (err) {
assert.ok(false, err);
assert.deepEqual(l, [id1, id2]);
});
});
}).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