Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for indexed-db #79

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/Lawnchair.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ Lawnchair.adapter = function (id, obj) {
, indexOf = this.prototype.indexOf
// mix in the adapter
for (var i in obj) {
if (indexOf(implementing, i) === -1) throw 'Invalid adapter! Nonstandard method: ' + i
if (indexOf(implementing, i) === -1) {
if (i != 'async_each')
throw 'Invalid adapter '+id+'! Nonstandard method: ' + i
}
}
// if we made it this far the adapter interface is valid
Lawnchair.adapters.push(obj)
Expand Down Expand Up @@ -133,17 +136,24 @@ Lawnchair.prototype = {
},

// a classic iterator
each: function (callback) {
each: function (callback, done_callback) {
var cb = this.lambda(callback)
var cb2 = this.lambda(done_callback)
// iterate from chain
if (this.__results) {
for (var i = 0, l = this.__results.length; i < l; i++) cb.call(this, this.__results[i], i)
if (cb2) cb2.call(this, null, -1);
}
// otherwise iterate the entire collection
else {
this.all(function(r) {
for (var i = 0, l = r.length; i < l; i++) cb.call(this, r[i], i)
})
if (this.async_each) {
this.async_each(cb, cb2);
} else {
this.all(function(r) {
for (var i = 0, l = r.length; i < l; i++) cb.call(this, r[i], i);
if (cb2) cb2.call(this, null, -1);
})
}
}
return this
}
Expand Down
39 changes: 32 additions & 7 deletions src/adapters/indexed-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Lawnchair.adapter('indexed-db', (function(){
return window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.oIndexedDB || window.msIndexedDB;
};


var READ_ONLY = 0;
var READ_WRITE = 1;
var VERSION_CHANGE = 2;

return {

Expand Down Expand Up @@ -68,7 +70,7 @@ Lawnchair.adapter('indexed-db', (function(){
var self = this;
var win = function (e) { if (callback) { obj.key = e.target.result; self.lambda(callback).call(self, obj) }};

var trans = this.db.transaction(["teststore"], webkitIDBTransaction.READ_WRITE, 0);
var trans = this.db.transaction(["teststore"], READ_WRITE, 0);
var store = trans.objectStore("teststore");
var request = obj.key ? store.put(obj, obj.key) : store.put(obj);

Expand All @@ -87,7 +89,7 @@ Lawnchair.adapter('indexed-db', (function(){

var updateProgress = function(obj) {
results.push(obj)
done = results.length === objs.length
done = (results.length === objs.length)
}

var checkProgress = setInterval(function() {
Expand Down Expand Up @@ -154,8 +156,31 @@ Lawnchair.adapter('indexed-db', (function(){
return this;
},

all:function(callback) {
async_each: function (callback, done_callback) {
if(!this.store) {
this.waiting.push(function() {
this.async_each(callback, done_callback);
});
return;
}
var self = this;
var objectStore = this.db.transaction("teststore").objectStore("teststore");
var index = 0;
objectStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
if (callback) callback.call(self, cursor.value, index++);
cursor['continue']();
}
else {
if (done_callback) done_callback.call(self, null, -1);
}
};
return this;
},

all:function(callback) {
if(!this.store) {
this.waiting.push(function() {
this.all(callback);
});
Expand All @@ -169,7 +194,7 @@ Lawnchair.adapter('indexed-db', (function(){
var cursor = event.target.result;
if (cursor) {
toReturn.push(cursor.value);
cursor.continue();
cursor['continue']();
}
else {
if (cb) cb.call(self, toReturn);
Expand All @@ -191,7 +216,7 @@ Lawnchair.adapter('indexed-db', (function(){
var self = this;
var win = function () { if (callback) self.lambda(callback).call(self) };

var request = this.db.transaction(["teststore"], webkitIDBTransaction.READ_WRITE).objectStore("teststore").delete(keyOrObj);
var request = this.db.transaction(["teststore"], READ_WRITE).objectStore("teststore")['delete'](keyOrObj);
request.onsuccess = win;
request.onerror = fail;
return this;
Expand All @@ -210,7 +235,7 @@ Lawnchair.adapter('indexed-db', (function(){

try {
this.db
.transaction(["teststore"], webkitIDBTransaction.READ_WRITE)
.transaction(["teststore"], READ_WRITE)
.objectStore("teststore").clear().onsuccess = win;

} catch(e) {
Expand Down
2 changes: 1 addition & 1 deletion test/lawnchair-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ test('scoped variable in shorthand callback', function() {
var tmp = new Lawnchair({name:'temps', record:'tmp'}, function() {
this.nuke(function() {
this.save({a:1}, function() {
this.each('ok(tmp, "this.record is passed to each callback"); QUnit.start()')
this.each('ok(tmp, "this.record is passed to each callback");', 'QUnit.start()')
})
})
})
Expand Down