UNPKG

fireproof

Version:
4 lines 16.8 kB
/*! fireproof 3.0.4, © 2015 J2H2 Inc. ISC License. * http://github.com/casetext/fireproof.git */ !function(t,r){"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?module.exports=r():t.Fireproof=r()}(this,function(){"use strict";function t(r,e){if(t.Promise){if("function"!=typeof t.Promise)throw new Error("The supplied value of Fireproof.Promise is not a constructor (got "+t.Promise+")")}else try{t.Promise=Promise}catch(n){throw new Error("You must supply a Promise library to Fireproof!")}this._ref=r,e&&e.then?this.then=e.then.bind(e):this.then=function(t,r){return this.once("value",function(){}).then(t||null,r||null)}}function r(t,r){return"function"!=typeof t&&void 0===typeof r?t:r}function e(r,e){return this instanceof t.Demux?(arguments.length>1&&!Array.isArray(r)&&(r=Array.prototype.slice.call(arguments,0)),this._limit=void 0!==e?e:!0,this._refs=r,this._positions=r.reduce(function(t,r){return t[r.ref().toString()]={name:void 0,priority:void 0},t},{}),this._previousPromise=t.Promise.resolve([]),void(this._buffer=[])):new t.Demux(r)}function n(t){this._items=[],this.keys=[],this.values=[],this.priorities=[],t&&(this.errorHandler=t)}function o(t,r){var e=Number(t[".key"]),n=Number(r[".key"]);return isNaN(e)||isNaN(n)?isNaN(e)?isNaN(n)?t[".key"].localeCompare(r[".key"]):1:-1:e-n}function i(t){return function(r,e){var n,i,s=r[".value"],u=e[".value"];return"object"==typeof s?(n=s[t],void 0===n&&(n=null)):n=null,"object"==typeof u?(i=u[t],void 0===i&&(i=null)):i=null,null===n&&null===i?o(r,e):null===n?-1:null===i?1:n===!1&&i===!1?o(r,e):n===!1?-1:i===!1?1:n===!0&&i===!0?o(r,e):n===!0?-1:i===!0?1:"number"==typeof n&&"number"==typeof i?n-i!==0?n-i:o(r,e):"number"==typeof n?-1:"number"==typeof i?1:"string"==typeof n&&"string"==typeof i?n===i?o(r,e):n.localeCompare(i):"string"==typeof n?-1:"string"==typeof i?1:o(r,e)}}function s(t,r){var e=t[".priority"],n=r[".priority"];if(null===e&&null===n)return o(t,r);if(null===e)return-1;if(null===n)return 1;if("number"==typeof e&&"number"==typeof n)return e-n===0?o(t,r):e-n;if("number"==typeof e)return-1;if("number"==typeof n)return 1;var i=e.localeCompare(n);return 0===i?o(t,r):i}function u(t){this._od=t.onDisconnect()}function a(r,e){if(arguments.length<1)throw new Error("Not enough arguments to Pager");this._mainRef=r.ref(),this._resetCurrentOperation(),this.hasNext=!0,this.hasPrevious=!1;var n;n=e?this.next(e):t.Promise.resolve([]),this.then=n.then.bind(n)}function p(t){this._snap=t}t.bless=function(r){t.Promise=function(t){var e=r.defer();this.then=e.promise.then.bind(e.promise),t(e.resolve.bind(e),e.reject.bind(e))},t.Promise.all=r.all,t.Promise.resolve=function(r){return new t.Promise(function(t){t(r)})},t.Promise.reject=function(r){return new t.Promise(function(t,e){e(r)})}},t.setNextTick=function(r){t.__nextTick=r},t._nextTick=function(r){t.__nextTick?t.__nextTick(r,0):setTimeout(r,0)},t._handleError=function(r){var e,n,o=new t.Promise(function(t,r){e=t,n=r}),i=function(o,s){var u=this,a=arguments;i.id&&t.stats._finish(i.id,o),r&&"function"==typeof r&&t._nextTick(function(){r.apply(u,a)}),o?n(o):e(s)};return i.promise=o,i},t.prototype.child=function(r){return new t(this._ref.child(r))},t.prototype.parent=function(){return null===this._ref.parent()?null:new t(this._ref.parent())},t.prototype.root=function(){return new t(this._ref.root())},t.prototype.toFirebase=function(){return this._ref},t.prototype.name=function(){return this._ref.name()},t.prototype.key=function(){return this._ref.key()},t.prototype.toString=function(){return this._ref.toString()};var f=[];return t._wrapAuth=function(t){function r(){if(!f.authing&&f[0]){f.authing=!0;var t=f.pop();t().then(e,e)}}function e(){f.authing=!1,r()}f.push(t),r()},t.prototype.auth=function(e,n,o){var i=t._handleError(n);o=r(n,o);var s=this;return t._wrapAuth(function(){return s._ref.auth(e,i,o),i.promise}),i.promise},t.prototype.authWithCustomToken=function(e,n,o){var i=t._handleError(n);o=r(n,o);var s=this;return t._wrapAuth(function(){return s._ref.authWithCustomToken(e,i,o),i.promise}),i.promise},t.prototype.authAnonymously=function(e,n){var o=t._handleError(e);n=r(e,n);var i=this;return t._wrapAuth(function(){i._ref.authAnonymously(o,n)}),o.promise},t.prototype.authWithPassword=function(e,n,o){var i=t._handleError(n);o=r(n,o);var s=this;return t._wrapAuth(function(){return s._ref.authWithPassword(e,i,o),i.promise}),i.promise},t.prototype.authWithOAuthPopup=function(e,n,o){var i=t._handleError(n);o=r(n,o);var s=this;return t._wrapAuth(function(){return s._ref.authWithOAuthPopup(e,i,o),i.promise}),i.promise},t.prototype.authWithOAuthRedirect=function(e,n,o){var i=t._handleError(n);o=r(n,o);var s=this;return t._wrapAuth(function(){return s._ref.authWithOAuthRedirect(e,i,o),i.promise}),i.promise},t.prototype.authWithOAuthToken=function(e,n,o,i){var s=t._handleError(o);i=r(o,i);var u=this;return t._wrapAuth(function(){return u._ref.authWithOAuthToken(e,n,s,i),s.promise}),s.promise},t.prototype.getAuth=function(){return this._ref.getAuth()},t.prototype.onAuth=function(t,r){return this._ref.onAuth(t,r)},t.prototype.offAuth=function(t,r){return this._ref.offAuth(t,r)},t.prototype.unauth=function(){return this._ref.unauth()},e.prototype.get=function(r){var e=this;return e._previousPromise=e._previousPromise.then(function(){return e._buffer.length>=r?e._buffer.splice(0,r):t.Promise.all(e._refs.map(function(t){var n,o=e._positions[t.ref().toString()].priority,i=e._positions[t.ref().toString()].name;return n=o&&i?t.startAt(o,i):o?t.startAt(o):t.startAt(),e._limit?n.limitToFirst(r-e._buffer.length):n})).then(e._concatenateResults.bind(e)).then(function(){return e._buffer.splice(0,r)})}),e._previousPromise},e.prototype._concatenateResults=function(t){var r=this,e=t.reduce(function(t,e){var n=e.ref().toString();return e.forEach(function(e){var o=r._positions[n];(o.priority!==e.getPriority()||o.name!==e.key())&&(t.push(e),o.priority=e.getPriority(),o.name=e.key())}),t},[]);r._buffer=r._buffer.concat(e).sort(function(t,r){var e=t.getPriority(),n=r.getPriority(),o=t.key(),i=r.key();return typeof e!=typeof n?null===e?-1:null===n?1:"number"==typeof e?-1:1:null===e?o.localeCompare(i):"number"==typeof e?e-n||o.localeCompare(i):"string"==typeof e?e.localeCompare(n)||o.localeCompare(i):void 0})},t.Demux=e,n.firebaseKeySort=o,n.firebaseChildSort=i,n.firebasePrioritySort=s,n.prototype.connect=function(t,r,e){function n(t){o.error=t,o.disconnect(),o.errorHandler&&o.errorHandler.call(null,t)}var o=this;o.ref=t,o.error=null,o.watchers=[t.on("child_added",function(t){var n={".key":t.key(),".value":t.val(),".priority":t.getPriority()};o._items.push(n),o._sort(r,e)},n),t.on("child_removed",function(t){var n=o.keys.indexOf(t.key());o._items.splice(n,1),o._sort(r,e)},n),t.on("child_changed",function(t){var n=o.keys.indexOf(t.key());o._items[n][".value"]=t.val(),o._sort(r,e)},n),t.on("child_moved",function(t){var n=o.keys.indexOf(t.key());o._items[n][".priority"]=t.getPriority(),o._sort(r,e)},n)]},n.prototype.disconnect=function(){this.ref&&this.watchers&&(this.ref.off("child_added",this.watchers[0]),this.ref.off("child_removed",this.watchers[1]),this.ref.off("child_changed",this.watchers[2]),this.ref.off("child_moved",this.watchers[3]),this.ref=null,this.watchers=null),this._items.length=0,this.keys.length=0,this.values.length=0,this.priorities.length=0},n.prototype._sort=function(t,r){switch(this.keys.length=0,this.values.length=0,this.priorities.length=0,t){case"child":this._items.sort(i(r));break;case"key":this._items.sort(o);break;default:this._items.sort(s)}for(var e=0;e<this._items.length;e++)this.keys[e]=this._items[e][".key"],this.values[e]=this._items[e][".value"],this.priorities[e]=this._items[e][".priority"]},t.LiveArray=n,u.prototype.cancel=function(r){var e=t._handleError(r);return this._od.cancel(e),e.promise},u.prototype.remove=function(r){var e=t._handleError(r);return this._od.remove(e),e.promise},u.prototype.set=function(r,e){var n=t._handleError(e);return this._od.set(r,n),n.promise},u.prototype.setWithPriority=function(r,e,n){var o=t._handleError(n);return this._od.setWithPriority(r,e,o),o.promise},u.prototype.update=function(r,e){var n=t._handleError(e);return this._od.update(r,n),n.promise},t.prototype.onDisconnect=function(){return new u(this._ref)},a.prototype.next=function(r){if(0===arguments.length)throw new Error("Not enough arguments to next");var e,n=this;return n.hasNext?n._currentOperation.then(function(){n._direction="next";var t=n._mainRef;return n._page?(e=r+1,t=t.orderByPriority().startAt(n._page.priority,n._page.key).limitToFirst(r+2)):(e=r,t=t.startAt().limitToFirst(r+1)),t.once("value")}).then(function(t){return n._handleResults(t,e)}):t.Promise.resolve([])},a.prototype.previous=function(r){if(0===arguments.length)throw new Error("Not enough arguments to previous");var e=this;return e.hasPrevious?e._currentOperation.then(function(){e._direction="previous";var t=e._mainRef;if(!e._page)throw new Error("Cannot call #previous on a Pager without calling #next first");return t=t.orderByPriority().endAt(e._page.priority,e._page.key).limitToLast(r+2),t.once("value")}).then(function(t){return e._handleResults(t,r+1)}):t.Promise.resolve([])},a.prototype._handleResults=function(t,r){var e=this,n=[];return t.forEach(function(t){n.push(t)}),n="next"===e._direction?e._page?n.slice(1,r):n.slice(0,r):t.numChildren()<=r?n.slice(0,t.numChildren()-1):n.slice(1,r),"next"===e._direction?(this.hasNext=t.numChildren()===r+1,this.hasPrevious=!0):(this.hasPrevious=t.numChildren()===r+1,this.hasNext=!0),n.length>0&&("next"===e._direction?e._page={priority:n[n.length-1].getPriority(),key:n[n.length-1].key()}:e._page={priority:n[0].getPriority(),key:n[0].key()}),e._currentOperationCount--,0===e._currentOperationCount&&e._resetCurrentOperation(),n},a.prototype._resetCurrentOperation=function(){this._currentOperation=t.Promise.resolve(null),this._currentOperationCount=0},t.Pager=a,t.prototype.limit=function(r){return new t(this._ref.limit(r))},t.prototype.limitToFirst=function(r){return new t(this._ref.limitToFirst(r))},t.prototype.limitToLast=function(r){return new t(this._ref.limitToLast(r))},t.prototype.orderByChild=function(r){return new t(this._ref.orderByChild(r))},t.prototype.orderByKey=function(){return new t(this._ref.orderByKey())},t.prototype.orderByValue=function(){return new t(this._ref.orderByValue())},t.prototype.orderByPriority=function(){return new t(this._ref.orderByPriority())},t.prototype.equalTo=function(r,e){return new t(e?this._ref.equalTo(r,e):this._ref.equalTo(r))},t.prototype.startAt=function(r,e){return new t(e?this._ref.startAt(r,e):this._ref.startAt(r))},t.prototype.endAt=function(r,e){return new t(e?this._ref.endAt(r,e):this._ref.endAt(r))},t.prototype.ref=function(){return new t(this._ref.ref())},t.prototype.transaction=function(r,e,n){var o=this,i=t.stats._start("transaction",o);return new t.Promise(function(s,u){o._ref.transaction(r,function(r,n,o){t.stats._finish(i,r),o=new t.Snapshot(o),e&&e(r,n,o),r?u(r):s({committed:n,snapshot:o})},n)})},t.prototype.on=function(r,e,n,o){var i=!1,s=!1,u=this,a=t.stats._start("read",u);t.stats._startListener(u),u._ids||(u._ids=[]),u._ids[r]||(u._ids[r]=[]),u._ids[r].push(a),"function"!=typeof e&&(e=function(){}),"function"!=typeof n&&(n=function(){});var p,f,h=new t.Promise(function(t,r){p=t,f=r}),c=function(n,o){s||(s=!0,u._ids[r].pop(),t.stats._finish(a)),n=new t.Snapshot(n),e(n,o),i||(i=!0,p(n,o))};return c.then=h.then.bind(h),u._ref.on(r,c,function(e){u._ids[r].pop(),t.stats._finish(a,e),t.stats._endListener(u,e),n(e),i||(i=!0,f(e))},o),c},t.prototype.off=function(r,e,n){this._ids&&this._ids[r]&&this._ids[r].length>0&&t.stats._finish(this._ids[r].pop()),t.stats._endListener(this),this._ref.off(r,e,n)},t.prototype.once=function(r,e,n,o){var i=this;return new t.Promise(function(s,u){var a=t.stats._start("read",i);"function"!=typeof e&&(e=function(){}),"function"!=typeof n&&(n=function(){}),i._ref.once(r,function(r){t.stats._finish(a),r=new t.Snapshot(r),s(r),e(r)},function(r){t.stats._finish(a,r),u(r),n(r)},o)})},t.Snapshot=p,p.prototype.exists=function(){return this._snap.exists()},p.prototype.child=function(t){return new p(this._snap.child(t))},p.prototype.forEach=function(t){return this._snap.forEach(function(r){return t(new p(r))===!0?!0:void 0})},p.prototype.hasChild=function(t){return this._snap.hasChild(t)},p.prototype.hasChildren=function(){return this._snap.hasChildren()},p.prototype.numChildren=function(){return this._snap.numChildren()},p.prototype.name=function(){return this._snap.name()},p.prototype.key=function(){return this._snap.key()},p.prototype.val=function(){return this._snap.val()},p.prototype.ref=function(){return new t(this._snap.ref())},p.prototype.getPriority=function(){return this._snap.getPriority()},p.prototype.exportVal=function(){return this._snap.exportVal()},t.stats={_eventSubscribers:{}},t.stats.reset=function(){var r={},e=0;for(var n in t.stats.operationLog){var o=t.stats.operationLog[n];o.hasOwnProperty("finish")||(e++,r[n]=o)}t.stats.operationLog=r,t.stats.runningOperationCount=e,t.stats.operationCount=e},t.stats.resetListeners=function(){t.stats.listeners={},t.stats.listenCount=0},t.stats._start=function(r,e){var n=e.ref().toString(),o=Math.random().toString(36).slice(2);return t.stats.runningOperationCount++,t.stats.operationLog[o]={id:o,type:r,path:n,start:Date.now()},t.stats._emit("start",t.stats.operationLog[o]),"listen"===r&&t.stats.listenCount++,o},t.stats._finish=function(r,e){var n=t.stats.operationLog[r];if(!n)throw new Error("Fireproof: reference to unknown log event "+r);n.finish||(t.stats.runningOperationCount--,t.stats.operationCount++,n.finish=Date.now(),n.duration=n.finish-n.start,e&&(n.error=e)),n.error?t.stats._emit("error",n):t.stats._emit("finish",n)},t.stats._startListener=function(r){var e=r.ref().toString();t.stats.listeners[e]||(t.stats.listeners[e]=0),t.stats.listeners[e]++,t.stats.listenCount++,t.stats._emit("listenStarted",e)},t.stats._endListener=function(r,e){var n=r.ref().toString();t.stats.listeners[n]--,t.stats.listenCount--,t.stats._emit("listenEnded",n,e)},t.stats.getListeners=function(){return t.stats.listeners},t.stats.getListenerCount=function(){return t.stats.listenCount},t.stats.getPathCounts=function(){var r={};for(var e in t.stats.operationLog){var n=t.stats.operationLog[e];r[n.type]||(r[n.type]={}),r[n.type][n.path]?r[n.type][n.path]++:r[n.type][n.path]=1}return r},t.stats.getCounts=function(){var r={};for(var e in t.stats.operationLog){var n=t.stats.operationLog[e];r[n.type]?r[n.type]++:r[n.type]=1}return r},t.stats.on=function(r,e){if("function"==typeof r&&void 0===e&&(e=r,r=null),"function"!=typeof e)throw new Error("Non-function passed to Fireproof.stats.on");return t.stats._eventSubscribers[r]||(t.stats._eventSubscribers[r]=[]),t.stats._eventSubscribers[r].push(e),e},t.stats.off=function(r,e){if("function"==typeof r&&void 0===e&&(e=r,r=null),"function"!=typeof e)throw new Error("Non-function passed to Fireproof.stats.off");if(r){var n=t.stats._eventSubscribers[r],o=n.indexOf(e);-1!==o&&(n[o]=null)}},t.stats._emit=function(r){var e=Array.prototype.slice.call(arguments,1);t.stats._eventSubscribers[r]||(t.stats._eventSubscribers[r]=[]);var n=t.stats._eventSubscribers[r];t._nextTick(function(){for(var t=0;t<n.length;t++)null!==n[t]&&n[t].apply(null,e)})},t.stats.reset(),t.stats.resetListeners(),t.prototype.createUser=function(r,e){var n=t._handleError(e);return this._ref.createUser(r,n),n.promise},t.prototype.changeEmail=function(r,e){var n=t._handleError(e);return this._ref.changeEmail(r,n),n.promise},t.prototype.changePassword=function(r,e){var n=t._handleError(e);return this._ref.changePassword(r,n),n.promise},t.prototype.resetPassword=function(r,e){var n=t._handleError(e);return this._ref.resetPassword(r,n),n.promise},t.prototype.removeUser=function(r,e){var n=t._handleError(e);return this._ref.removeUser(r,n),n.promise},t.prototype.set=function(r,e){var n=t._handleError(e);return n.id=t.stats._start("write",this),this._ref.set(r,n),n.promise},t.prototype.update=function(r,e){var n=t._handleError(e);return n.id=t.stats._start("update",this),this._ref.update(r,n),n.promise},t.prototype.remove=function(r){var e=t._handleError(r);return e.id=t.stats._start("write",this),this._ref.remove(e),e.promise},t.prototype.push=function(r,e){var n=t._handleError(e);n.id=t.stats._start("write",this);var o=new t(this._ref.push(r,n),n.promise);return o},t.prototype.setWithPriority=function(r,e,n){var o=t._handleError(n);return o.id=t.stats._start("write",this),this._ref.setWithPriority(r,e,o),o.promise},t.prototype.setPriority=function(r,e){var n=t._handleError(e);return n.id=t.stats._start("write",this),this._ref.setPriority(r,n),n.promise},t});