Modified when.js to avoid deferring callbacks using setTimeout (this breaks IndexedDB). Added promise-based API wrapper for IndexedDB.

This commit is contained in:
Alan Kligman 2013-05-25 11:15:00 -04:00
parent ced78ca040
commit 1dda23789e
7 changed files with 1410 additions and 723 deletions

3
dist/idbfs.min.js vendored

File diff suppressed because one or more lines are too long

268
examples/deferred-test.html Normal file
View File

@ -0,0 +1,268 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div id="stdout"></div>
</body>
<script>
window.define = function(factory) {
try{ delete window.define; } catch(e){ window.define = void 0; } // IE
window.when = factory();
};
window.define.amd = {};
</script>
<script src='../lib/when.js'></script>
<script>
//var when = require('when');
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
/*
* ObjectStore
*/
function ObjectStore(idbTransaction, name) {
this.idbObjectStore = idbTransaction.objectStore(name);
};
ObjectStore.prototype.add = function add(value, key) {
var idbObjectStore = this.idbObjectStore;
var deferred = when.defer();
var request = idbObjectStore.add.call(idbObjectStore, value, key);
request.onsuccess = function(event) {
deferred.resolve(event);
};
request.onerror = function(event) {
deferred.reject(event);
};
return deferred.promise;
};
ObjectStore.prototype.clear = function clear() {
var idbObjectStore = this.idbObjectStore;
var deferred = when.defer();
var request = idbObjectStore.clear();
request.onsuccess = function(event) {
deferred.resolve(event);
};
request.onerror = function(event) {
deferred.reject(event);
};
return deferred.promise;
};
ObjectStore.prototype.count = function count(key) {
var idbObjectStore = this.idbObjectStore;
var deferred = when.defer();
var request = idbObjectStore.count(key);
request.onsuccess = function(event) {
deferred.resolve(event);
};
request.onerror = function(event) {
deferred.reject(event);
};
return deferred.promise;
};
ObjectStore.prototype.createIndex = function createIndex(name, keyPath, optionalParameters) {
var idbObjectStore = this.idbObjectStore;
return idbObjectStore.createIndex.call(idbObjectStore, name, keyPath, optionalParameters);
};
ObjectStore.prototype.delete = function delete(key) {
var idbObjectStore = this.idbObjectStore;
var deferred = when.defer();
var request = idbObjectStore.delete(key);
request.onsuccess = function(event) {
deferred.resolve(event);
};
request.onerror = function(event) {
deferred.reject(event);
};
return deferred.promise;
};
ObjectStore.prototype.deleteIndex = function deleteIndex(name) {
var idbObjectStore = this.idbObjectStore;
return idbObjectStore.deleteIndex.call(idbObjectStore, name);
};
ObjectStore.prototype.get = function get(key) {
var idbObjectStore = this.idbObjectStore;
var deferred = when.defer();
var request = idbObjectStore.get(key);
request.onsuccess = function(event) {
deferred.resolve(event);
};
request.onerror = function(event) {
deferred.reject(event);
};
return deferred.promise;
};
ObjectStore.prototype.index = function index(name) {
var idbObjectStore = this.idbObjectStore;
return idbObjectStore.index.call(idbObjectStore, name);
};
ObjectStore.prototype.openCursor = function openCursor(range, direction) {
var idbObjectStore = this.idbObjectStore;
var deferred = when.defer();
var request = idbObjectStore.openCursor(range, direction);
request.onsuccess = function(event) {
deferred.resolve(event);
};
request.onerror = function(event) {
deferred.reject(event);
};
return deferred.promise;
};
ObjectStore.prototype.put = function put(value, key) {
var idbObjectStore = this.idbObjectStore;
var deferred = when.defer();
var request = idbObjectStore.put(value, key);
request.onsuccess = function(event) {
deferred.resolve(event);
};
request.onerror = function(event) {
deferred.reject(event);
};
return deferred.promise;
};
/*
* Transaction
*/
function Transaction(idbDatabase, storeNames, mode) {
this.idbTransaction = idbDatabase.transaction(storeNames, mode);
};
Transaction.prototype.objectStore = function objectStore(name) {
var idbTransaction = this.idbTransaction;
return new ObjectStore(idbTransaction, name);
};
Transaction.prototype.abort = function abort() {
var idbTransaction = this.idbTransaction;
return idbTransaction.abort();
};
/*
* Database
*/
function Database(idbDatabase) {
this.idbDatabase = idbDatabase;
this.name = idbDatabase.name;
this.version = idbDatabase.version;
};
Database.prototype.createObjectStore = function createObjectStore(name, optionalParameters) {
var idbDatabase = this.idbDatabase;
return idbDatabase.createObjectStore(name, optionalParameters);
};
Database.prototype.deleteObjectStore = function deleteObjectStore(name) {
var idbDatabase = this.idbDatabase;
return idbDatabase.deleteObjectStore(name);
};
Database.prototype.transaction = function transaction(storeNames, mode) {
var idbDatabase = this.idbDatabase;
return new Transaction(idbDatabase, storeNames, mode);
};
Database.prototype.close = function close() {
var idbDatabase = this.idbDatabase;
return idbDatabase.close();
};
Database.prototype.objectStoreNames = function objectStoreNames() {
var idbDatabase = this.idbDatabase;
return idbDatabase.objectStoreNames;
};
/*
* Factory
*/
function open(name, version) {
var deferred = when.defer();
var request = indexedDB.open(name);
request.onupgradeneeded = function(event) {
var result = new Database(event.target.result);
event.target._result = result;
deferred.notify(event);
};
request.onsuccess = function(event) {
var result = new Database(event.target.result);
event.target._result = result;
deferred.resolve(event);
};
request.onerror = function(event) {
deferred.reject(event);
};
return deferred.promise;
};
function deleteDatabase(name) {
var deferred = when.defer();
var request = indexedDB.deleteDatabase(name);
request.onupgradeneeded = function(event) {
deferred.notify(event);
};
request.onsuccess = function(event) {
deferred.resolve(event);
};
request.onerror = function(event) {
deferred.reject(event);
};
return deferred.promise;
};
var db;
open('test').then(
function(event) {
db = event.target._result;
var transaction = db.transaction(['FILES'], 'readwrite');
var files = transaction.objectStore('FILES');
return files.clear();
},
function(event) {
console.error('error:', event);
},
function(event) {
db = event.target._result;
if(db.objectStoreNames().contains('FILES')) {
db.deleteObjectStore('FILES');
}
var files = db.createObjectStore('FILES');
}
).then(
function(event) {
var transaction = db.transaction(['FILES'], 'readwrite');
var files = transaction.objectStore('FILES');
return files.put('value', 'key');
}
).then(
function(event) {
var transaction = db.transaction(['FILES'], 'readwrite');
var files = transaction.objectStore('FILES');
return files.get('key');
}
).then(
function(event) {
console.info('result', event.target.result);
}
).otherwise(function(event) {
console.error('error: ', event);
});
</script>
</html>

12
examples/test5.html Normal file
View File

@ -0,0 +1,12 @@
var fs = new FileSystem('packages');
var fd = fs.open('/myfile.txt', fs.RW);
var buffer = new Uint8Array(16);
fd.write(buffer).then(function(nbytes) {
fd.seek(-nbytes);
return fd.read(buffer);
}).then(function(nbytes) {
});

File diff suppressed because it is too large Load Diff

48
src/event.js Normal file
View File

@ -0,0 +1,48 @@
define function(require) {
var _ = require('lodash');
function Event(type, data) {
this.type = type;
this.data = data;
};
function EventSource(types) {
var events = this.events = {};
_(types).forEach(function(type) {
events[type] = [];
});
this.on = function on(type, callback) {
if(!_(events).has(type)) {
throw new Error('unsupported event: ' + type);
}
events[type].push(callback);
};
this.off = function off(type, callback) {
if(!_(events).has(type)) {
throw new Error('unsupported event: ' + type);
}
events[type] = _(events[type]).without(callback);
};
this.emit = function emit(event) {
if(!_(events).has(type)) {
throw new Error('unsupported event: ' + type);
}
_(events[type]).forEach(function(callack) {
callback.call(this, event);
});
};
};
return {
Event: Event,
EventSource: EventSource
};
};

57
src/fs2.js Normal file
View File

@ -0,0 +1,57 @@
define function(require) {
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
var _ = require('lodash');
var when = require('when');
var Path = require('src/path');
var METADATA_STORE_NAME = 'metadata';
var FILE_STORE_NAME = 'files';
var IDB_RO = 'readonly';
var IDB_RW = 'readwrite';
function Operation(fs, stores, mode) {
};
var FS_FORMAT = 'format';
function FileSystem(name, flags) {
var format = _(flags).contains(FS_FORMAT);
var openRequest = indexedDB.open(name);
};
FileSystem.prototype.open = function open(transaction, path, flags, mode) {
};
FileSystem.prototype.opendir = function opendir(transaction, path) {
};
FileSystem.prototype.mkdir = function mkdir(transaction, path) {
};
FileSystem.prototype.rmdir = function rmdir(transaction, path) {
};
FileSystem.prototype.stat = function stat(transaction, path) {
};
FileSystem.prototype.link = function link(transaction, oldpath, newpath) {
};
FileSystem.prototype.unlink = function unlink(transaction, path) {
};
FileSystem.prototype.getxattr = function getxattr(transaction, path, name) {
};
FileSystem.prototype.setxattr = function setxattr(transaction, path, name, value) {
};
return {
FileSystem: FileSystem
};
};

218
src/indexeddb.js Normal file
View File

@ -0,0 +1,218 @@
define function(require) {
var when = require('when');
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
/*
* ObjectStore
*/
function ObjectStore(idbTransaction, name) {
this.idbObjectStore = idbTransaction.objectStore(name);
};
ObjectStore.prototype.add = function add(value, key) {
var idbObjectStore = this.idbObjectStore;
var deferred = when.defer();
var request = idbObjectStore.add.call(idbObjectStore, value, key);
request.onsuccess = function(event) {
deferred.resolve(event);
};
request.onerror = function(event) {
deferred.reject(event);
};
return deferred.promise;
};
ObjectStore.prototype.clear = function clear() {
var idbObjectStore = this.idbObjectStore;
var deferred = when.defer();
var request = idbObjectStore.clear();
request.onsuccess = function(event) {
deferred.resolve(event);
};
request.onerror = function(event) {
deferred.reject(event);
};
return deferred.promise;
};
ObjectStore.prototype.count = function count(key) {
var idbObjectStore = this.idbObjectStore;
var deferred = when.defer();
var request = idbObjectStore.count(key);
request.onsuccess = function(event) {
deferred.resolve(event);
};
request.onerror = function(event) {
deferred.reject(event);
};
return deferred.promise;
};
ObjectStore.prototype.createIndex = function createIndex(name, keyPath, optionalParameters) {
var idbObjectStore = this.idbObjectStore;
return idbObjectStore.createIndex.call(idbObjectStore, name, keyPath, optionalParameters);
};
ObjectStore.prototype.delete = function delete(key) {
var idbObjectStore = this.idbObjectStore;
var deferred = when.defer();
var request = idbObjectStore.delete(key);
request.onsuccess = function(event) {
deferred.resolve(event);
};
request.onerror = function(event) {
deferred.reject(event);
};
return deferred.promise;
};
ObjectStore.prototype.deleteIndex = function deleteIndex(name) {
var idbObjectStore = this.idbObjectStore;
return idbObjectStore.deleteIndex.call(idbObjectStore, name);
};
ObjectStore.prototype.get = function get(key) {
var idbObjectStore = this.idbObjectStore;
var deferred = when.defer();
var request = idbObjectStore.get(key);
request.onsuccess = function(event) {
deferred.resolve(event);
};
request.onerror = function(event) {
deferred.reject(event);
};
return deferred.promise;
};
ObjectStore.prototype.index = function index(name) {
var idbObjectStore = this.idbObjectStore;
return idbObjectStore.index.call(idbObjectStore, name);
};
ObjectStore.prototype.openCursor = function openCursor(range, direction) {
var idbObjectStore = this.idbObjectStore;
var deferred = when.defer();
var request = idbObjectStore.openCursor(range, direction);
request.onsuccess = function(event) {
deferred.resolve(event);
};
request.onerror = function(event) {
deferred.reject(event);
};
return deferred.promise;
};
ObjectStore.prototype.put = function put(value, key) {
var idbObjectStore = this.idbObjectStore;
var deferred = when.defer();
var request = idbObjectStore.put(value, key);
request.onsuccess = function(event) {
deferred.resolve(event);
};
request.onerror = function(event) {
deferred.reject(event);
};
return deferred.promise;
};
/*
* Transaction
*/
function Transaction(idbDatabase, storeNames, mode) {
this.idbTransaction = idbDatabase.transaction(storeNames, mode);
};
Transaction.prototype.objectStore = function objectStore(name) {
var idbTransaction = this.idbTransaction;
return new ObjectStore(idbTransaction, name);
};
Transaction.prototype.abort = function abort() {
var idbTransaction = this.idbTransaction;
return idbTransaction.abort();
};
/*
* Database
*/
function Database(idbDatabase) {
this.idbDatabase = idbDatabase;
this.name = idbDatabase.name;
this.version = idbDatabase.version;
};
Database.prototype.createObjectStore = function createObjectStore(name, optionalParameters) {
var idbDatabase = this.idbDatabase;
return idbDatabase.createObjectStore(name, optionalParameters);
};
Database.prototype.deleteObjectStore = function deleteObjectStore(name) {
var idbDatabase = this.idbDatabase;
return idbDatabase.deleteObjectStore(name);
};
Database.prototype.transaction = function transaction(storeNames, mode) {
var idbDatabase = this.idbDatabase;
return new Transaction(idbDatabase, storeNames, mode);
};
Database.prototype.close = function close() {
var idbDatabase = this.idbDatabase;
return idbDatabase.close();
};
Database.prototype.objectStoreNames = function objectStoreNames() {
var idbDatabase = this.idbDatabase;
return idbDatabase.objectStoreNames;
};
/*
* Factory
*/
function open(name, version) {
var deferred = when.defer();
var request = indexedDB.open(name);
request.onupgradeneeded = function(event) {
var result = new Database(event.target.result);
event.target._result = result;
deferred.notify(event);
};
request.onsuccess = function(event) {
var result = new Database(event.target.result);
event.target._result = result;
deferred.resolve(event);
};
request.onerror = function(event) {
deferred.reject(event);
};
return deferred.promise;
};
function deleteDatabase(name) {
var deferred = when.defer();
var request = indexedDB.deleteDatabase(name);
request.onupgradeneeded = function(event) {
deferred.notify(event);
};
request.onsuccess = function(event) {
deferred.resolve(event);
};
request.onerror = function(event) {
deferred.reject(event);
};
return deferred.promise;
};
return {
open: open,
deleteDatabase: deleteDatabase
};
});