UNPKG

32.5 kBJavaScriptView Raw
1!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("@angular/fire/firestore"),require("@datorama/akita"),require("rxjs"),require("rxjs/operators"),require("@angular/router"),require("@angular/fire/auth"),require("firebase/app")):"function"==typeof define&&define.amd?define("akita-ng-fire",["exports","@angular/core","@angular/fire/firestore","@datorama/akita","rxjs","rxjs/operators","@angular/router","@angular/fire/auth","firebase/app"],e):e((t=t||self)["akita-ng-fire"]={},t.ng.core,t.ng.fire.firestore,t.akita,t.rxjs,t.rxjs.operators,t.ng.router,t.ng.fire.auth,t.firebase)}(this,(function(t,e,r,n,i,o,a,s,u){"use strict";u=u&&Object.prototype.hasOwnProperty.call(u,"default")?u.default:u;function c(t,e,r,n){return new(r||(r=Promise))((function(i,o){function a(t){try{u(n.next(t))}catch(t){o(t)}}function s(t){try{u(n.throw(t))}catch(t){o(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,s)}u((n=n.apply(t,e||[])).next())}))}function f(t,e){var r,n,i,o,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(o){return function(s){return function(o){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return a.label++,{value:o[1],done:!1};case 5:a.label++,n=o[1],o=[0];continue;case 7:o=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){a=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){a.label=o[1];break}if(6===o[0]&&a.label<i[1]){a.label=i[1],i=o;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(o);break}i[2]&&a.ops.pop(),a.trys.pop();continue}o=e.call(t,a)}catch(t){o=[6,t],n=0}finally{r=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,s])}}}Object.create;function h(t){var e="function"==typeof Symbol&&Symbol.iterator,r=e&&t[e],n=0;if(r)return r.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&n>=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function l(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var n,i,o=r.call(t),a=[];try{for(;(void 0===e||e-- >0)&&!(n=o.next()).done;)a.push(n.value)}catch(t){i={error:t}}finally{try{n&&!n.done&&(r=o.return)&&r.call(o)}finally{if(i)throw i.error}}return a}Object.create;function d(t,e){var r="",n="";if(t.id){if(!e)throw new Error("You should provide the colletion path with the id");r=e+"/"+(n=t.id)}else{if(!t.path)throw new Error('You should provide either an "id" OR a "path".');var i=(r=t.path).split("/");if(i.length%2!=0)throw new Error("Path "+r+" doesn't look like a Firestore's document path");n=i[i.length-1]}return{id:n,path:r}}function p(t,e){n.runStoreAction(t,n.StoreAction.Update,(function(t){return t({loading:e})}))}function y(t){n.runStoreAction(t,n.StoreAction.Update,(function(t){return t([])}))}function b(t,e){n.runStoreAction(t,n.StoreAction.Update,(function(t){return t({active:e})}))}function v(t,e,r){n.runEntityStoreAction(t,n.EntityStoreAction.UpsertEntities,(function(t){return t(r,e)}))}function m(t,e){n.runEntityStoreAction(t,n.EntityStoreAction.RemoveEntities,(function(t){return t(e)}))}function g(t,e,r,i){t?n.applyTransaction((function(){m(e,r),v(e,i,r)})):n.runEntityStoreAction(e,n.EntityStoreAction.UpdateEntities,(function(t){return t(r,i)}))}function w(t,e,r,n,i){var o,a,s;if(void 0===r&&(r="id"),p(t,!1),0!==e.length)try{for(var u=h(e),c=u.next();!c.done;c=u.next()){var f=c.value,l=f.payload.doc.id,d=i(f.payload.doc.data());switch(f.type){case"added":v(t,Object.assign(((s={})[r]=l,s),d),l);break;case"removed":m(t,l);break;case"modified":g(n,t,l,d)}}}catch(t){o={error:t}}finally{try{c&&!c.done&&(a=u.return)&&a.call(u)}finally{if(o)throw o.error}}}function P(t,e,r,n){var i;void 0===r&&(r="id"),p(t,!1);var o=e.payload.id,a=n(e.payload.data());e.payload.exists?v(t,Object.assign(((i={})[r]=o,i),a),o):m(t,o)}function A(t,e){if(void 0===e&&(e={}),!t&&!e.storeName)throw new Error("You should either provide a store name or inject a store instance in constructor");return e.storeName||t.storeName}function j(t){return t.split("/").filter((function(t){return":"===t.charAt(0)})).map((function(t){return t.substr(1)}))}function F(t,e){return t.split("/").map((function(r){if(":"===r.charAt(0)){var n=r.substr(1);if(!e[n])throw new Error("Required parameter "+n+" from "+t+" doesn't exist in params "+JSON.stringify(e));return e[n]}return r})).join("/")}function O(t,e,r){void 0===r&&(r=Object);var n=Object.getPrototypeOf(t);if(n&&n instanceof r)return Object.getOwnPropertyDescriptor(n,e)||O(n,e,r)}function C(t,e,r){var n=O(t,r,e);return n&&n.get&&!0}function S(t){return t&&!!t.get}var x=function(){function t(n,i,o){if(this.store=n,this.collectionPath=i,this.idsToListen={},!C(this,t,"path")&&!this.constructor.path&&!this.collectionPath)throw new Error("You should provide a path to the collection");try{this.db=o||e.inject(r.AngularFirestore)}catch(t){throw new Error("CollectionService requires AngularFirestore.")}}return t.prototype.getPath=function(t){return t&&t.params?F(this.path,t.params):this.currentPath},Object.defineProperty(t.prototype,"idKey",{get:function(){return this.constructor.idKey||this.store?this.store.idKey:"id"},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"path",{get:function(){return this.constructor.path||this.collectionPath},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"currentPath",{get:function(){if(i.isObservable(this.path))throw new Error("Cannot get a snapshot of the path if it is an Observable");return this.path},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"resetOnUpdate",{get:function(){return this.constructor.resetOnUpdate||!1},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"collection",{get:function(){return this.db.collection(this.currentPath)},enumerable:!1,configurable:!0}),t.prototype.formatToFirestore=function(t){return t},t.prototype.formatFromFirestore=function(t){return t},t.prototype.syncCollection=function(t,e,r){var i,o,a=this;void 0===t&&(t=this.currentPath),void 0===r&&(r={loading:!0}),"function"==typeof t?(o=t,i=this.getPath(e)):"object"==typeof t?(r=t,i=this.getPath(r)):i="string"==typeof t?t:this.getPath(r),"function"==typeof e?o=e:"object"==typeof e&&(r=e);var s=A(this.store,r);return r.reset&&y(s),r.loading&&p(s,!0),this.db.collection(i,o).stateChanges().pipe(n.withTransaction((function(t){return w(s,t,a.idKey,a.resetOnUpdate,(function(t){return a.formatFromFirestore(t)}))})))},t.prototype.syncCollectionGroup=function(t,e,r){var i,o,a=this;if(void 0===t&&(t=this.currentPath),void 0===r&&(r={loading:!0}),"string"==typeof t)i=t;else if("function"==typeof t)i=this.currentPath,o=t;else{if("object"!=typeof t)throw new Error("1ier parameter if either a string, a queryFn or a StoreOption");i=this.currentPath,r=t}"function"==typeof e?o=e:"object"==typeof e&&(r=e);var s=A(this.store,r);r.reset&&y(s),r.loading&&p(s,!0);var u=i.split("/").pop();return this.db.collectionGroup(u,o).stateChanges().pipe(n.withTransaction((function(t){return w(s,t,a.idKey,a.resetOnUpdate,(function(t){return a.formatFromFirestore(t)}))})))},t.prototype.syncManyDocs=function(t,e){var r=this;void 0===e&&(e={loading:!0}),i.isObservable(t)||(t=i.of(t));var n=A(this.store,e);return e.reset&&y(n),e.loading&&p(n,!0),t.pipe(o.switchMap((function(t){var a=r.idsToListen[n];if(a){var s=a.filter((function(e){return!t.includes(e)}));m(n,s)}if(r.idsToListen[n]=t,!t.length)return i.of([]);var u=t.map((function(t){var n=r.getPath(e)+"/"+t;return r.db.doc(n).snapshotChanges()}));return i.combineLatest(u).pipe(o.tap((function(t){return t.map((function(t){P(n,t,r.idKey,(function(t){return r.formatFromFirestore(t)}))}))})))})))},t.prototype.syncDoc=function(t,e){var r=this;void 0===e&&(e={loading:!1});var n=A(this.store,e),i=d(t,this.getPath(e)),a=i.id,s=i.path;return e.reset&&y(n),e.loading&&p(n,!0),this.db.doc(s).valueChanges().pipe(o.map((function(t){var e;if(t){var i=r.formatFromFirestore(Object.assign(((e={})[r.idKey]=a,e),t));return v(n,i,a),p(n,!1),i}p(n,!1)})))},t.prototype.syncActive=function(t,e){var r=this,n=A(this.store,e);return Array.isArray(t)?this.syncManyDocs(t,e).pipe(o.tap((function(e){return b(n,t)}))):this.syncDoc(t,e).pipe(o.tap((function(t){return t?b(n,t[r.idKey]):null})))},t.prototype.getRef=function(t,e){var r=this;void 0===e&&(e={});var n=this.getPath(e);if("string"==typeof t)return this.db.doc(n+"/"+t).ref;if(Array.isArray(t))return t.map((function(t){return r.db.doc(n+"/"+t).ref}));if("object"==typeof t){var i=this.getPath(t);return this.db.collection(i).ref}return this.db.collection(n,t).ref},t.prototype.getValue=function(t,e){return void 0===e&&(e={}),c(this,void 0,void 0,(function(){var r,n,i,o,a,s,u,c=this;return f(this,(function(f){switch(f.label){case 0:return r=this.getPath(e),"string"!=typeof t?[3,2]:[4,this.db.doc(r+"/"+t).ref.get()];case 1:return[2,(s=f.sent()).exists?this.formatFromFirestore(Object.assign(Object.assign({},s.data()),(u={},u[this.idKey]=s.id,u))):null];case 2:return Array.isArray(t)?[4,Promise.all(t.map((function(t){return c.db.doc(r+"/"+t).ref.get()})))]:[3,4];case 3:return n=f.sent(),[3,10];case 4:return"function"!=typeof t?[3,6]:(i=this.db.collection(r).ref,[4,t(i).get()]);case 5:return o=f.sent(),n=o.docs,[3,10];case 6:return"object"!=typeof t?[3,8]:(a=this.getPath(t),[4,this.db.collection(a).ref.get()]);case 7:return s=f.sent(),n=s.docs,[3,10];case 8:return[4,this.db.collection(r,t).ref.get()];case 9:s=f.sent(),n=s.docs,f.label=10;case 10:return[2,n.filter((function(t){return t.exists})).map((function(t){var e;return Object.assign(Object.assign({},t.data()),((e={})[c.idKey]=t.id,e))})).map((function(t){return c.formatFromFirestore(t)}))]}}))}))},t.prototype.valueChanges=function(t,e){var r=this;void 0===e&&(e={});var n,a=this.getPath(e);if("string"==typeof t)return this.db.doc(a+"/"+t).valueChanges().pipe(o.map((function(t){return r.formatFromFirestore(t)})));if(Array.isArray(t))n=t.length?i.combineLatest(t.map((function(t){return r.db.doc(a+"/"+t).valueChanges()}))):i.of([]);else if("function"==typeof t)n=this.db.collection(a,t).valueChanges();else if("object"==typeof t){var s=this.getPath(t);n=this.db.collection(s).valueChanges()}else n=this.db.collection(a,t).valueChanges();return n.pipe(o.map((function(t){return t.map((function(t){return r.formatFromFirestore(t)}))})))},t.prototype.batch=function(){return this.db.firestore.batch()},t.prototype.runTransaction=function(t){return this.db.firestore.runTransaction((function(e){return t(e)}))},t.prototype.upsert=function(t,e){return void 0===e&&(e={}),c(this,void 0,void 0,(function(){var r,n,i,o,a,s,u,d,p,y=this;return f(this,(function(b){switch(b.label){case 0:return r=function(t){return c(y,void 0,void 0,(function(){var r;return f(this,(function(n){switch(n.label){case 0:return r=this.getRef(t[this.idKey]),[4,S(e.write)?e.write.get(r):r.get()];case 1:return[2,n.sent().exists]}}))}))},v=t,Array.isArray(v)?[3,2]:[4,r(t)];case 1:return[2,b.sent()?this.update(t,e).then((function(e){return t[y.idKey]})):this.add(t,e)];case 2:n=[],i=[],b.label=3;case 3:b.trys.push([3,8,9,10]),o=h(t),a=o.next(),b.label=4;case 4:return a.done?[3,7]:(s=a.value,[4,r(s)]);case 5:b.sent()?i.push(s):n.push(s),b.label=6;case 6:return a=o.next(),[3,4];case 7:return[3,10];case 8:return u=b.sent(),d={error:u},[3,10];case 9:try{a&&!a.done&&(p=o.return)&&p.call(o)}finally{if(d)throw d.error}return[7];case 10:return[2,Promise.all([this.add(n,e),this.update(i,e).then((function(t){return i.map((function(t){return t[y.idKey]}))}))]).then((function(t){var e=l(t,2),r=e[0],n=e[1];return r.concat(n)}))]}var v}))}))},t.prototype.add=function(t,e){return void 0===e&&(e={}),c(this,void 0,void 0,(function(){var r,n,i,o,a,s,u,h=this;return f(this,(function(l){switch(l.label){case 0:return r=Array.isArray(t)?t:[t],n=e.write,i=void 0===n?this.batch():n,o=e.ctx,a=this.getPath(e),s=r.map((function(t){return c(h,void 0,void 0,(function(){var e,r,n,s;return f(this,(function(u){switch(u.label){case 0:return e=t[this.idKey]||this.db.createId(),r=this.formatToFirestore(Object.assign(Object.assign({},t),((s={})[this.idKey]=e,s))),n=this.db.doc(a+"/"+e).ref,i.set(n,r),this.onCreate?[4,this.onCreate(r,{write:i,ctx:o})]:[3,2];case 1:u.sent(),u.label=2;case 2:return[2,e]}}))}))})),[4,Promise.all(s)];case 1:return u=l.sent(),e.write?[3,3]:[4,i.commit()];case 2:l.sent(),l.label=3;case 3:return[2,Array.isArray(t)?u:u[0]]}}))}))},t.prototype.remove=function(t,e){return void 0===e&&(e={}),c(this,void 0,void 0,(function(){var r,n,i,o,a,s,u=this;return f(this,(function(h){switch(h.label){case 0:return r=e.write,n=void 0===r?this.batch():r,i=e.ctx,o=this.getPath(e),a=Array.isArray(t)?t:[t],s=a.map((function(t){return c(u,void 0,void 0,(function(){var e;return f(this,(function(r){switch(r.label){case 0:return e=this.db.doc(o+"/"+t).ref,n.delete(e),this.onDelete?[4,this.onDelete(t,{write:n,ctx:i})]:[3,2];case 1:r.sent(),r.label=2;case 2:return[2]}}))}))})),[4,Promise.all(s)];case 1:return h.sent(),e.write?[2]:[2,n.commit()]}}))}))},t.prototype.removeAll=function(t){return void 0===t&&(t={}),c(this,void 0,void 0,(function(){var e,r,n;return f(this,(function(i){switch(i.label){case 0:return e=this.getPath(t),[4,this.db.collection(e).ref.get()];case 1:return r=i.sent(),n=r.docs.map((function(t){return t.id})),[2,this.remove(n,t)]}}))}))},t.prototype.update=function(t,e,r){return void 0===r&&(r={}),c(this,void 0,void 0,(function(){var n,i,o,a,s,u,h,l,d,p,y,b=this;return f(this,(function(v){switch(v.label){case 0:if(n=[],s=function(t){return Array.isArray(t)&&t.every((function(t){return a(t)}))},(a=function(t){return"object"==typeof t&&t[b.idKey]})(t))n=[t[this.idKey]],o=function(){return t},r=e||{};else if(s(t))u=new Map(t.map((function(t){return[t[b.idKey],t]}))),n=Array.from(u.keys()),o=function(t){return u.get(t)},r=e||{};else if("function"==typeof e)n=Array.isArray(t)?t:[t],i=e;else{if("object"!=typeof e)throw new Error("Passed parameters match none of the function signatures.");n=Array.isArray(t)?t:[t],o=function(){return e}}return h=r.ctx,l=this.getPath(r),Array.isArray(n)&&n.length?i?[2,this.db.firestore.runTransaction((function(t){return c(b,void 0,void 0,(function(){var e,r=this;return f(this,(function(o){return e=n.map((function(e){return c(r,void 0,void 0,(function(){var r,n,o,a,s;return f(this,(function(u){switch(u.label){case 0:return r=this.db.doc(l+"/"+e).ref,[4,t.get(r)];case 1:return n=u.sent(),o=Object.freeze(Object.assign(Object.assign({},n.data()),((s={})[this.idKey]=e,s))),a=i(o,t),t.update(r,this.formatToFirestore(a)),this.onUpdate?[4,this.onUpdate(a,{write:t,ctx:h})]:[3,3];case 2:u.sent(),u.label=3;case 3:return[2,t]}}))}))})),[2,Promise.all(e)]}))}))}))]:[3,1]:[2];case 1:return d=r.write,p=void 0===d?this.batch():d,y=n.map((function(t){return c(b,void 0,void 0,(function(){var e,r;return f(this,(function(n){switch(n.label){case 0:if(e=Object.freeze(o(t)),!t)throw new Error("Document should have an unique id to be updated, but none was found in "+e);return r=this.db.doc(l+"/"+t).ref,p.update(r,this.formatToFirestore(e)),this.onUpdate?[4,this.onUpdate(e,{write:p,ctx:h})]:[3,2];case 1:n.sent(),n.label=2;case 2:return[2]}}))}))})),[4,Promise.all(y)];case 2:return v.sent(),r.write?[2]:[2,p.commit()]}}))}))},t}();var E={uid:null,emailVerified:void 0,profile:null,loading:!1},k=["github","google","microsoft","facebook","twitter","email","apple"];function U(t){return"string"==typeof t&&k.includes(t)}function K(t){switch(t){case"email":return new u.auth.EmailAuthProvider;case"facebook":return new u.auth.FacebookAuthProvider;case"github":return new u.auth.GithubAuthProvider;case"google":return new u.auth.GoogleAuthProvider;case"microsoft":return new u.auth.OAuthProvider("microsoft.com");case"twitter":return new u.auth.TwitterAuthProvider;case"apple":return new u.auth.OAuthProvider("apple")}}var T=function(){function t(t,n,i){this.store=t,this.collectionPath="users",this.db=n||e.inject(r.AngularFirestore),this.auth=i||e.inject(s.AngularFireAuth),this.collection=this.db.collection(this.path)}return t.prototype.selectProfile=function(t){return this.collection.doc(t.uid).valueChanges()},t.prototype.selectRoles=function(t){return i.of(null)},t.prototype.formatFromFirestore=function(t){return t},t.prototype.formatToFirestore=function(t){return t},t.prototype.createProfile=function(t,e){return{photoURL:t.photoURL,displayName:t.displayName}},Object.defineProperty(t.prototype,"user",{get:function(){return this.auth.currentUser},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"idKey",{get:function(){return this.constructor.idKey||"id"},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"path",{get:function(){return this.constructor.path||this.collectionPath},enumerable:!1,configurable:!0}),t.prototype.sync=function(){var t=this;return this.auth.authState.pipe(o.switchMap((function(e){return e?i.combineLatest([i.of(e),t.selectProfile(e),t.selectRoles(e)]):i.of([void 0,void 0,void 0])})),o.tap((function(e){var r=l(e,3),n=r[0],i=void 0===n?{}:n,o=r[1],a=r[2],s=t.formatFromFirestore(o),u=i.uid,c=i.emailVerified;t.store.update({uid:u,emailVerified:c,profile:s,roles:a})})),o.map((function(e){var r=l(e,3),n=r[0],i=r[1],o=r[2];return n?[n,t.formatFromFirestore(i),o]:null})))},t.prototype.delete=function(t){return void 0===t&&(t={}),c(this,void 0,void 0,(function(){var e,r,n,i,o;return f(this,(function(a){switch(a.label){case 0:return[4,this.user];case 1:if(!(e=a.sent()))throw new Error("No user connected");return r=t.write,n=void 0===r?this.db.firestore.batch():r,i=t.ctx,o=this.collection.doc(e.uid).ref,n.delete(o),this.onDelete?[4,this.onDelete({write:n,ctx:i})]:[3,3];case 2:a.sent(),a.label=3;case 3:return t.write?[3,5]:[4,n.commit()];case 4:a.sent(),a.label=5;case 5:return[2,e.delete()]}}))}))},t.prototype.update=function(t,e){return void 0===e&&(e={}),c(this,void 0,void 0,(function(){var r,n,i,o,a,s=this;return f(this,(function(u){switch(u.label){case 0:return[4,this.user];case 1:if(!(r=u.sent()).uid)throw new Error("No user connected.");return n=this.collection.doc(r.uid).ref,"function"!=typeof t?[3,2]:[2,this.db.firestore.runTransaction((function(r){return c(s,void 0,void 0,(function(){var i,o,a,s;return f(this,(function(u){switch(u.label){case 0:return[4,r.get(n)];case 1:return i=u.sent(),o=Object.freeze(Object.assign(Object.assign({},i.data()),((s={})[this.idKey]=i.id,s))),a=t(this.formatToFirestore(o),r),r.update(n,a),this.onUpdate?[4,this.onUpdate(a,{write:r,ctx:e.ctx})]:[3,3];case 2:u.sent(),u.label=3;case 3:return[2,r]}}))}))}))];case 2:return"object"!=typeof t?[3,5]:(i=e.write,o=void 0===i?this.db.firestore.batch():i,a=e.ctx,o.update(n,this.formatToFirestore(t)),this.onUpdate?[4,this.onUpdate(t,{write:o,ctx:a})]:[3,4]);case 3:u.sent(),u.label=4;case 4:if(!e.write)return[2,o.commit()];u.label=5;case 5:return[2]}}))}))},t.prototype.signup=function(t,e,r){return void 0===r&&(r={}),c(this,void 0,void 0,(function(){var n,i,o,a,s,u;return f(this,(function(c){switch(c.label){case 0:return[4,this.auth.createUserWithEmailAndPassword(t,e)];case 1:return n=c.sent(),i=r.write,o=void 0===i?this.db.firestore.batch():i,a=r.ctx,this.onSignup?[4,this.onSignup(n,{write:o,ctx:a})]:[3,3];case 2:c.sent(),c.label=3;case 3:return[4,this.createProfile(n.user,a)];case 4:return s=c.sent(),u=this.collection.doc(n.user.uid).ref,o.set(u,this.formatToFirestore(s)),this.onCreate?[4,this.onCreate(s,{write:o,ctx:a})]:[3,6];case 5:c.sent(),c.label=6;case 6:return r.write?[3,8]:[4,o.commit()];case 7:c.sent(),c.label=8;case 8:return[2,n]}}))}))},t.prototype.signin=function(t,e){return c(this,void 0,void 0,(function(){var r,n,i,o,a,s,u,c,h,l,d,p;return f(this,(function(f){switch(f.label){case 0:this.store.setLoading(!0),f.label=1;case 1:return f.trys.push([1,29,,30]),n=void 0,i=this.db.firestore.batch(),t?[3,3]:[4,this.auth.signInAnonymously()];case 2:return n=f.sent(),[3,11];case 3:return e&&"string"==typeof t&&"string"==typeof e?[4,this.auth.signInWithEmailAndPassword(t,e)]:[3,5];case 4:return n=f.sent(),[3,11];case 5:return"object"!=typeof t?[3,7]:[4,this.auth.signInWithPopup(t)];case 6:return n=f.sent(),[3,11];case 7:return U(t)?(o=K(t),[4,this.auth.signInWithPopup(o)]):[3,9];case 8:return n=f.sent(),[3,11];case 9:return[4,this.auth.signInWithCustomToken(t)];case 10:n=f.sent(),f.label=11;case 11:return n.additionalUserInfo.isNewUser?this.onSignup?[4,this.onSignup(n,{})]:[3,13]:[3,20];case 12:f.sent(),f.label=13;case 13:return[4,this.createProfile(n.user)];case 14:return r=f.sent(),this.store.update({profile:r}),a=this.collection.doc(n.user.uid).ref,i.set(a,this.formatToFirestore(r)),this.onCreate?"object"!=typeof e?[3,16]:[4,this.onCreate(r,{write:i,ctx:e.ctx})]:[3,18];case 15:return f.sent(),[3,18];case 16:return[4,this.onCreate(r,{write:i,ctx:{}})];case 17:f.sent(),f.label=18;case 18:return[4,i.commit()];case 19:return f.sent(),[3,26];case 20:return f.trys.push([20,25,,26]),[4,(s=this.collection.doc(n.user.uid)).get().toPromise()];case 21:return u=f.sent(),c=n.user,h=c.uid,l=c.emailVerified,u.exists?(r=this.formatFromFirestore(u.data()),[3,24]):[3,22];case 22:return[4,this.createProfile(n.user)];case 23:r=f.sent(),i.set(s.ref,this.formatToFirestore(r)),i.commit(),f.label=24;case 24:return this.store.update({profile:r,uid:h,emailVerified:l}),[3,26];case 25:return d=f.sent(),console.error(d),[3,26];case 26:return this.onSignin?[4,this.onSignin(n)]:[3,28];case 27:f.sent(),f.label=28;case 28:return this.store.setLoading(!1),[2,n];case 29:throw p=f.sent(),this.store.setLoading(!1),"auth/operation-not-allowed"===p.code&&console.warn("You tried to connect with a disabled auth provider. Enable it in Firebase console"),p;case 30:return[2]}}))}))},t.prototype.signOut=function(){return c(this,void 0,void 0,(function(){return f(this,(function(t){switch(t.label){case 0:return[4,this.auth.signOut()];case 1:return t.sent(),this.store.update(E),this.onSignout?[4,this.onSignout()]:[3,3];case 2:t.sent(),t.label=3;case 3:return[2]}}))}))},t}();var q=function(){function t(t){this.service=t;try{this.router=e.inject(a.Router)}catch(t){throw new Error("CollectionGuard requires RouterModule to be imported")}}return Object.defineProperty(t.prototype,"awaitSync",{get:function(){return this.constructor.awaitSync||!1},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"queryFn",{get:function(){return this.constructor.queryFn},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"redirect",{get:function(){return this.constructor.redirect},enumerable:!1,configurable:!0}),t.prototype.sync=function(t,e){var r=t.data.queryFn,n=void 0===r?this.queryFn:r;return this.service instanceof T?this.service.sync():this.service instanceof x?this.service.syncCollection(n):void 0},t.prototype.canActivate=function(t,e){var r=this,n=t.data,s=n.redirect,u=void 0===s?this.redirect:s,c=n.awaitSync,f=void 0===c?this.awaitSync:c;return new Promise((function(n,s){if(f){var c=new i.Subject;r.subscription=r.sync(t,e).pipe(o.takeUntil(c)).subscribe({next:function(t){if(t instanceof a.UrlTree)return n(t);switch(typeof t){case"string":return c.next(),c.complete(),n(r.router.parseUrl(t));case"boolean":return n(t);default:return n(!0)}},error:function(t){throw n(r.router.parseUrl(u||"")),new Error(t)}})}else r.subscription=r.sync(t,e).subscribe(),n(!0)}))},t.prototype.canDeactivate=function(){return this.subscription&&this.subscription.unsubscribe(),!0},t}(),L=function(){function t(t){this.store=t;try{this.db=e.inject(r.AngularFirestore)}catch(t){throw new Error("CollectionGroupService requires AngularFirestore.")}}return t.prototype.formatFromFirestore=function(t){return t},Object.defineProperty(t.prototype,"idKey",{get:function(){if(this.store)return this.store.idKey},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"resetOnUpdate",{get:function(){return this.constructor.resetOnUpdate||!1},enumerable:!1,configurable:!0}),t.prototype.syncCollection=function(t,e){var r,i=this;void 0===e&&(e={loading:!0}),"function"==typeof t?r=t:"object"==typeof t&&(e=t);var o=A(this.store,e);return e.reset&&y(o),e.loading&&p(o,!0),this.db.collectionGroup(this.collectionId,r).stateChanges().pipe(n.withTransaction((function(t){return w(o,t,i.idKey,i.resetOnUpdate,(function(t){return i.formatFromFirestore(t)}))})))},t.prototype.getValue=function(t){return c(this,void 0,void 0,(function(){var e=this;return f(this,(function(r){switch(r.label){case 0:return[4,this.db.collectionGroup(this.collectionId,t).get().toPromise()];case 1:return[2,r.sent().docs.map((function(t){var r=t.data();return e.formatFromFirestore(r)}))]}}))}))},t}();function R(t,e){return e.some((function(e){return e===t||"write"===e}))}function D(t,e){return e.some((function(e){return e===t||"write"===e}))}var I=["path","queryFn"];function Q(t){return Object.keys(t).filter((function(t){return!I.includes(t)}))}function G(t){return Q(t).length>0}function M(t,e){return"function"!=typeof t?t:t(e)}function N(t){return t.split("/").length%2==0}function W(t){return"object"==typeof t&&null!==t&&!!t.path}function V(t){var e=this;return z.call(this,t).pipe(o.tap((function(t){Array.isArray(t)?e.store.upsertMany(t):e.store.upsert(t[e.idKey],t)})))}function z(t){var e=this;return Y.call(this,t).pipe(o.map((function(t){return Array.isArray(t)?t.map((function(t){return e.formatFromFirestore(t)})):e.formatFromFirestore(t)})))}function Y(t){var e=this;if("string"==typeof t)return N(t)?this.db.doc(t).valueChanges():this.db.collection(t).valueChanges({idField:this.idKey});if(Array.isArray(t))return t.length?i.combineLatest(t.map((function(t){return V.call(e,t)}))):i.of(t);if(!W(t))return i.of(t);var r=function(r,n){if(!n)return i.throwError("Nothing found at path : "+r.path);if(!G(r))return i.of(n);var a=Q(t).map((function(t){return function(t,r){return t?"function"!=typeof t?i.of(t):Y.call(e,t(r)):i.throwError("Query failed")}(r[t],n).pipe(o.tap((function(e){return n[t]=e})))}));return a.length?i.combineLatest(a).pipe(o.map((function(){return n}))):i.of(n)},n=t.path,a=t.queryFn;if(N(n)){var s=d({path:n}).id;return this.db.doc(n).valueChanges().pipe(o.switchMap((function(e){return r(t,e)})),o.map((function(t){return Object.assign({id:s},t)})))}return this.db.collection(n,a).valueChanges({idField:this.idKey}).pipe(o.switchMap((function(e){var n=e.map((function(e){return r(t,e)}));return n.length?i.combineLatest(n):i.of([])})))}t.CollectionConfig=function(t){return void 0===t&&(t={}),function(e){Object.keys(t).forEach((function(r){return e[r]=t[r]}))}},t.CollectionGroupService=L,t.CollectionGuard=q,t.CollectionGuardConfig=function(t){return function(e){Object.keys(t).forEach((function(r){return e[r]=t[r]}))}},t.CollectionService=x,t.FireAuthService=T,t.authProviders=k,t.awaitQuery=Y,t.awaitSyncQuery=V,t.callFunction=function(t,e,r){return c(this,void 0,void 0,(function(){return f(this,(function(n){return[2,t.httpsCallable(e)(r).toPromise()]}))}))},t.canRead=D,t.canWrite=R,t.collection=function(t,e){return{path:t,queryFn:e}},t.doc=function(t){return{path:t}},t.getAuthProvider=K,t.getCustomClaims=function(t,e){return c(this,void 0,void 0,(function(){var r,n;return f(this,(function(i){switch(i.label){case 0:return[4,t.getIdTokenResult()];case 1:return r=i.sent().claims,e?(n=Array.isArray(e)?e:[e],[2,Object.keys(r).filter((function(t){return n.includes(t)})).reduce((function(t,e){return t[e]=r[e],t}),{})]):[2,r]}}))}))},t.getIdAndPath=d,t.getPathParams=j,t.getSubQuery=M,t.getSubQueryKeys=Q,t.hasRole=function(t,e){switch(t){case"write":return e.includes("write");case"create":return R("create",e);case"delete":return R("delete",e);case"update":return R("update",e);case"read":return e.includes("read");case"get":return D("get",e);case"list":return D("list",e);default:return!1}},t.hasSubQueries=G,t.initialAuthState=E,t.isDocPath=N,t.isFireAuthProvider=U,t.isQuery=W,t.isTransaction=S,t.pathWithParams=F,t.queryChanges=z,t.queryKeys=I,t.redirectIfEmpty=function(t){return o.map((function(e){return 0!==e.length||t}))},t.removeStoreEntity=m,t.resetStore=y,t.setActive=b,t.setLoading=p,t.shouldCancel=function(t){var e=t.validate,r=t.cancel;return i.race([e.pipe(o.map((function(t){return!1}))),r.pipe(o.map((function(t){return!0})))])},t.syncQuery=function t(e){var r=this;if("string"==typeof e)return N(e)?this.syncDoc({path:e}):this.syncCollection(e);if(Array.isArray(e))return i.combineLatest(e.map((function(e){return t.call(r,e)})));if(!W(e))throw new Error("Query should be either a path, a Query object or an array of Queries");var a=function(t,e){var s,u=e.parentId,c=e.key;if(!W(t)){var f=r.store.update(u,((s={})[c]=t,s));return i.of(f)}if(Array.isArray(t)){var l=t.map((function(i){if(W(t)){var s=d({path:t.path});return r.db.doc(t.path).valueChanges().pipe(o.tap((function(t){r.store.update(u,(function(e){var r;return(r={})[c]=n.arrayAdd(e[c],s,t),r}))})))}return a(i,e)}));return i.combineLatest(l)}if("object"!=typeof t)throw new Error("Query should be either a path, a Query object or an array of Queries");return N(t.path)?r.db.doc(t.path).valueChanges().pipe(o.tap((function(t){var e;return r.store.update(u,((e={})[c]=t,e))}))):r.db.collection(t.path,t.queryFn).stateChanges().pipe(n.withTransaction((function(t){return function(t,e){var i,o,a="id",s=e.parentId,u=e.key,c=function(t){var e=t.payload.doc.id,i=t.payload.doc.data();switch(t.type){case"added":r.store.update(s,(function(t){var r;return(r={})[u]=n.arrayUpsert(t[u]||[],e,i,a),r}));break;case"removed":r.store.update(s,(function(t){var r;return(r={})[u]=n.arrayRemove(t[u],e,a),r}));break;case"modified":r.store.update(s,(function(t){var r;return(r={})[u]=n.arrayUpdate(t[u],e,i,a),r}))}};try{for(var f=h(t),l=f.next();!l.done;l=f.next())c(l.value)}catch(t){i={error:t}}finally{try{l&&!l.done&&(o=f.return)&&o.call(f)}finally{if(i)throw i.error}}}(t,e)})))},s=function(t,e){var n=Object.keys(t).filter((function(t){return"path"!==t&&"queryFn"!==t})).map((function(n){var i=M(t[n],e),o={key:n,parentId:e[r.idKey]};return a(i,o)}));return i.combineLatest(n)},u=e.path,c=e.queryFn;if(N(u)){var f,l=d({path:u}).id;return this.db.doc(u).valueChanges().pipe(o.tap((function(t){r.store.upsert(l,r.formatFromFirestore(Object.assign({id:l},t))),f||(f=s(e,t).subscribe())})),o.finalize((function(){return f.unsubscribe()})))}var p={};return this.db.collection(u,c).stateChanges().pipe(n.withTransaction((function(t){return function(t,e,n){var i,o,a;try{for(var u=h(t),c=u.next();!c.done;c=u.next()){var f=c.value,l=f.payload.doc.id,d=f.payload.doc.data();switch(f.type){case"added":var p=r.formatFromFirestore(Object.assign(((a={})[r.idKey]=l,a),d));r.store.upsert(l,p),n[l]=s(e,p).subscribe();break;case"removed":r.store.remove(l),n[l].unsubscribe(),delete n[l];break;case"modified":r.store.update(l,d)}}}catch(t){i={error:t}}finally{try{c&&!c.done&&(o=u.return)&&o.call(u)}finally{if(i)throw i.error}}}(t,e,p)})),o.finalize((function(){return Object.keys(p).forEach((function(t){p[t].unsubscribe(),delete p[t]}))})))},t.syncStoreFromDocAction=w,t.syncStoreFromDocActionSnapshot=P,t.syncWithRouter=function(t){var e=this;if(!this.store.resettable)throw new Error("Store "+this.store.storeName+" is required to be resettable for syncWithRouter to work.");var r=j(this.path);return t.selectParams().pipe(o.distinctUntilChanged((function(t,n){var i=!!r.find((function(e){return t[e]!==n[e]}));return i&&e.store.reset(),!i})),o.filter((function(t){return r.every((function(e){return!!t[e]}))})),o.switchMap((function(t){return e.syncCollection({params:t})})),o.share())},t.updateStoreEntity=g,t.upsertStoreEntity=v,t.waitForCancel=function(t){var e=t.startWith,r=t.endWith,n=t.shouldValidate,a=t.shouldCancel;return c(this,void 0,void 0,(function(){var t;return f(this,(function(s){switch(s.label){case 0:return e(),[4,i.race([n.pipe(o.map((function(t){return!1}))),a.pipe(o.map((function(t){return!0})))]).toPromise()];case 1:return t=s.sent(),r(t),[2]}}))}))},Object.defineProperty(t,"__esModule",{value:!0})}));
2//# sourceMappingURL=akita-ng-fire.umd.min.js.map
\No newline at end of file