Comparing version 3.5.2 to 3.6.0
@@ -691,2 +691,21 @@ (function() { | ||
/** | ||
* Insert a phantom into the cache, if no real entity with the same ID exists yet. | ||
* Phantoms bypass the raw entity cache. | ||
* @param {Object} entity | ||
*/ | ||
CacheService.prototype.phantom = function CacheService$phantom( entity ) { | ||
var self = this; | ||
if( self.has( entity.id ) ) { | ||
return; | ||
} | ||
return self.__onEntityReceived( null, entity ); | ||
}; | ||
/** | ||
* Drop existing cache and force reload. | ||
* @returns {Promise.<Array.<configuration.model>>|IPromise.<Array>|IPromise.<void>|Q.Promise.<Array.<configuration.model>>|angular.IPromise.<TResult>} | ||
*/ | ||
CacheService.prototype.sync = function CacheService$sync() { | ||
@@ -701,2 +720,13 @@ var self = this; | ||
/** | ||
* Check if the given element with the given ID is in the cache. | ||
* @param {String} id | ||
* @return {Boolean} true if the element is in the cache; false otherwise. | ||
*/ | ||
CacheService.prototype.has = function CacheService$has( id ) { | ||
var self = this; | ||
return self.entityCache.__lookup.hasOwnProperty( id ); | ||
}; | ||
/** | ||
* Read a single entity from the cache, or load it from the server if required. | ||
@@ -703,0 +733,0 @@ * The entity will be placed into the cache. |
@@ -382,2 +382,21 @@ (function() { | ||
/** | ||
* Insert a phantom into the cache, if no real entity with the same ID exists yet. | ||
* Phantoms bypass the raw entity cache. | ||
* @param {Object} entity | ||
*/ | ||
CacheService.prototype.phantom = function CacheService$phantom( entity ) { | ||
var self = this; | ||
if( self.has( entity.id ) ) { | ||
return; | ||
} | ||
return self.__onEntityReceived( null, entity ); | ||
}; | ||
/** | ||
* Drop existing cache and force reload. | ||
* @returns {Promise.<Array.<configuration.model>>|IPromise.<Array>|IPromise.<void>|Q.Promise.<Array.<configuration.model>>|angular.IPromise.<TResult>} | ||
*/ | ||
CacheService.prototype.sync = function CacheService$sync() { | ||
@@ -392,2 +411,13 @@ var self = this; | ||
/** | ||
* Check if the given element with the given ID is in the cache. | ||
* @param {String} id | ||
* @return {Boolean} true if the element is in the cache; false otherwise. | ||
*/ | ||
CacheService.prototype.has = function CacheService$has( id ) { | ||
var self = this; | ||
return self.entityCache.__lookup.hasOwnProperty( id ); | ||
}; | ||
/** | ||
* Read a single entity from the cache, or load it from the server if required. | ||
@@ -394,0 +424,0 @@ * The entity will be placed into the cache. |
@@ -1,2 +0,2 @@ | ||
!function(){"use strict";angular.module("absync",[])}(),function(){"use strict";function e(e,n,r){return new t(e,n,r)}function t(e,t,n){var r=this;r.__injector=e,r.__provide=t,r.__absyncCache=n,r.__ioSocket=null,r.__registerLater=[],r.__listeners=[],r.__collections={},r.__entities={},r.debug=void 0}e.$inject=["$injector","$provide","absyncCache"],angular.module("absync").provider("absync",e),t.prototype.configure=function(e){var t=this;if("undefined"!=typeof e.socket){var n=e.socket,r="undefined"!=typeof io&&io.Socket&&n instanceof io.Socket;if("function"==typeof n)t.__ioSocket=new n;else{if(!r)throw new Error("configure() expects input to be a function or a socket.io Socket instance.");t.__ioSocket=n}t.__registerLater.length&&(angular.forEach(t.__registerLater,t.__registerListener.bind(t)),t.__registerLater=[])}"undefined"!=typeof e.debug&&(t.debug=e.debug||!1),t.debug&&(angular.forEach(t.__collections,function(e){e.configuration.debug=!0}),angular.forEach(t.__entities,function(e){e.configuration.debug=!0}))},t.prototype.disconnect=function(e){var t=this;e=e||!1,angular.forEach(t.__listeners,function(e){e.unregister(),delete e.unregister,t.__registerLater.push(e)}),t.__listeners=[],e&&(t.__ioSocket.disconnect(),t.__ioSocket=null)},t.prototype.__registerListener=function(e){var t=this;t.__listeners.push(e),e.unregister=t.__handleEntityEvent(e.eventName,e.callback)},t.prototype.collection=function(e,t){var n=this;if(n.__collections[e])throw new Error("A collection with the name '"+e+"' was already requested. Names for collections must be unique.");if(n.__entities[e])throw new Error("An entity with the name '"+e+"' was already requested. Names for collections must be unique and can't be shared with entities.");t.debug="undefined"==typeof t.debug?n.debug:t.debug,n.__collections[e]={constructor:n.__absyncCache(e,t),configuration:t},n.__provide.service(e,n.__collections[e].constructor)},t.prototype.entity=function(e,t){var n=this;if(n.__entities[e])throw new Error("An entity with the name '"+e+"' was already requested. Names for entities must be unique.");if(n.__collections[e])throw new Error("A collection with the name '"+e+"' was already requested. Names for entities must be unique and can't be shared with collections.");t.debug="undefined"==typeof t.debug?n.debug:t.debug,n.__entities[e]={constructor:n.__absyncCache(e,t),configuration:t},n.__provide.service(e,n.__entities[e].constructor)},t.prototype.on=function(e,t){var n=this;return n.__ioSocket?n.__registerListener({eventName:e,callback:t}):n.__registerLater.length>8192?null:(n.__registerLater.push({eventName:e,callback:t}),null)},t.prototype.__handleEntityEvent=function(e,t){var n=this;return n.__ioSocket.on(e,t),function(){n.__ioSocket.removeListener(e,t)}},t.prototype.emit=function(e,t,n){var r=this;if(!r.__ioSocket)throw new Error("socket.io is not initialized.");r.__ioSocket.emit(e,t,function(){n&&n.apply(r.__ioSocket,arguments)})},t.prototype.$get=function(){return this}}(),function(){"use strict";function e(e,n){function r(r,o,i,a,c,s,l,u){var h=this,d=n.injector||o,_=d.has(n.model);if(!_)throw new Error("Unable to construct the '"+e+"' service, because the referenced model '"+n.model+"' is not available for injection.");var f="string"==typeof n.model?d.get(n.model):n.model,y=f.serialize||n.serialize||t,p=f.deserialize||n.deserialize||t;h.name=e,h.configuration=n,h.entityCache=n.collectionName?[]:{},h.entityCache.__lookup={},h.__entityCacheRaw=null,h.enableRequestCache=!0,h.__requestCache={},h.allowBrowserCache=(angular.merge||angular.extend)({},{sync:!0,request:!0},n.allowBrowserCache),h.__uncached=u,h.httpInterface=r,h.logInterface=n.debug?i:l,h.scope=c,h.q=a,h.logPrefix="absync:"+e.toLocaleUpperCase()+" ",h.forceEarlyCacheUpdate=!1,h.throwFailures=!0,h.serializer=y,h.deserializer=p,h.filter=n.filter,s.on(n.entityName,h.__onEntityOnWebsocket.bind(h)),n.collectionName&&s.on(n.collectionName,h.__onCollectionOnWebsocket.bind(h)),c.$on(n.entityName,h.__onEntityReceived.bind(h)),n.collectionName&&c.$on(n.collectionName,h.__onCollectionReceived.bind(h)),h.logInterface.info(h.logPrefix+"service was instantiated.")}function o(e,t){var r=this;if((e||r.forceEarlyCacheUpdate)&&t.data[n.entityName]){var o=t.data[n.entityName];if(r.forceEarlyCacheUpdate){var i=r.deserializer(o);if(r.__updateCacheWithEntity(i),e)return i}if(e)return o}}function i(e){var t=this;if(t.logInterface.error(t.logPrefix+"Unable to store entity on the server.",e),t.logInterface.error(e),t.scope.$emit("absyncError",e),t.throwFailures)throw e}return r.$inject=["$http","$injector","$log","$q","$rootScope","absync","absyncNoopLog","absyncUncachedFilter"],r.prototype.__onEntityOnWebsocket=function(e){var t=this;t.scope.$broadcast(n.entityName,e[n.entityName])},r.prototype.__onCollectionOnWebsocket=function(e){var t=this;t.scope.$broadcast(n.collectionName,e[n.collectionName])},r.prototype.__onDataAvailable=function(e){function t(e){var t=r.deserializer(e);r.entityCache.push(t),r.entityCache.__lookup&&(r.entityCache.__lookup[t.id]=r.entityCache.length-1)}var r=this;if(Array.isArray(r.entityCache))angular.forEach(e[n.collectionName],t),r.scope.$broadcast("collectionNew",{service:r,cache:r.entityCache});else{var o=r.deserializer(e[n.entityName]);r.__updateCacheWithEntity(o)}return r.entityCache},r.prototype.__onEntityReceived=function(e,t){var r=this,o=t;return r.__entityCacheRaw&&r.__entityCacheRaw[n.collectionName||n.entityName]?1===Object.keys(o).length&&o.hasOwnProperty("id")?(r.logInterface.info(r.logPrefix+"Entity was deleted from the server. Updating cache…"),r.__cacheMaintain(r.__entityCacheRaw[n.collectionName||n.entityName],o,"delete",!1),r.__removeEntityFromCache(o.id)):(r.logInterface.debug(r.logPrefix+"Entity was updated on the server. Updating cache…"),r.__cacheMaintain(r.__entityCacheRaw[n.collectionName||n.entityName],o,"update",!1),r.__updateCacheWithEntity(r.deserializer(o))):r.ensureLoaded().then(function(){return r.read(o.id)})},r.prototype.__onCollectionReceived=function(e,t){function n(e){var t=r.deserializer(e);r.__updateCacheWithEntity(t)}var r=this,o=t;r.entityCache.length=0,angular.forEach(o,n)},r.prototype.ensureLoaded=function(e){function t(e){if(!e.data[n.collectionName])throw new Error("The response from the server was not in the expected format. It should have a member named '"+n.collectionName+"'.");return a.__entityCacheRaw=e.data,a.entityCache.splice(0,a.entityCache.length),a.__onDataAvailable(e.data)}function r(e){if(a.logInterface.error(a.logPrefix+"Unable to retrieve the collection from the server.",e),a.__entityCacheRaw=null,a.scope.$emit("absyncError",e),a.throwFailures)throw e}function o(e){if(!e.data[n.entityName])throw new Error("The response from the server was not in the expected format. It should have a member named '"+n.entityName+"'.");a.__entityCacheRaw=e.data,a.__onDataAvailable(e.data)}function i(e){if(a.logInterface.error(a.logPrefix+"Unable to retrieve the entity from the server.",e),a.__entityCacheRaw=null,a.scope.$emit("absyncError",e),a.throwFailures)throw e}var a=this;if(e=e===!0,e&&delete a.__loading,a.__loading)return a.__loading;if(null===a.__entityCacheRaw||e){if(n.collectionName&&n.collectionUri)a.logInterface.info(a.logPrefix+"Retrieving '"+n.collectionName+"' collection…"),a.__loading=a.httpInterface.get(a.allowBrowserCache.sync?n.collectionUri:a.__uncached(n.collectionUri)).then(t,r);else{if(!n.entityName||!n.entityUri)return a.q.when([]);a.__loading=a.httpInterface.get(a.allowBrowserCache.sync?n.entityUri:a.__uncached(n.entityUri)).then(o,i)}return a.__loading}return a.q.when(a.entityCache)},r.prototype.seed=function(e){var t=this;return t.__entityCacheRaw=e,t.__onDataAvailable(t.__entityCacheRaw)},r.prototype.sync=function(){var e=this;return e.__entityCacheRaw=null,e.ensureLoaded(!0)},r.prototype.read=function(e,t){function r(e){if(!e.data[n.entityName])throw new Error("The response from the server was not in the expected format. It should have a member named '"+n.entityName+"'.");var t=e.data[n.entityName];i.__cacheMaintain(i.__entityCacheRaw[n.collectionName||n.entityName],t,"update",!1);var r=i.deserializer(t);return i.__updateCacheWithEntity(r),r}function o(t){if(i.logInterface.error(i.logPrefix+"Unable to retrieve entity with ID '"+e+"' from the server.",t),i.scope.$emit("absyncError",t),i.throwFailures)throw t}var i=this;if(t=t===!0,i.logInterface.debug(i.logPrefix+"Requesting entity '"+e+"' (forceReload:"+t+")…"),!t){var a=0;i.entityCache.__lookup&&(a=i.entityCache.__lookup.hasOwnProperty(e)?i.entityCache.__lookup[e]:i.entityCache.length);for(var c=i.entityCache[a];a<i.entityCache.length;++a,c=i.entityCache[a])if(c.id===e)return i.logInterface.debug(i.logPrefix+"Requested entity '"+e+"' is served from cache."),i.q.when(c)}return i.logInterface.debug(i.logPrefix+"Requested entity '"+e+"' is fetched from backend."),i.__requestEntity(e).then(r,o)},r.prototype.__requestEntity=function(e){function t(e,t){return delete r.__requestCache[e],t}var r=this;if(r.enableRequestCache&&r.__requestCache&&r.__requestCache[e])return r.logInterface.debug(r.logPrefix+"Entity request '"+e+"' served from request cache."),r.__requestCache[e];r.__entityCacheRaw=r.__entityCacheRaw||{},r.__entityCacheRaw[n.collectionName]=r.__entityCacheRaw[n.collectionName]||[];var o=n.entityUri+(e?"/"+e:""),i=r.httpInterface.get(r.allowBrowserCache.request?o:r.__uncached(o)).then(t.bind(r,e));return r.enableRequestCache&&r.__requestCache&&(r.__requestCache[e]=i),i},r.prototype.update=function(e,t){var r=this;t=t||!1;var a=r.reduceComplex(e),c=r.serializer(a),s={};return s[n.entityName]=c,"undefined"!=typeof e.id?r.httpInterface.put(n.entityUri+"/"+e.id,s).then(o.bind(r,t),i.bind(r)):r.httpInterface.post(n.collectionUri,s).then(o.bind(r,t),i.bind(r))},r.prototype.patch=function(e,t){var r=this;t=t||!1;var a=r.reduceComplex(e),c=r.serializer(a),s={};if(s[n.entityName]=c,"undefined"!=typeof e.id)return r.httpInterface.patch(n.entityUri+"/"+e.id,s).then(o.bind(r,t),i.bind(r));throw new Error("Attempted to patch an entity that was never stored on the server.")},r.prototype.create=r.prototype.update,r.prototype["delete"]=function(e){function t(t){return o.__cacheMaintain(o.__entityCacheRaw[n.collectionName||n.entityName],e,"delete",!1),o.__removeEntityFromCache(i)}function r(e){if(o.logInterface.error(e.data),o.scope.$emit("absyncError",e),o.throwFailures)throw e}var o=this,i=e.id;return o.httpInterface["delete"](n.entityUri+"/"+i).then(t)["catch"](r)},r.prototype.__updateCacheWithEntity=function(e){var t=this;return t.logInterface.info(t.logPrefix+"Updating entity '"+(e.id||t.name)+"' in cache…",e),t.__cacheMaintain(t.entityCache,e,"update",!0)},r.prototype.__cacheMaintain=function(e,t,n,r){var o=this,i=0,a=e[i];if(o.filter&&!o.filter(t))return void o.logInterface.info(o.logPrefix+"Entity '"+(t.id||o.name)+"' was filtered.",t);switch(e.__lookup&&(i=e.__lookup.hasOwnProperty(t.id)?e.__lookup[t.id]:e.length,a=e[i]),n){case"update":if(!Array.isArray(e))return r&&o.scope.$broadcast("beforeEntityUpdated",{service:o,cache:e,entity:e,updated:t}),"function"==typeof e.copyFrom?e.copyFrom(t):angular.extend(e,t),void o.scope.$broadcast("entityUpdated",{service:o,cache:e,entity:e});var c=!1;for(angular.noop;i<e.length;++i,a=e[i])if(a.id===t.id){r&&o.scope.$broadcast("beforeEntityUpdated",{service:o,cache:e,entity:e[i],updated:t});var s=e[i];"function"==typeof s.copyFrom?s.copyFrom(t):angular.extend(s,t),c=!0,r&&o.scope.$broadcast("entityUpdated",{service:o,cache:e,entity:e[i]});break}c||(r&&o.scope.$broadcast("beforeEntityNew",{service:o,cache:e,entity:t}),e.push(t),e.__lookup&&(e.__lookup[t.id]=e.length-1),r&&o.scope.$broadcast("entityNew",{service:o,cache:e,entity:t}));break;case"delete":for(angular.noop;i<e.length;++i,a=e[i])if(a.id===t.id){if(r&&o.scope.$broadcast("beforeEntityRemoved",{service:o,cache:e,entity:a}),e.splice(i,1),e.__lookup){delete e.__lookup[t.id];for(var l in e.__lookup)i<=e.__lookup[l]&&--e.__lookup[l]}r&&o.scope.$broadcast("entityRemoved",{service:o,cache:e,entity:a});break}}},r.prototype.__removeEntityFromCache=function(e){var t=this;return t.__cacheMaintain(t.entityCache,{id:e},"delete",!0)},r.prototype.lookupTableById=function(){for(var e=this,t=[],n=0;n<e.entityCache.length;++n)t[e.entityCache[n].id]=e.entityCache[n];return t},r.prototype.reduceComplex=function(e,t){var n=this,r=t?[]:{};for(var o in e)e.hasOwnProperty(o)&&(Array.isArray(e[o])?r[o]=n.reduceComplex(e[o],!0):e[o]&&e[o].id?r[o]=e[o].id:r[o]=e[o]);return r},r.prototype.populateComplex=function(e,t,n,r){function o(o,i){function s(n){return e[t][i]=n,r.crossLink&&a(n,e),e}if("string"!=typeof e[t][i]){if(!r.force||"object"!=typeof e[t][i]||"string"!=typeof e[t][i].id){if(c.throwFailures)throw new Error("The referenced entity did not have an 'id' property that would be expected.");return c.q.when(!1)}e[t][i]=e[t][i].id}return n.read(e[t][i]).then(s)}function i(n){return e[t]=n,r.crossLink&&a(n,e),e}function a(e,t){if(Array.isArray(e[r.crossLinkProperty])){var n=e[r.crossLinkProperty].indexOf(t.id);if(n>-1)return void(e[r.crossLinkProperty][n]=t);for(var o=0,i=e[r.crossLinkProperty][0];o<e[r.crossLinkProperty].length;++o,i=e[r.crossLinkProperty][o]){if(i===t)return;if(i.id===t.id)return void(e[r.crossLinkProperty][o]=t)}return void e[r.crossLinkProperty].push(t)}e[r.crossLinkProperty]=t}var c=this;if(r=r||{},"boolean"==typeof r&&(c.logInterface.warn("Argument 'force' is deprecated. Provide an options hash instead."),r={force:r}),r.force=r.force||!1,r.crossLink=r.crossLink||!1,r.crossLinkProperty=r.crossLinkProperty||"",r.crossLink&&!r.crossLinkProperty&&(c.logInterface.warn("Option 'crossLink' given without 'crossLinkProperty'. Cross-linking will be disabled."),r.crossLink=!1),Array.isArray(e[t])){var s=e[t].map(o);return c.q.all(s)}if("string"!=typeof e[t]){if(!r.force||"object"!=typeof e[t]||"string"!=typeof e[t].id){if(c.throwFailures)throw new Error("The referenced entity did not have an 'id' property that would be expected.");return c.q.when(!1)}e[t]=e[t].id}return n.read(e[t]).then(i)},r.prototype.reset=function(){var e=this;e.entityCache=e.configuration.collectionName?[]:{},e.entityCache.__lookup=e.entityCache.__lookup||{},e.__entityCacheRaw=null,e.__requestCache={}},r}function t(e){return e}angular.module("absync").constant("absyncCache",e)}(),function(){"use strict";function e(){return t}function t(e,t,n,r,o,i,a,c,s,l){this.model=e,this.collectionUri=t,this.entityUri=n;var u=e.prototype.constructor.name.toLowerCase();this.collectionName=r||u+"s",this.entityName=o||u,this.deserialize=i||void 0,this.serialize=a||void 0,this.injector=c||void 0,this.debug=s||!1,this.allowBrowserCache=angular.merge({},{sync:!0,request:!0},l)}angular.module("absync").service("AbsyncServiceConfiguration",e)}(),function(){"use strict";angular.module("absync").constant("absyncNoopLog",{debug:angular.noop,info:angular.noop,warn:angular.noop,error:angular.noop})}(),function(){"use strict";function e(){function e(e){if(!e)return e;var t=-1<e.indexOf("?")?"&":"?",n=(new Date).getTime();return e+t+"t="+n}return e}angular.module("absync").filter("absyncUncached",e)}(); | ||
!function(){"use strict";angular.module("absync",[])}(),function(){"use strict";function e(e,n,r){return new t(e,n,r)}function t(e,t,n){var r=this;r.__injector=e,r.__provide=t,r.__absyncCache=n,r.__ioSocket=null,r.__registerLater=[],r.__listeners=[],r.__collections={},r.__entities={},r.debug=void 0}e.$inject=["$injector","$provide","absyncCache"],angular.module("absync").provider("absync",e),t.prototype.configure=function(e){var t=this;if("undefined"!=typeof e.socket){var n=e.socket,r="undefined"!=typeof io&&io.Socket&&n instanceof io.Socket;if("function"==typeof n)t.__ioSocket=new n;else{if(!r)throw new Error("configure() expects input to be a function or a socket.io Socket instance.");t.__ioSocket=n}t.__registerLater.length&&(angular.forEach(t.__registerLater,t.__registerListener.bind(t)),t.__registerLater=[])}"undefined"!=typeof e.debug&&(t.debug=e.debug||!1),t.debug&&(angular.forEach(t.__collections,function(e){e.configuration.debug=!0}),angular.forEach(t.__entities,function(e){e.configuration.debug=!0}))},t.prototype.disconnect=function(e){var t=this;e=e||!1,angular.forEach(t.__listeners,function(e){e.unregister(),delete e.unregister,t.__registerLater.push(e)}),t.__listeners=[],e&&(t.__ioSocket.disconnect(),t.__ioSocket=null)},t.prototype.__registerListener=function(e){var t=this;t.__listeners.push(e),e.unregister=t.__handleEntityEvent(e.eventName,e.callback)},t.prototype.collection=function(e,t){var n=this;if(n.__collections[e])throw new Error("A collection with the name '"+e+"' was already requested. Names for collections must be unique.");if(n.__entities[e])throw new Error("An entity with the name '"+e+"' was already requested. Names for collections must be unique and can't be shared with entities.");t.debug="undefined"==typeof t.debug?n.debug:t.debug,n.__collections[e]={constructor:n.__absyncCache(e,t),configuration:t},n.__provide.service(e,n.__collections[e].constructor)},t.prototype.entity=function(e,t){var n=this;if(n.__entities[e])throw new Error("An entity with the name '"+e+"' was already requested. Names for entities must be unique.");if(n.__collections[e])throw new Error("A collection with the name '"+e+"' was already requested. Names for entities must be unique and can't be shared with collections.");t.debug="undefined"==typeof t.debug?n.debug:t.debug,n.__entities[e]={constructor:n.__absyncCache(e,t),configuration:t},n.__provide.service(e,n.__entities[e].constructor)},t.prototype.on=function(e,t){var n=this;return n.__ioSocket?n.__registerListener({eventName:e,callback:t}):n.__registerLater.length>8192?null:(n.__registerLater.push({eventName:e,callback:t}),null)},t.prototype.__handleEntityEvent=function(e,t){var n=this;return n.__ioSocket.on(e,t),function(){n.__ioSocket.removeListener(e,t)}},t.prototype.emit=function(e,t,n){var r=this;if(!r.__ioSocket)throw new Error("socket.io is not initialized.");r.__ioSocket.emit(e,t,function(){n&&n.apply(r.__ioSocket,arguments)})},t.prototype.$get=function(){return this}}(),function(){"use strict";function e(e,n){function r(r,o,i,a,c,s,l,u){var h=this,d=n.injector||o,_=d.has(n.model);if(!_)throw new Error("Unable to construct the '"+e+"' service, because the referenced model '"+n.model+"' is not available for injection.");var f="string"==typeof n.model?d.get(n.model):n.model,y=f.serialize||n.serialize||t,p=f.deserialize||n.deserialize||t;h.name=e,h.configuration=n,h.entityCache=n.collectionName?[]:{},h.entityCache.__lookup={},h.__entityCacheRaw=null,h.enableRequestCache=!0,h.__requestCache={},h.allowBrowserCache=(angular.merge||angular.extend)({},{sync:!0,request:!0},n.allowBrowserCache),h.__uncached=u,h.httpInterface=r,h.logInterface=n.debug?i:l,h.scope=c,h.q=a,h.logPrefix="absync:"+e.toLocaleUpperCase()+" ",h.forceEarlyCacheUpdate=!1,h.throwFailures=!0,h.serializer=y,h.deserializer=p,h.filter=n.filter,s.on(n.entityName,h.__onEntityOnWebsocket.bind(h)),n.collectionName&&s.on(n.collectionName,h.__onCollectionOnWebsocket.bind(h)),c.$on(n.entityName,h.__onEntityReceived.bind(h)),n.collectionName&&c.$on(n.collectionName,h.__onCollectionReceived.bind(h)),h.logInterface.info(h.logPrefix+"service was instantiated.")}function o(e,t){var r=this;if((e||r.forceEarlyCacheUpdate)&&t.data[n.entityName]){var o=t.data[n.entityName];if(r.forceEarlyCacheUpdate){var i=r.deserializer(o);if(r.__updateCacheWithEntity(i),e)return i}if(e)return o}}function i(e){var t=this;if(t.logInterface.error(t.logPrefix+"Unable to store entity on the server.",e),t.logInterface.error(e),t.scope.$emit("absyncError",e),t.throwFailures)throw e}return r.$inject=["$http","$injector","$log","$q","$rootScope","absync","absyncNoopLog","absyncUncachedFilter"],r.prototype.__onEntityOnWebsocket=function(e){var t=this;t.scope.$broadcast(n.entityName,e[n.entityName])},r.prototype.__onCollectionOnWebsocket=function(e){var t=this;t.scope.$broadcast(n.collectionName,e[n.collectionName])},r.prototype.__onDataAvailable=function(e){function t(e){var t=r.deserializer(e);r.entityCache.push(t),r.entityCache.__lookup&&(r.entityCache.__lookup[t.id]=r.entityCache.length-1)}var r=this;if(Array.isArray(r.entityCache))angular.forEach(e[n.collectionName],t),r.scope.$broadcast("collectionNew",{service:r,cache:r.entityCache});else{var o=r.deserializer(e[n.entityName]);r.__updateCacheWithEntity(o)}return r.entityCache},r.prototype.__onEntityReceived=function(e,t){var r=this,o=t;return r.__entityCacheRaw&&r.__entityCacheRaw[n.collectionName||n.entityName]?1===Object.keys(o).length&&o.hasOwnProperty("id")?(r.logInterface.info(r.logPrefix+"Entity was deleted from the server. Updating cache…"),r.__cacheMaintain(r.__entityCacheRaw[n.collectionName||n.entityName],o,"delete",!1),r.__removeEntityFromCache(o.id)):(r.logInterface.debug(r.logPrefix+"Entity was updated on the server. Updating cache…"),r.__cacheMaintain(r.__entityCacheRaw[n.collectionName||n.entityName],o,"update",!1),r.__updateCacheWithEntity(r.deserializer(o))):r.ensureLoaded().then(function(){return r.read(o.id)})},r.prototype.__onCollectionReceived=function(e,t){function n(e){var t=r.deserializer(e);r.__updateCacheWithEntity(t)}var r=this,o=t;r.entityCache.length=0,angular.forEach(o,n)},r.prototype.ensureLoaded=function(e){function t(e){if(!e.data[n.collectionName])throw new Error("The response from the server was not in the expected format. It should have a member named '"+n.collectionName+"'.");return a.__entityCacheRaw=e.data,a.entityCache.splice(0,a.entityCache.length),a.__onDataAvailable(e.data)}function r(e){if(a.logInterface.error(a.logPrefix+"Unable to retrieve the collection from the server.",e),a.__entityCacheRaw=null,a.scope.$emit("absyncError",e),a.throwFailures)throw e}function o(e){if(!e.data[n.entityName])throw new Error("The response from the server was not in the expected format. It should have a member named '"+n.entityName+"'.");a.__entityCacheRaw=e.data,a.__onDataAvailable(e.data)}function i(e){if(a.logInterface.error(a.logPrefix+"Unable to retrieve the entity from the server.",e),a.__entityCacheRaw=null,a.scope.$emit("absyncError",e),a.throwFailures)throw e}var a=this;if(e=e===!0,e&&delete a.__loading,a.__loading)return a.__loading;if(null===a.__entityCacheRaw||e){if(n.collectionName&&n.collectionUri)a.logInterface.info(a.logPrefix+"Retrieving '"+n.collectionName+"' collection…"),a.__loading=a.httpInterface.get(a.allowBrowserCache.sync?n.collectionUri:a.__uncached(n.collectionUri)).then(t,r);else{if(!n.entityName||!n.entityUri)return a.q.when([]);a.__loading=a.httpInterface.get(a.allowBrowserCache.sync?n.entityUri:a.__uncached(n.entityUri)).then(o,i)}return a.__loading}return a.q.when(a.entityCache)},r.prototype.seed=function(e){var t=this;return t.__entityCacheRaw=e,t.__onDataAvailable(t.__entityCacheRaw)},r.prototype.phantom=function(e){var t=this;if(!t.has(e.id))return t.__onEntityReceived(null,e)},r.prototype.sync=function(){var e=this;return e.__entityCacheRaw=null,e.ensureLoaded(!0)},r.prototype.has=function(e){var t=this;return t.entityCache.__lookup.hasOwnProperty(e)},r.prototype.read=function(e,t){function r(e){if(!e.data[n.entityName])throw new Error("The response from the server was not in the expected format. It should have a member named '"+n.entityName+"'.");var t=e.data[n.entityName];i.__cacheMaintain(i.__entityCacheRaw[n.collectionName||n.entityName],t,"update",!1);var r=i.deserializer(t);return i.__updateCacheWithEntity(r),r}function o(t){if(i.logInterface.error(i.logPrefix+"Unable to retrieve entity with ID '"+e+"' from the server.",t),i.scope.$emit("absyncError",t),i.throwFailures)throw t}var i=this;if(t=t===!0,i.logInterface.debug(i.logPrefix+"Requesting entity '"+e+"' (forceReload:"+t+")…"),!t){var a=0;i.entityCache.__lookup&&(a=i.entityCache.__lookup.hasOwnProperty(e)?i.entityCache.__lookup[e]:i.entityCache.length);for(var c=i.entityCache[a];a<i.entityCache.length;++a,c=i.entityCache[a])if(c.id===e)return i.logInterface.debug(i.logPrefix+"Requested entity '"+e+"' is served from cache."),i.q.when(c)}return i.logInterface.debug(i.logPrefix+"Requested entity '"+e+"' is fetched from backend."),i.__requestEntity(e).then(r,o)},r.prototype.__requestEntity=function(e){function t(e,t){return delete r.__requestCache[e],t}var r=this;if(r.enableRequestCache&&r.__requestCache&&r.__requestCache[e])return r.logInterface.debug(r.logPrefix+"Entity request '"+e+"' served from request cache."),r.__requestCache[e];r.__entityCacheRaw=r.__entityCacheRaw||{},r.__entityCacheRaw[n.collectionName]=r.__entityCacheRaw[n.collectionName]||[];var o=n.entityUri+(e?"/"+e:""),i=r.httpInterface.get(r.allowBrowserCache.request?o:r.__uncached(o)).then(t.bind(r,e));return r.enableRequestCache&&r.__requestCache&&(r.__requestCache[e]=i),i},r.prototype.update=function(e,t){var r=this;t=t||!1;var a=r.reduceComplex(e),c=r.serializer(a),s={};return s[n.entityName]=c,"undefined"!=typeof e.id?r.httpInterface.put(n.entityUri+"/"+e.id,s).then(o.bind(r,t),i.bind(r)):r.httpInterface.post(n.collectionUri,s).then(o.bind(r,t),i.bind(r))},r.prototype.patch=function(e,t){var r=this;t=t||!1;var a=r.reduceComplex(e),c=r.serializer(a),s={};if(s[n.entityName]=c,"undefined"!=typeof e.id)return r.httpInterface.patch(n.entityUri+"/"+e.id,s).then(o.bind(r,t),i.bind(r));throw new Error("Attempted to patch an entity that was never stored on the server.")},r.prototype.create=r.prototype.update,r.prototype["delete"]=function(e){function t(t){return o.__cacheMaintain(o.__entityCacheRaw[n.collectionName||n.entityName],e,"delete",!1),o.__removeEntityFromCache(i)}function r(e){if(o.logInterface.error(e.data),o.scope.$emit("absyncError",e),o.throwFailures)throw e}var o=this,i=e.id;return o.httpInterface["delete"](n.entityUri+"/"+i).then(t)["catch"](r)},r.prototype.__updateCacheWithEntity=function(e){var t=this;return t.logInterface.info(t.logPrefix+"Updating entity '"+(e.id||t.name)+"' in cache…",e),t.__cacheMaintain(t.entityCache,e,"update",!0)},r.prototype.__cacheMaintain=function(e,t,n,r){var o=this,i=0,a=e[i];if(o.filter&&!o.filter(t))return void o.logInterface.info(o.logPrefix+"Entity '"+(t.id||o.name)+"' was filtered.",t);switch(e.__lookup&&(i=e.__lookup.hasOwnProperty(t.id)?e.__lookup[t.id]:e.length,a=e[i]),n){case"update":if(!Array.isArray(e))return r&&o.scope.$broadcast("beforeEntityUpdated",{service:o,cache:e,entity:e,updated:t}),"function"==typeof e.copyFrom?e.copyFrom(t):angular.extend(e,t),void o.scope.$broadcast("entityUpdated",{service:o,cache:e,entity:e});var c=!1;for(angular.noop;i<e.length;++i,a=e[i])if(a.id===t.id){r&&o.scope.$broadcast("beforeEntityUpdated",{service:o,cache:e,entity:e[i],updated:t});var s=e[i];"function"==typeof s.copyFrom?s.copyFrom(t):angular.extend(s,t),c=!0,r&&o.scope.$broadcast("entityUpdated",{service:o,cache:e,entity:e[i]});break}c||(r&&o.scope.$broadcast("beforeEntityNew",{service:o,cache:e,entity:t}),e.push(t),e.__lookup&&(e.__lookup[t.id]=e.length-1),r&&o.scope.$broadcast("entityNew",{service:o,cache:e,entity:t}));break;case"delete":for(angular.noop;i<e.length;++i,a=e[i])if(a.id===t.id){if(r&&o.scope.$broadcast("beforeEntityRemoved",{service:o,cache:e,entity:a}),e.splice(i,1),e.__lookup){delete e.__lookup[t.id];for(var l in e.__lookup)i<=e.__lookup[l]&&--e.__lookup[l]}r&&o.scope.$broadcast("entityRemoved",{service:o,cache:e,entity:a});break}}},r.prototype.__removeEntityFromCache=function(e){var t=this;return t.__cacheMaintain(t.entityCache,{id:e},"delete",!0)},r.prototype.lookupTableById=function(){for(var e=this,t=[],n=0;n<e.entityCache.length;++n)t[e.entityCache[n].id]=e.entityCache[n];return t},r.prototype.reduceComplex=function(e,t){var n=this,r=t?[]:{};for(var o in e)e.hasOwnProperty(o)&&(Array.isArray(e[o])?r[o]=n.reduceComplex(e[o],!0):e[o]&&e[o].id?r[o]=e[o].id:r[o]=e[o]);return r},r.prototype.populateComplex=function(e,t,n,r){function o(o,i){function s(n){return e[t][i]=n,r.crossLink&&a(n,e),e}if("string"!=typeof e[t][i]){if(!r.force||"object"!=typeof e[t][i]||"string"!=typeof e[t][i].id){if(c.throwFailures)throw new Error("The referenced entity did not have an 'id' property that would be expected.");return c.q.when(!1)}e[t][i]=e[t][i].id}return n.read(e[t][i]).then(s)}function i(n){return e[t]=n,r.crossLink&&a(n,e),e}function a(e,t){if(Array.isArray(e[r.crossLinkProperty])){var n=e[r.crossLinkProperty].indexOf(t.id);if(n>-1)return void(e[r.crossLinkProperty][n]=t);for(var o=0,i=e[r.crossLinkProperty][0];o<e[r.crossLinkProperty].length;++o,i=e[r.crossLinkProperty][o]){if(i===t)return;if(i.id===t.id)return void(e[r.crossLinkProperty][o]=t)}return void e[r.crossLinkProperty].push(t)}e[r.crossLinkProperty]=t}var c=this;if(r=r||{},"boolean"==typeof r&&(c.logInterface.warn("Argument 'force' is deprecated. Provide an options hash instead."),r={force:r}),r.force=r.force||!1,r.crossLink=r.crossLink||!1,r.crossLinkProperty=r.crossLinkProperty||"",r.crossLink&&!r.crossLinkProperty&&(c.logInterface.warn("Option 'crossLink' given without 'crossLinkProperty'. Cross-linking will be disabled."),r.crossLink=!1),Array.isArray(e[t])){var s=e[t].map(o);return c.q.all(s)}if("string"!=typeof e[t]){if(!r.force||"object"!=typeof e[t]||"string"!=typeof e[t].id){if(c.throwFailures)throw new Error("The referenced entity did not have an 'id' property that would be expected.");return c.q.when(!1)}e[t]=e[t].id}return n.read(e[t]).then(i)},r.prototype.reset=function(){var e=this;e.entityCache=e.configuration.collectionName?[]:{},e.entityCache.__lookup=e.entityCache.__lookup||{},e.__entityCacheRaw=null,e.__requestCache={}},r}function t(e){return e}angular.module("absync").constant("absyncCache",e)}(),function(){"use strict";function e(){return t}function t(e,t,n,r,o,i,a,c,s,l){this.model=e,this.collectionUri=t,this.entityUri=n;var u=e.prototype.constructor.name.toLowerCase();this.collectionName=r||u+"s",this.entityName=o||u,this.deserialize=i||void 0,this.serialize=a||void 0,this.injector=c||void 0,this.debug=s||!1,this.allowBrowserCache=angular.merge({},{sync:!0,request:!0},l)}angular.module("absync").service("AbsyncServiceConfiguration",e)}(),function(){"use strict";angular.module("absync").constant("absyncNoopLog",{debug:angular.noop,info:angular.noop,warn:angular.noop,error:angular.noop})}(),function(){"use strict";function e(){function e(e){if(!e)return e;var t=-1<e.indexOf("?")?"&":"?",n=(new Date).getTime();return e+t+"t="+n}return e}angular.module("absync").filter("absyncUncached",e)}(); | ||
//# sourceMappingURL=maps/absync.concat.min.js.map |
{ | ||
"name": "absync", | ||
"version": "3.5.2", | ||
"version": "3.6.0", | ||
"description": "absync", | ||
@@ -5,0 +5,0 @@ "main": "dist/development/absync.concat.js", |
@@ -379,2 +379,21 @@ /* globals angular */ | ||
/** | ||
* Insert a phantom into the cache, if no real entity with the same ID exists yet. | ||
* Phantoms bypass the raw entity cache. | ||
* @param {Object} entity | ||
*/ | ||
CacheService.prototype.phantom = function CacheService$phantom( entity ) { | ||
var self = this; | ||
if( self.has( entity.id ) ) { | ||
return; | ||
} | ||
return self.__onEntityReceived( null, entity ); | ||
}; | ||
/** | ||
* Drop existing cache and force reload. | ||
* @returns {Promise.<Array.<configuration.model>>|IPromise.<Array>|IPromise.<void>|Q.Promise.<Array.<configuration.model>>|angular.IPromise.<TResult>} | ||
*/ | ||
CacheService.prototype.sync = function CacheService$sync() { | ||
@@ -389,2 +408,13 @@ var self = this; | ||
/** | ||
* Check if the given element with the given ID is in the cache. | ||
* @param {String} id | ||
* @return {Boolean} true if the element is in the cache; false otherwise. | ||
*/ | ||
CacheService.prototype.has = function CacheService$has( id ) { | ||
var self = this; | ||
return self.entityCache.__lookup.hasOwnProperty( id ); | ||
}; | ||
/** | ||
* Read a single entity from the cache, or load it from the server if required. | ||
@@ -391,0 +421,0 @@ * The entity will be placed into the cache. |
@@ -260,3 +260,3 @@ // jscs:disable requireNamedUnassignedFunctions | ||
it( "should provide an entity when collection is not loaded", function( done ) { | ||
devices.read( 1 ) | ||
devices.read( "1" ) | ||
.then( function( device ) { | ||
@@ -270,2 +270,9 @@ expect( devices.entityCache ).to.be.an( "array" ).with.length( 1 ); | ||
} ); | ||
it( "should know which elements are in the index", function() { | ||
devices.ensureLoaded(); | ||
$httpBackend.flush(); | ||
expect( devices.has( "1" ) ).to.be.true; | ||
expect( devices.has( "nope" ) ).to.be.false; | ||
} ); | ||
} ); | ||
@@ -272,0 +279,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 4 instances in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
0
0
0
2
625259
35
4481