-
-
Save katowulf/335395ee91ea3db154e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// v0.0.4 | |
var Firebase = require('firebase'); | |
//var FirebaseTokenGenerator = require("firebase-token-generator"); | |
var D = require('JQDeferred'); | |
var _ = require('underscore'); | |
module.exports = function(conf) { | |
var exports = {}; | |
exports.ready = D(function(def) { | |
new Firebase(conf.firebase.url).auth(conf.firebase.key, function(error) { | |
if( error ) { | |
def.reject(error); | |
} | |
else { | |
// must defer this or .info/authenticated is null | |
_.defer(def.resolve); | |
} | |
}); | |
}); | |
exports.path = function() { | |
return _.flatten(arguments).join('/'); | |
}; | |
exports.connect = function(path) { | |
var opts; | |
if(!_.isArray(path) && _.isObject(path)) { | |
opts = path.opts; | |
path = path.path; | |
} | |
var ref = new Firebase(conf.firebase.url).child(exports.path(path)); | |
if( opts ) { | |
_.each(opts, function(v,k) { | |
ref = v === 'undefined'? ref[k]() : ref[k](v); | |
}); | |
} | |
return ref; | |
}; | |
exports.child = exports.connect; | |
exports.validSegment = function(path) { | |
return path && !path.match(/[.$\[\]#\/]/); | |
}; | |
exports.get = function(path) { | |
return D(function(def) { | |
exports.connect(path).once('value', function(ss) { | |
def.resolve(ss.val(), ss.name(), ss); | |
}, def.reject); | |
}); | |
}; | |
exports.push = function(path, data) { | |
return D(function(def) { | |
var ref = exports.connect(path).push(data, function(e) { | |
if( e ) { def.reject(e); } | |
else { def.resolve(ref.name(), ref); } | |
}) | |
}); | |
}; | |
exports.setWithPriority = function(path, data, priority) { | |
return D(function(def) { | |
var ref = exports.connect(path); | |
ref.setWithPriority(data, priority, function(e) { | |
if( e ) { def.reject(e); } | |
else { def.resolve(ref.name(), ref); } | |
}); | |
}); | |
}; | |
exports.set = function(path, data) { | |
return D(function(def) { | |
var ref = exports.connect(path); | |
ref.set(data, function(e) { | |
if( e ) { def.reject(e); } | |
else { def.resolve(ref.name(), ref); } | |
}); | |
}); | |
}; | |
exports.update = function(path, data) { | |
return D(function(def) { | |
var ref = exports.connect(path); | |
ref.update(data, function(e) { | |
if( e ) { def.reject(e); } | |
else { def.resolve(ref.name(), ref); } | |
}) | |
}); | |
}; | |
exports.remove = function(path) { | |
return D(function(def) { | |
var ref = exports.connect(path); | |
ref.remove(function(e) { | |
if( e ) { def.reject(e); } | |
else { def.resolve(ref.name(), ref); } | |
}); | |
}); | |
}; | |
exports.adjust = function(path, amt, opts) { | |
opts = _.extend({unit: 'amount', decimals: 0, create: false, allowNeg: false}, opts); | |
return D(function(def) { | |
var reason; | |
//todo-hack Firebase bug causes transaction to get null if you don't fetch the value first | |
exports.connect(path).once('value', function(ss) { | |
ss.ref().transaction(function(curValue) { | |
if( curValue === null && !opts.create ) { | |
reason = 'record not found'; | |
return; | |
} | |
if( curValue + amt < 0 && !opts.allowNeg ) { | |
reason = 'insufficient '+opts.unit; | |
return; | |
} | |
var v = (curValue || 0) + amt; | |
return round(v, opts.decimals); | |
}, function(error, success, ss) { | |
if( error ) { | |
conf.smtp.error('transaction failed', error); | |
def.reject(error); | |
} | |
else if( success ) { | |
def.resolve(ss.val(), ss.name(), ss); | |
} | |
else { | |
def.reject(reason||'transaction failure'); | |
} | |
}); | |
}); | |
}); | |
}; | |
exports.inc = function(path) { | |
return exports.adjust(path, 1, {decimals: 0, create: true}); | |
}; | |
/** | |
* Runs until iterator returns {boolean}true, and then calls off | |
* @param path | |
* @param evt | |
* @param iterator | |
*/ | |
exports.until = function(path, evt, iterator) { | |
var ref = exports.connect(path), def = D(); | |
var fnx = ref.on(evt, function(ss) { | |
if( iterator(ss) === true ) { | |
ref.off(evt, fnx); | |
def.resolve(); | |
} | |
}, def.reject); | |
return def; | |
}; | |
exports.newId = function(path) { | |
return exports.connect(path).push().name(); | |
}; | |
function round(original, decimals) { | |
decimals || (decimals = 0); | |
var pow = Math.pow(10, Math.abs(decimals)); | |
if( decimals > 0 ) { | |
return Math.round(original*pow)/pow; | |
} | |
else if( decimals < 0 ) { | |
return Math.round(original/pow)*pow; | |
} | |
else { | |
return Math.round(original); | |
} | |
} | |
return exports; | |
}; |
When I try to use this in a firefox addon, I copy this code into lib/firebase.js, and then in main.js, I run `var firebase = require('firebase'), but then I get this error. What's up?
The code is an implementation for node.js, to run in the browser you may be able to use browserify
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When I try to use this in a firefox addon, I copy this code into lib/firebase.js, and then in main.js, I run `var firebase = require('firebase'), but then I get this error. What's up?