1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-22 17:02:17 -05:00

upgrade lawnchair deps... fixed bugs

This commit is contained in:
Tankred Hase 2013-06-03 03:49:43 +02:00
parent 5a2a559485
commit 6224d01afd
3 changed files with 61 additions and 81 deletions

View File

@ -9,11 +9,9 @@ Lawnchair.adapter('indexed-db', (function(){
function fail(e, i) { console.error('error in indexed-db adapter!', e, i); } function fail(e, i) { console.error('error in indexed-db adapter!', e, i); }
var STORE_NAME = 'lawnchair';
// update the STORE_VERSION when the schema used by this adapter changes // update the STORE_VERSION when the schema used by this adapter changes
// (for example, if you change the STORE_NAME above) // (for example, if you change the STORE_NAME above)
var STORE_VERSION = 2; var STORE_VERSION = 3;
var getIDB = function() { var getIDB = function() {
return window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.oIndexedDB || window.msIndexedDB; return window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.oIndexedDB || window.msIndexedDB;
@ -56,10 +54,11 @@ Lawnchair.adapter('indexed-db', (function(){
var request = this.idb.open(this.name, STORE_VERSION); var request = this.idb.open(this.name, STORE_VERSION);
var self = this; var self = this;
var cb = self.fn(self.name, callback); var cb = self.fn(self.name, callback);
if (cb && typeof cb != 'function') throw 'callback not valid';
var win = function() { var win = function() {
// manually clean up event handlers on request; this helps on chrome // manually clean up event handlers on request; this helps on chrome
request.onupgradeneeded = request.onsuccess = request.error = null; request.onupgradeneeded = request.onsuccess = request.error = null;
return cb.call(self, self); if(cb) return cb.call(self, self);
}; };
var upgrade = function(from, to) { var upgrade = function(from, to) {
@ -68,13 +67,13 @@ Lawnchair.adapter('indexed-db', (function(){
self.db.deleteObjectStore('teststore'); // old adapter self.db.deleteObjectStore('teststore'); // old adapter
} catch (e1) { /* ignore */ } } catch (e1) { /* ignore */ }
try { try {
self.db.deleteObjectStore(STORE_NAME); self.db.deleteObjectStore(self.record);
} catch (e2) { /* ignore */ } } catch (e2) { /* ignore */ }
// ok, create object store. // ok, create object store.
var params = {}; var params = {};
if (self.useAutoIncrement) { params.autoIncrement = true; } if (self.useAutoIncrement) { params.autoIncrement = true; }
self.db.createObjectStore(STORE_NAME, params); self.db.createObjectStore(self.record, params);
self.store = true; self.store = true;
}; };
request.onupgradeneeded = function(event) { request.onupgradeneeded = function(event) {
@ -84,7 +83,7 @@ Lawnchair.adapter('indexed-db', (function(){
// will end up in onsuccess callback // will end up in onsuccess callback
}; };
request.onsuccess = function(event) { request.onsuccess = function(event) {
self.db = request.result; self.db = event.target.result;
if(self.db.version != (''+STORE_VERSION)) { if(self.db.version != (''+STORE_VERSION)) {
// DEPRECATED API: modern implementations will fire the // DEPRECATED API: modern implementations will fire the
@ -107,7 +106,7 @@ Lawnchair.adapter('indexed-db', (function(){
}; };
setVrequest.onerror = function(e) { setVrequest.onerror = function(e) {
setVrequest.onsuccess = setVrequest.onerror = null; setVrequest.onsuccess = setVrequest.onerror = null;
console.log("Failed to create objectstore " + e); console.error("Failed to create objectstore " + e);
fail(e); fail(e);
}; };
} else { } else {
@ -131,56 +130,35 @@ Lawnchair.adapter('indexed-db', (function(){
}, },
save:function(obj, callback) { save:function(obj, callback) {
var self = this;
if(!this.store) { if(!this.store) {
this.waiting.push(function() { this.waiting.push(function() {
this.save(obj, callback); this.save(obj, callback);
}); });
return; return;
} }
var self = this, request; var objs = (this.isArray(obj) ? obj : [obj]).map(function(o){if(!o.key) { o.key = self.uuid()} return o})
var win = function (e) { var win = function (e) {
// manually clean up event handlers; helps free memory on chrome. if (callback) { self.lambda(callback).call(self, self.isArray(obj) ? objs : objs[0] ) }
request.onsuccess = request.onerror = null;
if (callback) { obj.key = e.target.result; self.lambda(callback).call(self, obj) }
}; };
var trans = this.db.transaction(STORE_NAME, READ_WRITE); var trans = this.db.transaction(this.record, READ_WRITE);
var store = trans.objectStore(STORE_NAME); var store = trans.objectStore(this.record);
if (obj.key) {
request = store.put(obj, obj.key); for (var i = 0; i < objs.length; i++) {
} else if (this.useAutoIncrement) { var o = objs[i];
request = store.put(obj); // use autoIncrementing key. store.put(o, o.key);
} else {
request = store.put(obj, this.uuid()); // use randomly-generated key
} }
store.transaction.oncomplete = win;
request.onsuccess = win; store.transaction.onabort = fail;
request.onerror = fail;
return this; return this;
}, },
batch: function (objs, callback) { batch: function (objs, callback) {
return this.save(objs, callback);
var results = []
, done = objs.length
, self = this
var putOne = function(i) {
self.save(objs[i], function(obj) {
results[i] = obj;
if ((--done) > 0) { return; }
if (callback) {
self.lambda(callback).call(self, results);
}
});
};
for (var i = 0, l = objs.length; i < l; i++)
putOne(i);
return this
}, },
@ -203,14 +181,13 @@ Lawnchair.adapter('indexed-db', (function(){
}; };
if (!this.isArray(key)){ if (!this.isArray(key)){
var req = this.db.transaction(STORE_NAME).objectStore(STORE_NAME).get(key); var req = this.db.transaction(this.record).objectStore(this.record).get(key);
req.onsuccess = function(event) { req.onsuccess = function(event) {
req.onsuccess = req.onerror = null; req.onsuccess = req.onerror = null;
win(event); win(event);
}; };
req.onerror = function(event) { req.onerror = function(event) {
console.log("Failed to find " + key);
req.onsuccess = req.onerror = null; req.onsuccess = req.onerror = null;
fail(event); fail(event);
}; };
@ -248,7 +225,7 @@ Lawnchair.adapter('indexed-db', (function(){
var self = this; var self = this;
var req = this.db.transaction(STORE_NAME).objectStore(STORE_NAME).openCursor(getIDBKeyRange().only(key)); var req = this.db.transaction(self.record).objectStore(this.record).openCursor(getIDBKeyRange().only(key));
req.onsuccess = function(event) { req.onsuccess = function(event) {
req.onsuccess = req.onerror = null; req.onsuccess = req.onerror = null;
@ -260,7 +237,6 @@ Lawnchair.adapter('indexed-db', (function(){
}; };
req.onerror = function(event) { req.onerror = function(event) {
req.onsuccess = req.onerror = null; req.onsuccess = req.onerror = null;
console.log("Failed to test for " + key);
fail(event); fail(event);
}; };
@ -276,7 +252,7 @@ Lawnchair.adapter('indexed-db', (function(){
} }
var cb = this.fn(this.name, callback) || undefined; var cb = this.fn(this.name, callback) || undefined;
var self = this; var self = this;
var objectStore = this.db.transaction(STORE_NAME).objectStore(STORE_NAME); var objectStore = this.db.transaction(this.record).objectStore(this.record);
var toReturn = []; var toReturn = [];
objectStore.openCursor().onsuccess = function(event) { objectStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result; var cursor = event.target.result;
@ -300,7 +276,7 @@ Lawnchair.adapter('indexed-db', (function(){
} }
var cb = this.fn(this.name, callback) || undefined; var cb = this.fn(this.name, callback) || undefined;
var self = this; var self = this;
var objectStore = this.db.transaction(STORE_NAME).objectStore(STORE_NAME); var objectStore = this.db.transaction(this.record).objectStore(this.record);
var toReturn = []; var toReturn = [];
// in theory we could use openKeyCursor() here, but no one actually // in theory we could use openKeyCursor() here, but no one actually
// supports it yet. // supports it yet.
@ -325,30 +301,28 @@ Lawnchair.adapter('indexed-db', (function(){
return; return;
} }
var self = this; var self = this;
if (this.isArray(keyOrArray)) {
// batch remove var toDelete = keyOrArray;
var i, done = keyOrArray.length; if (!this.isArray(keyOrArray)) {
var removeOne = function(i) { toDelete=[keyOrArray];
self.remove(keyOrArray[i], function() {
if ((--done) > 0) { return; }
if (callback) {
self.lambda(callback).call(self);
}
});
};
for (i=0; i < keyOrArray.length; i++)
removeOne(i);
return this;
} }
var request;
var win = function () {
request.onsuccess = request.onerror = null; var win = function () {
if (callback) self.lambda(callback).call(self) if (callback) self.lambda(callback).call(self)
}; };
var os = this.db.transaction(this.record, READ_WRITE).objectStore(this.record);
var key = keyOrArray.key ? keyOrArray.key : keyOrArray; var key = keyOrArray.key ? keyOrArray.key : keyOrArray;
request = this.db.transaction(STORE_NAME, READ_WRITE).objectStore(STORE_NAME)['delete'](key); for (var i = 0; i < toDelete.length; i++) {
request.onsuccess = win; var key = toDelete[i].key ? toDelete[i].key : toDelete[i];
request.onerror = fail; os.delete(key);
};
os.transaction.oncomplete = win;
os.transaction.onabort = fail;
return this; return this;
}, },
@ -364,12 +338,15 @@ Lawnchair.adapter('indexed-db', (function(){
, win = callback ? function() { self.lambda(callback).call(self) } : function(){}; , win = callback ? function() { self.lambda(callback).call(self) } : function(){};
try { try {
this.db var os = this.db.transaction(this.record, READ_WRITE).objectStore(this.record);
.transaction(STORE_NAME, READ_WRITE) os.clear();
.objectStore(STORE_NAME).clear().onsuccess = win; os.transaction.oncomplete = win;
os.transaction.onabort = fail;
} catch(e) { } catch (e) {
fail(); if (e.name=='NotFoundError')
win()
else
fail(e);
} }
return this; return this;
} }

View File

@ -1,6 +1,6 @@
Lawnchair.adapter('webkit-sqlite', (function () { Lawnchair.adapter('webkit-sqlite', (function () {
// private methods // private methods
var fail = function (e, i) { console.log('error in sqlite adaptor!', e, i) } var fail = function (e, i) { console.error('error in sqlite adaptor!', e, i) }
, now = function () { return new Date() } // FIXME need to use better date fn , now = function () { return new Date() } // FIXME need to use better date fn
// not entirely sure if this is needed... // not entirely sure if this is needed...
if (!Function.prototype.bind) { if (!Function.prototype.bind) {
@ -27,12 +27,15 @@ Lawnchair.adapter('webkit-sqlite', (function () {
var that = this var that = this
, cb = that.fn(that.name, callback) , cb = that.fn(that.name, callback)
, create = "CREATE TABLE IF NOT EXISTS " + this.record + " (id NVARCHAR(32) UNIQUE PRIMARY KEY, value TEXT, timestamp REAL)" , create = "CREATE TABLE IF NOT EXISTS " + this.record + " (id NVARCHAR(32) UNIQUE PRIMARY KEY, value TEXT, timestamp REAL)"
, win = function(){ return cb.call(that, that); } , win = function(){ if(cb) return cb.call(that, that); }
if (cb && typeof cb != 'function') throw 'callback not valid';
// open a connection and create the db if it doesn't exist // open a connection and create the db if it doesn't exist
this.db = openDatabase(this.name, '1.0.0', this.name, 65536) this.db = openDatabase(this.name, '1.0.0', this.name, 65536)
this.db.transaction(function (t) { this.db.transaction(function (t) {
t.executeSql(create, [], win, fail) t.executeSql(create, [])
}) }, fail, win)
}, },
keys: function (callback) { keys: function (callback) {

View File

@ -21,8 +21,8 @@
<script src="../../src/lib/underscore-1.4.4.min.js"></script> <script src="../../src/lib/underscore-1.4.4.min.js"></script>
<script src="../../src/lib/backbone-1.0.0.min.js"></script> <script src="../../src/lib/backbone-1.0.0.min.js"></script>
<script src="../../src/lib/lawnchair/lawnchair-git.min.js"></script> <script src="../../src/lib/lawnchair/lawnchair-git.min.js"></script>
<script src="../../src/lib/lawnchair/lawnchair-adapter-indexed-db-git.js"></script>
<script src="../../src/lib/lawnchair/lawnchair-adapter-webkit-sqlite-git.js"></script> <script src="../../src/lib/lawnchair/lawnchair-adapter-webkit-sqlite-git.js"></script>
<script src="../../src/lib/lawnchair/lawnchair-adapter-indexed-db-git.js"></script>
<script src="../../src/lib/forge/forge.rsa.bundle.js"></script> <script src="../../src/lib/forge/forge.rsa.bundle.js"></script>
<script src="../../src/lib/uuid.js"></script> <script src="../../src/lib/uuid.js"></script>