Skip to content

gconnolly/deferred-tools

Repository files navigation

Deferred-tools

Deferred tools comprise a couple of lightweight wrappers around common asynchronous APIs. These wrappers adapt the APIs to use jQuery's Deferred object rather than argument based callbacks.

Hey, its not much. But it can be helpful if you have already bought into using the jQuery Deferred object to handle asynchronous operations.

Example

var db = window.openDatabaseDeferred('example', 1, 'Example Database', function (tx) {
	tx.executeSql('CREATE TABLE IF NOT EXISTS Employee ( ID INTEGER PRIMARY KEY, Name VARCHAR )')
		.done(function () {
			console.log('Employee table created.');
		});
});

db.transaction(function (tx) {
	tx.executeSql('INSERT INTO Employee (ID, Name) VALUES (?, ?)', [1, 'John Smith']);
})
.done(function (result) {
	console.log(result.rowsAffected + ' record(s) updated.');
})
.then(function (result) {
	return db.transaction(function (tx) {
		tx.executeSql('SELECT * FROM Employee WHERE ID = ?', [result.insertId]);
	});
})
.then(function (result) {
	console.log(results.rows.item(i).Name + ' was added as an employee.');
})
.fail(function () {
	console.log('Something went wrong.');
})

API

window.openDatabaseDeferred( name, version, displayName, estimatedSize, creationCallback );

SQL Database

db.transaction( callback )
    .done(function ( ) {})
    .fail(function ( error ) {});
db.readTransaction( callback )
    .done(function ( ) {})
    .fail(function ( error ) {});
db.changeVersion( oldVersion, newVersion, callback )
    .done(function ( ) {})
    .fail(function ( error ) {});

SQL Transaction

transaction.executeSql( sqlStatement, sqlArguments, errorCallback )
    .done(function ( transaction, resultSet ) {})
    .fail(function ( transaction, error ) {});

Again, not much to see here beyond what you would expect. Fewer callbacks, more promises, if that is what you want.

API

window.requestFileSystemDeferred( type, size )
	.done(function ( filesystem ) {})
	.fail(function ( error ) {});
window.resolveLocalFileSystemURLDeferred( type, size )
	.done(function ( entry ) {})
	.fail(function ( error ) {});

File Entry

fileEntry.getMetadata()
	.done(function ( metadata ) {})
	.fail(function ( error ) {});
fileEntry.moveTo( parent, newName )
	.done(function ( entry ) {})
	.fail(function ( error ) {});
fileEntry.copyTo( parent, newName )
	.done(function ( entry ) {})
	.fail(function ( error ) {});
fileEntry.remove()
	.done(function () {})
	.fail(function ( error ) {});
fileEntry.getParent()
	.done(function ( entry ) {})
	.fail(function ( error ) {});
fileEntry.createWriter()
	.done(function ( fileWriter ) {})
	.fail(function ( error ) {});
fileEntry.file()
	.done(function ( file ) {})
	.fail(function ( error ) {});

Directory Entry

directoryEntry.getMetadata()
	.done(function ( metadata ) {})
	.fail(function ( error ) {});
directoryEntry.moveTo( parent, newName )
	.done(function ( entry ) {})
	.fail(function ( error ) {});
directoryEntry.copyTo( parent, newName )
	.done(function ( entry ) {})
	.fail(function ( error ) {});
directoryEntry.remove()
	.done(function () {})
	.fail(function ( error ) {});
directoryEntry.getParent()
	.done(function ( entry ) {})
	.fail(function ( error ) {});
directoryEntry.getFile( path, options )
	.done(function ( entry ) {})
	.fail(function ( error ) {});
directoryEntry.getDirectory( path, options )
	.done(function ( entry ) {})
	.fail(function ( error ) {});
directoryEntry.removeRecursively()
	.done(function () {})
	.fail(function ( error ) {});

Directory Reader

directoryReader.readEntries()
	.done(function ( entries ) {})
	.fail(function ( error ) {});

About

Web Database and FileSystem API wrapped to return promises

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published