fireproof
Version:
Promises for Firebase objects.
108 lines (76 loc) • 2.65 kB
JavaScript
Fireproof
new Firebase
Fireproof.prototype.auth = proxyCallback('auth');
Fireproof.prototype.authWithCustomToken = proxyCallback('authWithCustomToken');
Fireproof.prototype.authAnonymously = proxyCallback('authAnonymously');
Fireproof.prototype.authWithPassword = proxyCallback('authWithPassword');
Fireproof.prototype.authWithOAuthPopup = proxyCallback('authWithOAuthPopup');
Fireproof.prototype.authWithOAuthRedirect = proxyCallback('authWithOAuthRedirect');
Fireproof.prototype.authWithOAuthToken = proxyCallback('authWithOAuthToken');
Fireproof.prototype.createUser = proxyCallback('createUser');
Fireproof.prototype.changeEmail = proxyCallback('changeEmail');
Fireproof.prototype.changePassword = proxyCallback('changePassword');
Fireproof.prototype.removeUser = proxyCallback('removeUser');
Fireproof.prototype.resetPassword = proxyCallback('resetPassword');
Fireproof.prototype.getAuth = proxy('getAuth');
Fireproof.prototype.onAuth = proxy('onAuth');
Fireproof.prototype.offAuth = proxy('offAuth');
Fireproof.prototype.unauth = proxy('unauth');
Fireproof.prototype.key = proxy('key');
Fireproof.prototype.name = proxy('name');
Fireproof.prototype.toString = proxy('toString');
child
parent
root
set
update
remove
push
setWithPriority
setPriority
transaction
Fireproof.goOnline = Firebase.goOnline;
Fireproof.goOffline = Firebase.goOffine;
function Fireproof() {
}
function proxy(name) {
var ref = this._ref;
return function() {
return ref[name].apply(ref, arguments);
};
}
function proxyCallback(methodName) {
return function() {
var self = this;
var args = Array.prototype.slice.call(arguments, 0);
var cb;
if (typeof args[args.length-1] === 'function') {
cb = args[args.length-1];
} else {
cb = function() {};
}
var result = promisify(args[args.length-1]);
var promise = args[0];
var newCb = args[1];
args[args.length-1] = newCb;
self._ref[name].call(self._ref, args);
return promise;
};
}
function promisify(cb, self) {
var results = new Array(2);
var newCb = makeResolver(cb);
results[0] = new Fireproof.Promise(newCb);
results[1] = newCb;
function makeResolver(resolve, reject) {
return function() {
if (arguments.length > 0 && arguments[0] !== null && arguments[0] !== undefined) {
reject(err);
} else if (arguments.length > 1) {
resolve(arguments[1]);
}
cb.apply(self, arguments);
};
}
}
;