Comparing version 3.0.1 to 3.1.0
@@ -382,10 +382,2 @@ (function() { | ||
// TODO: Using deferreds is an anti-pattern and probably provides no value here. | ||
self.__dataAvailableDeferred = $q.defer(); | ||
self.__objectsAvailableDeferred = $q.defer(); | ||
// A promise that is resolved once initial data synchronization has taken place. | ||
self.dataAvailable = self.__dataAvailableDeferred.promise; | ||
// A promise that is resolved once the received data is extended to models. | ||
self.objectsAvailable = self.__objectsAvailableDeferred.promise; | ||
// Use $http by default and expose it on the service. | ||
@@ -430,6 +422,2 @@ // This allows the user to set a different, possibly decorated, HTTP interface for this service. | ||
// Wait for data to be available. | ||
self.dataAvailable | ||
.then( self.__onDataAvailable.bind( self ) ); | ||
self.logInterface.info( self.logPrefix + "service was instantiated." ); | ||
@@ -475,6 +463,2 @@ } | ||
// Resolve our "objects are available" deferred. | ||
// TODO: We could just as well initialize objectAvailable to the return value of this call block. | ||
self.__objectsAvailableDeferred.resolve( self.entityCache ); | ||
// Notify the rest of the application about a fresh collection. | ||
@@ -489,8 +473,6 @@ self.scope.$broadcast( "collectionNew", { | ||
self.__updateCacheWithEntity( deserialized ); | ||
// Resolve our "objects are available" deferred. | ||
// TODO: We could just as well initialize objectAvailable to the return value of this call block. | ||
self.__objectsAvailableDeferred.resolve( self.entityCache ); | ||
} | ||
return self.entityCache; | ||
function deserializeCollectionEntry( rawEntity ) { | ||
@@ -511,16 +493,12 @@ self.entityCache.push( self.deserializer( rawEntity ) ); | ||
// Defer processing until data has been received initially. | ||
return self.dataAvailable | ||
.then( function handleReceivedEntity() { | ||
// Determine if the received record consists ONLY of an id property, | ||
// which would mean that this record was deleted from the backend. | ||
if( 1 === Object.keys( _entityReceived ).length && _entityReceived.hasOwnProperty( "id" ) ) { | ||
self.logInterface.info( self.logPrefix + "Entity was deleted from the server. Updating cache…" ); | ||
return self.__removeEntityFromCache( _entityReceived.id ); | ||
// Determine if the received record consists ONLY of an id property, | ||
// which would mean that this record was deleted from the backend. | ||
if( 1 === Object.keys( _entityReceived ).length && _entityReceived.hasOwnProperty( "id" ) ) { | ||
self.logInterface.info( self.logPrefix + "Entity was deleted from the server. Updating cache…" ); | ||
return self.__removeEntityFromCache( _entityReceived.id ); | ||
} else { | ||
self.logInterface.debug( self.logPrefix + "Entity was updated on the server. Updating cache…" ); | ||
return self.__updateCacheWithEntity( self.deserializer( _entityReceived ) ); | ||
} | ||
} ); | ||
} else { | ||
self.logInterface.debug( self.logPrefix + "Entity was updated on the server. Updating cache…" ); | ||
return self.__updateCacheWithEntity( self.deserializer( _entityReceived ) ); | ||
} | ||
}; | ||
@@ -564,8 +542,5 @@ | ||
if( null === self.__entityCacheRaw || forceReload ) { | ||
self.__entityCacheRaw = []; | ||
if( !configuration.collectionName || !configuration.collectionUri ) { | ||
if( configuration.entityName && configuration.entityUri ) { | ||
self.__entityCacheRaw = {}; | ||
self.httpInterface | ||
return self.httpInterface | ||
.get( self.allowBrowserCache.sync ? configuration.entityUri : self.__uncached( | ||
@@ -583,3 +558,3 @@ configuration.entityUri ) ) | ||
self.logInterface.info( self.logPrefix + "Retrieving '" + configuration.collectionName + "' collection…" ); | ||
self.httpInterface | ||
return self.httpInterface | ||
.get( self.allowBrowserCache.sync ? configuration.collectionUri : self.__uncached( | ||
@@ -591,12 +566,3 @@ configuration.collectionUri ) ) | ||
// Return a promise that is resolved once the data was read and converted to models. | ||
// When the promise is resolved, it will return a reference to the entity cache. | ||
return self.q.all( | ||
[ | ||
self.dataAvailable, | ||
self.objectsAvailable | ||
] ) | ||
.then( function dataAvailable() { | ||
return self.entityCache; | ||
} ); | ||
return self.q.when( self.entityCache ); | ||
@@ -613,3 +579,4 @@ /** | ||
self.__entityCacheRaw = serverResponse.data; | ||
self.__dataAvailableDeferred.resolve( serverResponse.data ); | ||
self.entityCache.splice( 0, self.entityCache.length ); | ||
return self.__onDataAvailable( serverResponse.data ); | ||
} | ||
@@ -627,3 +594,6 @@ | ||
self.scope.$emit( "absyncError", serverResponse ); | ||
self.__dataAvailableDeferred.reject( serverResponse ); | ||
if( self.throwFailures ) { | ||
throw serverResponse; | ||
} | ||
} | ||
@@ -641,3 +611,3 @@ | ||
self.__entityCacheRaw = serverResponse.data; | ||
self.__dataAvailableDeferred.resolve( serverResponse.data ); | ||
self.__onDataAvailable( serverResponse.data ); | ||
} | ||
@@ -655,3 +625,6 @@ | ||
self.scope.$emit( "absyncError", serverResponse ); | ||
self.__dataAvailableDeferred.reject( serverResponse ); | ||
if( self.throwFailures ) { | ||
throw serverResponse; | ||
} | ||
} | ||
@@ -667,29 +640,6 @@ }; | ||
CacheService.prototype.seed = function CacheService$seed( cache ) { | ||
var self = this; | ||
self.entityCache = cache; | ||
var self = this; | ||
self.__entityCacheRaw = cache; | ||
if( Array.isArray( self.entityCache ) ) { | ||
// Notify the rest of the application about a fresh collection. | ||
self.scope.$broadcast( "collectionNew", { | ||
service : self, | ||
cache : self.entityCache | ||
} ); | ||
} else { | ||
self.scope.$broadcast( "beforeEntityNew", { | ||
service : self, | ||
cache : self.entityCache, | ||
entity : self.entityCache | ||
} ); | ||
self.scope.$broadcast( "entityNew", { | ||
service : self, | ||
cache : self.entityCache, | ||
entity : self.entityCache | ||
} ); | ||
self.__objectsAvailableDeferred.resolve( self.entityCache ); | ||
} | ||
return self; | ||
return self.__onDataAvailable( self.__entityCacheRaw ); | ||
}; | ||
@@ -699,2 +649,5 @@ | ||
var self = this; | ||
self.__entityCacheRaw = null; | ||
return self.ensureLoaded( true ); | ||
@@ -878,10 +831,14 @@ }; | ||
if( serverResponse.data[ configuration.entityName ] ) { | ||
var newEntity = self.deserializer( serverResponse.data[ configuration.entityName ] ); | ||
var rawEntity = serverResponse.data[ configuration.entityName ]; | ||
// If early cache updates are forced, put the return entity into the cache. | ||
if( self.forceEarlyCacheUpdate ) { | ||
var newEntity = self.deserializer( rawEntity ); | ||
self.__updateCacheWithEntity( newEntity ); | ||
if( returnResult ) { | ||
return newEntity; | ||
} | ||
} | ||
if( returnResult ) { | ||
return newEntity; | ||
return rawEntity; | ||
} | ||
@@ -1188,2 +1145,15 @@ } | ||
/** | ||
* Reset the state of the cache service to when it was first instantiated. | ||
* Assumes that the configuration was not touched. | ||
* This method is primarily targeted at testing, but can be useful in production as well. | ||
*/ | ||
CacheService.prototype.reset = function CacheService$reset() { | ||
var self = this; | ||
self.entityCache = self.configuration.collectionName ? [] : {}; | ||
self.__entityCacheRaw = null; | ||
self.__requestCache = {}; | ||
}; | ||
return CacheService; | ||
@@ -1190,0 +1160,0 @@ } |
@@ -87,10 +87,2 @@ (function() { | ||
// TODO: Using deferreds is an anti-pattern and probably provides no value here. | ||
self.__dataAvailableDeferred = $q.defer(); | ||
self.__objectsAvailableDeferred = $q.defer(); | ||
// A promise that is resolved once initial data synchronization has taken place. | ||
self.dataAvailable = self.__dataAvailableDeferred.promise; | ||
// A promise that is resolved once the received data is extended to models. | ||
self.objectsAvailable = self.__objectsAvailableDeferred.promise; | ||
// Use $http by default and expose it on the service. | ||
@@ -135,6 +127,2 @@ // This allows the user to set a different, possibly decorated, HTTP interface for this service. | ||
// Wait for data to be available. | ||
self.dataAvailable | ||
.then( self.__onDataAvailable.bind( self ) ); | ||
self.logInterface.info( self.logPrefix + "service was instantiated." ); | ||
@@ -180,6 +168,2 @@ } | ||
// Resolve our "objects are available" deferred. | ||
// TODO: We could just as well initialize objectAvailable to the return value of this call block. | ||
self.__objectsAvailableDeferred.resolve( self.entityCache ); | ||
// Notify the rest of the application about a fresh collection. | ||
@@ -194,8 +178,6 @@ self.scope.$broadcast( "collectionNew", { | ||
self.__updateCacheWithEntity( deserialized ); | ||
// Resolve our "objects are available" deferred. | ||
// TODO: We could just as well initialize objectAvailable to the return value of this call block. | ||
self.__objectsAvailableDeferred.resolve( self.entityCache ); | ||
} | ||
return self.entityCache; | ||
function deserializeCollectionEntry( rawEntity ) { | ||
@@ -216,16 +198,12 @@ self.entityCache.push( self.deserializer( rawEntity ) ); | ||
// Defer processing until data has been received initially. | ||
return self.dataAvailable | ||
.then( function handleReceivedEntity() { | ||
// Determine if the received record consists ONLY of an id property, | ||
// which would mean that this record was deleted from the backend. | ||
if( 1 === Object.keys( _entityReceived ).length && _entityReceived.hasOwnProperty( "id" ) ) { | ||
self.logInterface.info( self.logPrefix + "Entity was deleted from the server. Updating cache…" ); | ||
return self.__removeEntityFromCache( _entityReceived.id ); | ||
// Determine if the received record consists ONLY of an id property, | ||
// which would mean that this record was deleted from the backend. | ||
if( 1 === Object.keys( _entityReceived ).length && _entityReceived.hasOwnProperty( "id" ) ) { | ||
self.logInterface.info( self.logPrefix + "Entity was deleted from the server. Updating cache…" ); | ||
return self.__removeEntityFromCache( _entityReceived.id ); | ||
} else { | ||
self.logInterface.debug( self.logPrefix + "Entity was updated on the server. Updating cache…" ); | ||
return self.__updateCacheWithEntity( self.deserializer( _entityReceived ) ); | ||
} | ||
} ); | ||
} else { | ||
self.logInterface.debug( self.logPrefix + "Entity was updated on the server. Updating cache…" ); | ||
return self.__updateCacheWithEntity( self.deserializer( _entityReceived ) ); | ||
} | ||
}; | ||
@@ -269,8 +247,5 @@ | ||
if( null === self.__entityCacheRaw || forceReload ) { | ||
self.__entityCacheRaw = []; | ||
if( !configuration.collectionName || !configuration.collectionUri ) { | ||
if( configuration.entityName && configuration.entityUri ) { | ||
self.__entityCacheRaw = {}; | ||
self.httpInterface | ||
return self.httpInterface | ||
.get( self.allowBrowserCache.sync ? configuration.entityUri : self.__uncached( | ||
@@ -288,3 +263,3 @@ configuration.entityUri ) ) | ||
self.logInterface.info( self.logPrefix + "Retrieving '" + configuration.collectionName + "' collection…" ); | ||
self.httpInterface | ||
return self.httpInterface | ||
.get( self.allowBrowserCache.sync ? configuration.collectionUri : self.__uncached( | ||
@@ -296,12 +271,3 @@ configuration.collectionUri ) ) | ||
// Return a promise that is resolved once the data was read and converted to models. | ||
// When the promise is resolved, it will return a reference to the entity cache. | ||
return self.q.all( | ||
[ | ||
self.dataAvailable, | ||
self.objectsAvailable | ||
] ) | ||
.then( function dataAvailable() { | ||
return self.entityCache; | ||
} ); | ||
return self.q.when( self.entityCache ); | ||
@@ -318,3 +284,4 @@ /** | ||
self.__entityCacheRaw = serverResponse.data; | ||
self.__dataAvailableDeferred.resolve( serverResponse.data ); | ||
self.entityCache.splice( 0, self.entityCache.length ); | ||
return self.__onDataAvailable( serverResponse.data ); | ||
} | ||
@@ -332,3 +299,6 @@ | ||
self.scope.$emit( "absyncError", serverResponse ); | ||
self.__dataAvailableDeferred.reject( serverResponse ); | ||
if( self.throwFailures ) { | ||
throw serverResponse; | ||
} | ||
} | ||
@@ -346,3 +316,3 @@ | ||
self.__entityCacheRaw = serverResponse.data; | ||
self.__dataAvailableDeferred.resolve( serverResponse.data ); | ||
self.__onDataAvailable( serverResponse.data ); | ||
} | ||
@@ -360,3 +330,6 @@ | ||
self.scope.$emit( "absyncError", serverResponse ); | ||
self.__dataAvailableDeferred.reject( serverResponse ); | ||
if( self.throwFailures ) { | ||
throw serverResponse; | ||
} | ||
} | ||
@@ -372,29 +345,6 @@ }; | ||
CacheService.prototype.seed = function CacheService$seed( cache ) { | ||
var self = this; | ||
self.entityCache = cache; | ||
var self = this; | ||
self.__entityCacheRaw = cache; | ||
if( Array.isArray( self.entityCache ) ) { | ||
// Notify the rest of the application about a fresh collection. | ||
self.scope.$broadcast( "collectionNew", { | ||
service : self, | ||
cache : self.entityCache | ||
} ); | ||
} else { | ||
self.scope.$broadcast( "beforeEntityNew", { | ||
service : self, | ||
cache : self.entityCache, | ||
entity : self.entityCache | ||
} ); | ||
self.scope.$broadcast( "entityNew", { | ||
service : self, | ||
cache : self.entityCache, | ||
entity : self.entityCache | ||
} ); | ||
self.__objectsAvailableDeferred.resolve( self.entityCache ); | ||
} | ||
return self; | ||
return self.__onDataAvailable( self.__entityCacheRaw ); | ||
}; | ||
@@ -404,2 +354,5 @@ | ||
var self = this; | ||
self.__entityCacheRaw = null; | ||
return self.ensureLoaded( true ); | ||
@@ -583,10 +536,14 @@ }; | ||
if( serverResponse.data[ configuration.entityName ] ) { | ||
var newEntity = self.deserializer( serverResponse.data[ configuration.entityName ] ); | ||
var rawEntity = serverResponse.data[ configuration.entityName ]; | ||
// If early cache updates are forced, put the return entity into the cache. | ||
if( self.forceEarlyCacheUpdate ) { | ||
var newEntity = self.deserializer( rawEntity ); | ||
self.__updateCacheWithEntity( newEntity ); | ||
if( returnResult ) { | ||
return newEntity; | ||
} | ||
} | ||
if( returnResult ) { | ||
return newEntity; | ||
return rawEntity; | ||
} | ||
@@ -893,2 +850,15 @@ } | ||
/** | ||
* Reset the state of the cache service to when it was first instantiated. | ||
* Assumes that the configuration was not touched. | ||
* This method is primarily targeted at testing, but can be useful in production as well. | ||
*/ | ||
CacheService.prototype.reset = function CacheService$reset() { | ||
var self = this; | ||
self.entityCache = self.configuration.collectionName ? [] : {}; | ||
self.__entityCacheRaw = null; | ||
self.__requestCache = {}; | ||
}; | ||
return CacheService; | ||
@@ -895,0 +865,0 @@ } |
@@ -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.__collections={},r.__entities={},r.debug=void 0}function n(e){this.__absyncProvider=e}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.__registerListener=function(e){var t=this;t.$get().__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.$get=function(){return new n(this)},n.prototype.configure=function(e){var t=this.__absyncProvider;t.configure(e)},n.prototype.on=function(e,t){var n=this.__absyncProvider,r=this;return n.__ioSocket?r.__handleEntityEvent(e,t):n.__registerLater.length>8192?null:(n.__registerLater.push({eventName:e,callback:t}),null)},n.prototype.__handleEntityEvent=function(e,t){var n=this.__absyncProvider;return n.__ioSocket.on(e,t),function(){n.__ioSocket.removeListener(e,t)}},n.prototype.emit=function(e,t,n){var r=this.__absyncProvider;if(!r.__ioSocket)throw new Error("socket.io is not initialized.");r.__ioSocket.emit(e,t,function(){n&&n.apply(r.__ioSocket,arguments)})}}(),function(){"use strict";function e(e,n){function r(r,i,o,a,c,s,l,h){var u=this,d=n.injector||i,y=d.has(n.model);if(!y)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,_=f.serialize||n.serialize||t,p=f.deserialize||n.deserialize||t;u.name=e,u.configuration=n,u.entityCache=n.collectionName?[]:{},u.__entityCacheRaw=null,u.enableRequestCache=!0,u.__requestCache={},u.allowBrowserCache=(angular.merge||angular.extend)({},{sync:!0,request:!0},n.allowBrowserCache),u.__uncached=h,u.__dataAvailableDeferred=a.defer(),u.__objectsAvailableDeferred=a.defer(),u.dataAvailable=u.__dataAvailableDeferred.promise,u.objectsAvailable=u.__objectsAvailableDeferred.promise,u.httpInterface=r,u.logInterface=n.debug?o:l,u.scope=c,u.q=a,u.logPrefix="absync:"+e.toLocaleUpperCase()+" ",u.forceEarlyCacheUpdate=!1,u.throwFailures=!0,u.serializer=_,u.deserializer=p,s.on(n.entityName,u.__onEntityOnWebsocket.bind(u)),n.collectionName&&s.on(n.collectionName,u.__onCollectionOnWebsocket.bind(u)),c.$on(n.entityName,u.__onEntityReceived.bind(u)),n.collectionName&&c.$on(n.collectionName,u.__onCollectionReceived.bind(u)),u.dataAvailable.then(u.__onDataAvailable.bind(u)),u.logInterface.info(u.logPrefix+"service was instantiated.")}function i(e,t){var r=this;if((e||r.forceEarlyCacheUpdate)&&t.data[n.entityName]){var i=r.deserializer(t.data[n.entityName]);if(r.forceEarlyCacheUpdate&&r.__updateCacheWithEntity(i),e)return i}}function o(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){r.entityCache.push(r.deserializer(e))}var r=this;if(Array.isArray(r.entityCache))angular.forEach(e[n.collectionName],t),r.__objectsAvailableDeferred.resolve(r.entityCache),r.scope.$broadcast("collectionNew",{service:r,cache:r.entityCache});else{var i=r.deserializer(e[n.entityName]);r.__updateCacheWithEntity(i),r.__objectsAvailableDeferred.resolve(r.entityCache)}},r.prototype.__onEntityReceived=function(e,t){var n=this,r=t;return n.dataAvailable.then(function(){return 1===Object.keys(r).length&&r.hasOwnProperty("id")?(n.logInterface.info(n.logPrefix+"Entity was deleted from the server. Updating cache…"),n.__removeEntityFromCache(r.id)):(n.logInterface.debug(n.logPrefix+"Entity was updated on the server. Updating cache…"),n.__updateCacheWithEntity(n.deserializer(r)))})},r.prototype.__onCollectionReceived=function(e,t){function n(e){var t=r.deserializer(e);r.__updateCacheWithEntity(t)}var r=this,i=t;r.entityCache.length=0,angular.forEach(i,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+"'.");a.__entityCacheRaw=e.data,a.__dataAvailableDeferred.resolve(e.data)}function r(e){a.logInterface.error(a.logPrefix+"Unable to retrieve the collection from the server.",e),a.__entityCacheRaw=null,a.scope.$emit("absyncError",e),a.__dataAvailableDeferred.reject(e)}function i(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.__dataAvailableDeferred.resolve(e.data)}function o(e){a.logInterface.error(a.logPrefix+"Unable to retrieve the entity from the server.",e),a.__entityCacheRaw=null,a.scope.$emit("absyncError",e),a.__dataAvailableDeferred.reject(e)}var a=this;if(e=e===!0,null===a.__entityCacheRaw||e)if(a.__entityCacheRaw=[],n.collectionName&&n.collectionUri)a.logInterface.info(a.logPrefix+"Retrieving '"+n.collectionName+"' collection…"),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.__entityCacheRaw={},a.httpInterface.get(a.allowBrowserCache.sync?n.entityUri:a.__uncached(n.entityUri)).then(i,o)}return a.q.all([a.dataAvailable,a.objectsAvailable]).then(function(){return a.entityCache})},r.prototype.seed=function(e){var t=this;return t.entityCache=e,Array.isArray(t.entityCache)?t.scope.$broadcast("collectionNew",{service:t,cache:t.entityCache}):(t.scope.$broadcast("beforeEntityNew",{service:t,cache:t.entityCache,entity:t.entityCache}),t.scope.$broadcast("entityNew",{service:t,cache:t.entityCache,entity:t.entityCache}),t.__objectsAvailableDeferred.resolve(t.entityCache)),t},r.prototype.sync=function(){var e=this;return 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=o.deserializer(e.data[n.entityName]);return o.__updateCacheWithEntity(t),t}function i(t){if(o.logInterface.error(o.logPrefix+"Unable to retrieve entity with ID '"+e+"' from the server.",t),o.scope.$emit("absyncError",t),o.throwFailures)throw t}var o=this;if(t=t===!0,o.logInterface.debug(o.logPrefix+"Requesting entity '"+e+"' (forceReload:"+t+")…"),!t)for(var a=0,c=o.entityCache[0];a<o.entityCache.length;++a,c=o.entityCache[a])if(c.id===e)return o.logInterface.debug(o.logPrefix+"Requested entity '"+e+"' is served from cache."),o.q.when(c);return o.logInterface.debug(o.logPrefix+"Requested entity '"+e+"' is fetched from backend."),o.__requestEntity(e).then(r,i)},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];var i=n.entityUri+(e?"/"+e:""),o=r.httpInterface.get(r.allowBrowserCache.request?i:r.__uncached(i)).then(t.bind(r,e));return r.enableRequestCache&&r.__requestCache&&(r.__requestCache[e]=o),o},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(i.bind(r,t),o.bind(r)):r.httpInterface.post(n.collectionUri,s).then(i.bind(r,t),o.bind(r))},r.prototype.patch=function(e){var t=this,r=t.reduceComplex(e),a=t.serializer(r),c={};if(c[n.entityName]=a,"undefined"!=typeof e.id)return t.httpInterface.patch(n.entityUri+"/"+e.id,c).then(i.bind(t),o.bind(t));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(e){return i.__removeEntityFromCache(o)}function r(e){if(i.logInterface.error(e.data),i.scope.$emit("absyncError",e),i.throwFailures)throw e}var i=this,o=e.id;return i.httpInterface["delete"](n.entityUri+"/"+o).then(t)["catch"](r)},r.prototype.__updateCacheWithEntity=function(e){var t=this;if(t.logInterface.info(t.logPrefix+"Updating entity '"+(e.id||t.name)+"' in cache…",e),!Array.isArray(t.entityCache))return t.scope.$broadcast("beforeEntityUpdated",{service:t,cache:t.entityCache,entity:t.entityCache,updated:e}),"function"==typeof t.entityCache.copyFrom?t.entityCache.copyFrom(e):angular.extend(t.entityCache,e),void t.scope.$broadcast("entityUpdated",{service:t,cache:t.entityCache,entity:t.entityCache});for(var n=!1,r=0,i=t.entityCache[0];r<t.entityCache.length;++r,i=t.entityCache[r])if(i.id==e.id){t.scope.$broadcast("beforeEntityUpdated",{service:t,cache:t.entityCache,entity:t.entityCache[r],updated:e});var o=t.entityCache[r];"function"==typeof o.copyFrom?o.copyFrom(e):angular.extend(o,e),n=!0,t.scope.$broadcast("entityUpdated",{service:t,cache:t.entityCache,entity:t.entityCache[r]});break}n||(t.scope.$broadcast("beforeEntityNew",{service:t,cache:t.entityCache,entity:e}),t.entityCache.push(e),t.scope.$broadcast("entityNew",{service:t,cache:t.entityCache,entity:e}))},r.prototype.__removeEntityFromCache=function(e){for(var t=this,n=0,r=t.entityCache[0];n<t.entityCache.length;++n,r=t.entityCache[n])if(r.id==e){t.scope.$broadcast("beforeEntityRemoved",{service:t,cache:t.entityCache,entity:r}),t.entityCache.splice(n,1),t.scope.$broadcast("entityRemoved",{service:t,cache:t.entityCache,entity:r});break}},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 i in e)e.hasOwnProperty(i)&&(Array.isArray(e[i])?r[i]=n.reduceComplex(e[i],!0):e[i]&&e[i].id?r[i]=e[i].id:r[i]=e[i]);return r},r.prototype.populateComplex=function(e,t,n,r){function i(i,o){function c(n){return e[t][o]=n,e}if("string"!=typeof e[t][o]){if(!r||"object"!=typeof e[t][o]||"string"!=typeof e[t][o].id)return a.q.when(!1);e[t][o]=e[t][o].id}return n.read(e[t][o]).then(c)}function o(n){e[t]=n}var a=this;if(Array.isArray(e[t])){var c=e[t].map(i);return a.q.all(c)}if("string"!=typeof e[t]){if(!r||"object"!=typeof e[t]||"string"!=typeof e[t].id)return a.q.when(!1);e[t]=e[t].id}return n.read(e[t]).then(o)},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,i,o,a,c,s,l){this.model=e,this.collectionUri=t,this.entityUri=n;var h=e.prototype.constructor.name.toLowerCase();this.collectionName=r||h+"s",this.entityName=i||h,this.deserialize=o||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,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.__collections={},r.__entities={},r.debug=void 0}function n(e){this.__absyncProvider=e}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.__registerListener=function(e){var t=this;t.$get().__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.$get=function(){return new n(this)},n.prototype.configure=function(e){var t=this.__absyncProvider;t.configure(e)},n.prototype.on=function(e,t){var n=this.__absyncProvider,r=this;return n.__ioSocket?r.__handleEntityEvent(e,t):n.__registerLater.length>8192?null:(n.__registerLater.push({eventName:e,callback:t}),null)},n.prototype.__handleEntityEvent=function(e,t){var n=this.__absyncProvider;return n.__ioSocket.on(e,t),function(){n.__ioSocket.removeListener(e,t)}},n.prototype.emit=function(e,t,n){var r=this.__absyncProvider;if(!r.__ioSocket)throw new Error("socket.io is not initialized.");r.__ioSocket.emit(e,t,function(){n&&n.apply(r.__ioSocket,arguments)})}}(),function(){"use strict";function e(e,n){function r(r,i,o,a,c,s,h,u){var l=this,y=n.injector||i,d=y.has(n.model);if(!d)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?y.get(n.model):n.model,_=f.serialize||n.serialize||t,p=f.deserialize||n.deserialize||t;l.name=e,l.configuration=n,l.entityCache=n.collectionName?[]:{},l.__entityCacheRaw=null,l.enableRequestCache=!0,l.__requestCache={},l.allowBrowserCache=(angular.merge||angular.extend)({},{sync:!0,request:!0},n.allowBrowserCache),l.__uncached=u,l.httpInterface=r,l.logInterface=n.debug?o:h,l.scope=c,l.q=a,l.logPrefix="absync:"+e.toLocaleUpperCase()+" ",l.forceEarlyCacheUpdate=!1,l.throwFailures=!0,l.serializer=_,l.deserializer=p,s.on(n.entityName,l.__onEntityOnWebsocket.bind(l)),n.collectionName&&s.on(n.collectionName,l.__onCollectionOnWebsocket.bind(l)),c.$on(n.entityName,l.__onEntityReceived.bind(l)),n.collectionName&&c.$on(n.collectionName,l.__onCollectionReceived.bind(l)),l.logInterface.info(l.logPrefix+"service was instantiated.")}function i(e,t){var r=this;if((e||r.forceEarlyCacheUpdate)&&t.data[n.entityName]){var i=t.data[n.entityName];if(r.forceEarlyCacheUpdate){var o=r.deserializer(i);if(r.__updateCacheWithEntity(o),e)return o}if(e)return i}}function o(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){r.entityCache.push(r.deserializer(e))}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 i=r.deserializer(e[n.entityName]);r.__updateCacheWithEntity(i)}return r.entityCache},r.prototype.__onEntityReceived=function(e,t){var n=this,r=t;return 1===Object.keys(r).length&&r.hasOwnProperty("id")?(n.logInterface.info(n.logPrefix+"Entity was deleted from the server. Updating cache…"),n.__removeEntityFromCache(r.id)):(n.logInterface.debug(n.logPrefix+"Entity was updated on the server. Updating cache…"),n.__updateCacheWithEntity(n.deserializer(r)))},r.prototype.__onCollectionReceived=function(e,t){function n(e){var t=r.deserializer(e);r.__updateCacheWithEntity(t)}var r=this,i=t;r.entityCache.length=0,angular.forEach(i,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 i(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 o(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;return e=e===!0,null===a.__entityCacheRaw||e?n.collectionName&&n.collectionUri?(a.logInterface.info(a.logPrefix+"Retrieving '"+n.collectionName+"' collection…"),a.httpInterface.get(a.allowBrowserCache.sync?n.collectionUri:a.__uncached(n.collectionUri)).then(t,r)):n.entityName&&n.entityUri?a.httpInterface.get(a.allowBrowserCache.sync?n.entityUri:a.__uncached(n.entityUri)).then(i,o):a.q.when([]):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=o.deserializer(e.data[n.entityName]);return o.__updateCacheWithEntity(t),t}function i(t){if(o.logInterface.error(o.logPrefix+"Unable to retrieve entity with ID '"+e+"' from the server.",t),o.scope.$emit("absyncError",t),o.throwFailures)throw t}var o=this;if(t=t===!0,o.logInterface.debug(o.logPrefix+"Requesting entity '"+e+"' (forceReload:"+t+")…"),!t)for(var a=0,c=o.entityCache[0];a<o.entityCache.length;++a,c=o.entityCache[a])if(c.id===e)return o.logInterface.debug(o.logPrefix+"Requested entity '"+e+"' is served from cache."),o.q.when(c);return o.logInterface.debug(o.logPrefix+"Requested entity '"+e+"' is fetched from backend."),o.__requestEntity(e).then(r,i)},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];var i=n.entityUri+(e?"/"+e:""),o=r.httpInterface.get(r.allowBrowserCache.request?i:r.__uncached(i)).then(t.bind(r,e));return r.enableRequestCache&&r.__requestCache&&(r.__requestCache[e]=o),o},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(i.bind(r,t),o.bind(r)):r.httpInterface.post(n.collectionUri,s).then(i.bind(r,t),o.bind(r))},r.prototype.patch=function(e){var t=this,r=t.reduceComplex(e),a=t.serializer(r),c={};if(c[n.entityName]=a,"undefined"!=typeof e.id)return t.httpInterface.patch(n.entityUri+"/"+e.id,c).then(i.bind(t),o.bind(t));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(e){return i.__removeEntityFromCache(o)}function r(e){if(i.logInterface.error(e.data),i.scope.$emit("absyncError",e),i.throwFailures)throw e}var i=this,o=e.id;return i.httpInterface["delete"](n.entityUri+"/"+o).then(t)["catch"](r)},r.prototype.__updateCacheWithEntity=function(e){var t=this;if(t.logInterface.info(t.logPrefix+"Updating entity '"+(e.id||t.name)+"' in cache…",e),!Array.isArray(t.entityCache))return t.scope.$broadcast("beforeEntityUpdated",{service:t,cache:t.entityCache,entity:t.entityCache,updated:e}),"function"==typeof t.entityCache.copyFrom?t.entityCache.copyFrom(e):angular.extend(t.entityCache,e),void t.scope.$broadcast("entityUpdated",{service:t,cache:t.entityCache,entity:t.entityCache});for(var n=!1,r=0,i=t.entityCache[0];r<t.entityCache.length;++r,i=t.entityCache[r])if(i.id==e.id){t.scope.$broadcast("beforeEntityUpdated",{service:t,cache:t.entityCache,entity:t.entityCache[r],updated:e});var o=t.entityCache[r];"function"==typeof o.copyFrom?o.copyFrom(e):angular.extend(o,e),n=!0,t.scope.$broadcast("entityUpdated",{service:t,cache:t.entityCache,entity:t.entityCache[r]});break}n||(t.scope.$broadcast("beforeEntityNew",{service:t,cache:t.entityCache,entity:e}),t.entityCache.push(e),t.scope.$broadcast("entityNew",{service:t,cache:t.entityCache,entity:e}))},r.prototype.__removeEntityFromCache=function(e){for(var t=this,n=0,r=t.entityCache[0];n<t.entityCache.length;++n,r=t.entityCache[n])if(r.id==e){t.scope.$broadcast("beforeEntityRemoved",{service:t,cache:t.entityCache,entity:r}),t.entityCache.splice(n,1),t.scope.$broadcast("entityRemoved",{service:t,cache:t.entityCache,entity:r});break}},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 i in e)e.hasOwnProperty(i)&&(Array.isArray(e[i])?r[i]=n.reduceComplex(e[i],!0):e[i]&&e[i].id?r[i]=e[i].id:r[i]=e[i]);return r},r.prototype.populateComplex=function(e,t,n,r){function i(i,o){function c(n){return e[t][o]=n,e}if("string"!=typeof e[t][o]){if(!r||"object"!=typeof e[t][o]||"string"!=typeof e[t][o].id)return a.q.when(!1);e[t][o]=e[t][o].id}return n.read(e[t][o]).then(c)}function o(n){e[t]=n}var a=this;if(Array.isArray(e[t])){var c=e[t].map(i);return a.q.all(c)}if("string"!=typeof e[t]){if(!r||"object"!=typeof e[t]||"string"!=typeof e[t].id)return a.q.when(!1);e[t]=e[t].id}return n.read(e[t]).then(o)},r.prototype.reset=function(){var e=this;e.entityCache=e.configuration.collectionName?[]:{},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,i,o,a,c,s,h){this.model=e,this.collectionUri=t,this.entityUri=n;var u=e.prototype.constructor.name.toLowerCase();this.collectionName=r||u+"s",this.entityName=i||u,this.deserialize=o||void 0,this.serialize=a||void 0,this.injector=c||void 0,this.debug=s||!1,this.allowBrowserCache=angular.merge({},{sync:!0,request:!0},h)}angular.module("absync").service("AbsyncServiceConfiguration",e)}(),function(){"use strict";angular.module("absync").constant("absyncNoopLog",{debug:angular.noop,info: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.0.1", | ||
"version": "3.1.0", | ||
"description": "absync", | ||
@@ -5,0 +5,0 @@ "main": "dist/development/absync.concat.js", |
@@ -84,10 +84,2 @@ /* globals angular */ | ||
// TODO: Using deferreds is an anti-pattern and probably provides no value here. | ||
self.__dataAvailableDeferred = $q.defer(); | ||
self.__objectsAvailableDeferred = $q.defer(); | ||
// A promise that is resolved once initial data synchronization has taken place. | ||
self.dataAvailable = self.__dataAvailableDeferred.promise; | ||
// A promise that is resolved once the received data is extended to models. | ||
self.objectsAvailable = self.__objectsAvailableDeferred.promise; | ||
// Use $http by default and expose it on the service. | ||
@@ -132,6 +124,2 @@ // This allows the user to set a different, possibly decorated, HTTP interface for this service. | ||
// Wait for data to be available. | ||
self.dataAvailable | ||
.then( self.__onDataAvailable.bind( self ) ); | ||
self.logInterface.info( self.logPrefix + "service was instantiated." ); | ||
@@ -177,6 +165,2 @@ } | ||
// Resolve our "objects are available" deferred. | ||
// TODO: We could just as well initialize objectAvailable to the return value of this call block. | ||
self.__objectsAvailableDeferred.resolve( self.entityCache ); | ||
// Notify the rest of the application about a fresh collection. | ||
@@ -191,8 +175,6 @@ self.scope.$broadcast( "collectionNew", { | ||
self.__updateCacheWithEntity( deserialized ); | ||
// Resolve our "objects are available" deferred. | ||
// TODO: We could just as well initialize objectAvailable to the return value of this call block. | ||
self.__objectsAvailableDeferred.resolve( self.entityCache ); | ||
} | ||
return self.entityCache; | ||
function deserializeCollectionEntry( rawEntity ) { | ||
@@ -213,16 +195,12 @@ self.entityCache.push( self.deserializer( rawEntity ) ); | ||
// Defer processing until data has been received initially. | ||
return self.dataAvailable | ||
.then( function handleReceivedEntity() { | ||
// Determine if the received record consists ONLY of an id property, | ||
// which would mean that this record was deleted from the backend. | ||
if( 1 === Object.keys( _entityReceived ).length && _entityReceived.hasOwnProperty( "id" ) ) { | ||
self.logInterface.info( self.logPrefix + "Entity was deleted from the server. Updating cache…" ); | ||
return self.__removeEntityFromCache( _entityReceived.id ); | ||
// Determine if the received record consists ONLY of an id property, | ||
// which would mean that this record was deleted from the backend. | ||
if( 1 === Object.keys( _entityReceived ).length && _entityReceived.hasOwnProperty( "id" ) ) { | ||
self.logInterface.info( self.logPrefix + "Entity was deleted from the server. Updating cache…" ); | ||
return self.__removeEntityFromCache( _entityReceived.id ); | ||
} else { | ||
self.logInterface.debug( self.logPrefix + "Entity was updated on the server. Updating cache…" ); | ||
return self.__updateCacheWithEntity( self.deserializer( _entityReceived ) ); | ||
} | ||
} ); | ||
} else { | ||
self.logInterface.debug( self.logPrefix + "Entity was updated on the server. Updating cache…" ); | ||
return self.__updateCacheWithEntity( self.deserializer( _entityReceived ) ); | ||
} | ||
}; | ||
@@ -266,8 +244,5 @@ | ||
if( null === self.__entityCacheRaw || forceReload ) { | ||
self.__entityCacheRaw = []; | ||
if( !configuration.collectionName || !configuration.collectionUri ) { | ||
if( configuration.entityName && configuration.entityUri ) { | ||
self.__entityCacheRaw = {}; | ||
self.httpInterface | ||
return self.httpInterface | ||
.get( self.allowBrowserCache.sync ? configuration.entityUri : self.__uncached( | ||
@@ -285,3 +260,3 @@ configuration.entityUri ) ) | ||
self.logInterface.info( self.logPrefix + "Retrieving '" + configuration.collectionName + "' collection…" ); | ||
self.httpInterface | ||
return self.httpInterface | ||
.get( self.allowBrowserCache.sync ? configuration.collectionUri : self.__uncached( | ||
@@ -293,12 +268,3 @@ configuration.collectionUri ) ) | ||
// Return a promise that is resolved once the data was read and converted to models. | ||
// When the promise is resolved, it will return a reference to the entity cache. | ||
return self.q.all( | ||
[ | ||
self.dataAvailable, | ||
self.objectsAvailable | ||
] ) | ||
.then( function dataAvailable() { | ||
return self.entityCache; | ||
} ); | ||
return self.q.when( self.entityCache ); | ||
@@ -315,3 +281,4 @@ /** | ||
self.__entityCacheRaw = serverResponse.data; | ||
self.__dataAvailableDeferred.resolve( serverResponse.data ); | ||
self.entityCache.splice( 0, self.entityCache.length ); | ||
return self.__onDataAvailable( serverResponse.data ); | ||
} | ||
@@ -329,3 +296,6 @@ | ||
self.scope.$emit( "absyncError", serverResponse ); | ||
self.__dataAvailableDeferred.reject( serverResponse ); | ||
if( self.throwFailures ) { | ||
throw serverResponse; | ||
} | ||
} | ||
@@ -343,3 +313,3 @@ | ||
self.__entityCacheRaw = serverResponse.data; | ||
self.__dataAvailableDeferred.resolve( serverResponse.data ); | ||
self.__onDataAvailable( serverResponse.data ); | ||
} | ||
@@ -357,3 +327,6 @@ | ||
self.scope.$emit( "absyncError", serverResponse ); | ||
self.__dataAvailableDeferred.reject( serverResponse ); | ||
if( self.throwFailures ) { | ||
throw serverResponse; | ||
} | ||
} | ||
@@ -369,29 +342,6 @@ }; | ||
CacheService.prototype.seed = function CacheService$seed( cache ) { | ||
var self = this; | ||
self.entityCache = cache; | ||
var self = this; | ||
self.__entityCacheRaw = cache; | ||
if( Array.isArray( self.entityCache ) ) { | ||
// Notify the rest of the application about a fresh collection. | ||
self.scope.$broadcast( "collectionNew", { | ||
service : self, | ||
cache : self.entityCache | ||
} ); | ||
} else { | ||
self.scope.$broadcast( "beforeEntityNew", { | ||
service : self, | ||
cache : self.entityCache, | ||
entity : self.entityCache | ||
} ); | ||
self.scope.$broadcast( "entityNew", { | ||
service : self, | ||
cache : self.entityCache, | ||
entity : self.entityCache | ||
} ); | ||
self.__objectsAvailableDeferred.resolve( self.entityCache ); | ||
} | ||
return self; | ||
return self.__onDataAvailable( self.__entityCacheRaw ); | ||
}; | ||
@@ -401,2 +351,5 @@ | ||
var self = this; | ||
self.__entityCacheRaw = null; | ||
return self.ensureLoaded( true ); | ||
@@ -580,10 +533,14 @@ }; | ||
if( serverResponse.data[ configuration.entityName ] ) { | ||
var newEntity = self.deserializer( serverResponse.data[ configuration.entityName ] ); | ||
var rawEntity = serverResponse.data[ configuration.entityName ]; | ||
// If early cache updates are forced, put the return entity into the cache. | ||
if( self.forceEarlyCacheUpdate ) { | ||
var newEntity = self.deserializer( rawEntity ); | ||
self.__updateCacheWithEntity( newEntity ); | ||
if( returnResult ) { | ||
return newEntity; | ||
} | ||
} | ||
if( returnResult ) { | ||
return newEntity; | ||
return rawEntity; | ||
} | ||
@@ -890,2 +847,15 @@ } | ||
/** | ||
* Reset the state of the cache service to when it was first instantiated. | ||
* Assumes that the configuration was not touched. | ||
* This method is primarily targeted at testing, but can be useful in production as well. | ||
*/ | ||
CacheService.prototype.reset = function CacheService$reset() { | ||
var self = this; | ||
self.entityCache = self.configuration.collectionName ? [] : {}; | ||
self.__entityCacheRaw = null; | ||
self.__requestCache = {}; | ||
}; | ||
return CacheService; | ||
@@ -892,0 +862,0 @@ } |
@@ -42,2 +42,11 @@ // jscs:disable requireNamedUnassignedFunctions | ||
} ); | ||
$httpBackend | ||
.when( "GET", "/api/device/1" ) | ||
.respond( { | ||
device : { | ||
id : 1, | ||
name : "My Device" | ||
} | ||
} ); | ||
} ) ); | ||
@@ -47,2 +56,3 @@ | ||
devices = _devices_; | ||
devices.reset(); | ||
} ) ); | ||
@@ -60,2 +70,25 @@ | ||
it( "should cached the loaded collection", function() { | ||
devices.ensureLoaded(); | ||
$httpBackend.flush(); | ||
expect( devices.entityCache ).to.be.an( "array" ).with.length( 1 ); | ||
var entity = devices.entityCache[ 0 ]; | ||
devices.ensureLoaded(); | ||
expect( devices.entityCache[ 0 ] ).to.equal( entity ); | ||
} ); | ||
it( "should forget cached collections when reset", function() { | ||
devices.ensureLoaded(); | ||
$httpBackend.flush(); | ||
expect( devices.entityCache ).to.be.an( "array" ).with.length( 1 ); | ||
var entity = devices.entityCache[ 0 ]; | ||
devices.reset(); | ||
devices.ensureLoaded(); | ||
expect( devices.entityCache[ 0 ] ).to.not.equal( entity ); | ||
} ); | ||
it( "should provide an entity", function( done ) { | ||
@@ -66,2 +99,3 @@ devices.ensureLoaded(); | ||
.then( function( device ) { | ||
expect( devices.entityCache ).to.be.an( "array" ).with.length( 1 ); | ||
expect( device ).to.be.an( "object" ).with.property( "name" ).that.equals( "My Device" ); | ||
@@ -73,2 +107,53 @@ } ) | ||
} ); | ||
it( "should provide an entity when collection is not loaded", function( done ) { | ||
devices.read( 1 ) | ||
.then( function( device ) { | ||
expect( devices.entityCache ).to.be.an( "array" ).with.length( 1 ); | ||
expect( device ).to.be.an( "object" ).with.property( "name" ).that.equals( "My Device" ); | ||
} ) | ||
.then( done ) | ||
.catch( done ); | ||
$httpBackend.flush(); | ||
} ); | ||
it( "should provide seeded content", function( done ) { | ||
devices.seed( { | ||
devices : [ { | ||
id : 1, | ||
name : "My Device" | ||
} ] | ||
} | ||
); | ||
devices.read( 1 ) | ||
.then( function( device ) { | ||
expect( device ).to.be.an( "object" ).with.property( "name" ).that.equals( "My Device" ); | ||
} ) | ||
.then( done ) | ||
.catch( done ); | ||
$rootScope.$digest(); | ||
} ); | ||
it( "should provide updated content when syncing after seeding", function( done ) { | ||
var seed = { | ||
devices : [ { | ||
id : 1, | ||
name : "My Device" | ||
} ] | ||
}; | ||
devices.seed( seed ); | ||
devices.sync(); | ||
$httpBackend.flush(); | ||
devices.read( 1 ) | ||
.then( function( device ) { | ||
expect( device ).to.not.equal( seed.devices[ 0 ] ); | ||
} ) | ||
.then( done ) | ||
.catch( done ); | ||
$rootScope.$digest(); | ||
} ); | ||
} ); | ||
@@ -75,0 +160,0 @@ |
Sorry, the diff of this file is not supported yet
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
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
7472017
109
121112