1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 | 1× 1× 1× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 12× 12× 12× 12× 12× 1× 12× 28× 2× 2× 2× 2× 1× 1× 1× 1× 1× 1× 28× 1× 28× 12× 12× 12× 11× 11× 11× 1× 1× 10× 10× 10× 10× 1× 1× 1× 28× 2× 2× 2× 28× 1× 1× 1× 28× 9× 9× 9× 9× 9× 5× 4× 4× 5× 5× 1× 5× 5× 5× 5× 5× 5× 1× 28× 5× 5× 5× 5× 5× 5× 5× 5× 5× 1× 5× 5× 28× 28× 28× 1× 1× 28× 1× 1× 1× 1× 1× 1× 1× 28× 6× 6× 6× 28× 16× 16× 16× 16× 12× 12× 12× 4× 2× 1× 2× 2× 2× 2× 2× 1× 2× 12× 10× 5× 10× 10× 5× 12× 4× 4× 4× 2× 4× 4× 2× 4× 4× 28× 2× 2× 28× 28× 28× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 1× 1× 1× 2× 2× 1× 2× 1× 1× 1× 1× 1× 1× 1× 1× 28× 29× 29× 29× 29× 28× 1× | /* globals angular */ /** * Please make note of the following conventions: * 1. Function-scope local variables must be prefixed with a single underscore. * This indicates a temporary variable. * 2. Private variables that are persisted onto publicly accessible entities must be prefixed with two underscores. * This indicates a publicly visible, private variable. * Hiding private variables, by using closures, is discouraged. * Modifying these values from outside of absync is discouraged, but should be respected whenever possible. */ angular .module( "absync" ) .constant( "absyncCache", getServiceConstructor ); /** * A closure to make the configuration available to the cache service. * @param {String} name The name of the service. * @param {AbsyncServiceConfiguration} configuration The configuration for this service. * @returns {CacheService} */ function getServiceConstructor( name, configuration ) { // There is no code here, other than the CacheService definition, followed by "return CacheService;" /** * This service factory is the core of absync. * It returns a CacheService instance that is specialized to the given configuration. * This service will handle keep the stored collection in sync. * @param {angular.IHttpService|Object} $http * @param {angular.auto.IInjectorService|Object} $injector * @param {angular.ILogService|Object} $log * @param {angular.IQService|Object} $q * @param {angular.IRootScopeService|Object} $rootScope * @param {AbsyncProvider} absync * @param {Object} absyncNoopLog A log interface that does nothing. * @param {Object} absyncUncachedFilter A filter that mutates URLs so they will bypass the browser cache. * @returns {CacheService} * @ngInject */ function CacheService( $http, $injector, $log, $q, $rootScope, absyncNoopLog, absync, absyncUncachedFilter ) { var self = this; // Retrieve a reference to the model of the collection that is being cached. var _injector = configuration.injector || $injector; var _injectorHasModel = _injector.has( configuration.model ); Iif( !_injectorHasModel ) { throw new Error( "Unable to construct the '" + name + "' service, because the referenced model '" + configuration.model + "' is not available for injection." ); } var _model = ( typeof configuration.model === "string" ) ? _injector.get( configuration.model ) : configuration.model; // Retrieve the serialization methods. var serializeModel = _model.serialize || configuration.serialize || serializationNoop; var deserializeModel = _model.deserialize || configuration.deserialize || serializationNoop; // Store configuration. self.name = name; self.configuration = configuration; // The entity cache must be constructed as an empty array or object, to allow the user to place watchers on it. // We must never replace the cache with a new array or object, we must always manipulate the existing one. // Otherwise watchers will not behave as the user expects them to. /* @type {Array<configuration.model>|configuration.model} */ self.entityCache = configuration.collectionName ? [] : {}; // The raw cache is data that hasn't been deserialized and is used internally. self.__entityCacheRaw = null; // Should request caching be used at all? self.enableRequestCache = true; // Cache requests made to the backend to avoid multiple, simultaneous requests for the same resource. self.__requestCache = {}; // When we make HTTP requests, the browser is generally allowed to cache the responses. // The server can control this behavior with cache control HTTPS headers. // However, at times it may be desirable to force the browser to always fetch fresh data from the backend. // This hash controls this behavior. self.allowBrowserCache = ( angular.merge || angular.extend )( {}, { // Should browser caching be allowed for initial cache sync operations? sync : true, // Should browser caching be allowed when we retrieve single entities from the backend? request : true }, configuration.allowBrowserCache ); self.__uncached = absyncUncachedFilter; // Use $http by default and expose it on the service. // This allows the user to set a different, possibly decorated, HTTP interface for this service. self.httpInterface = $http; // Do the same for our logger. self.logInterface = configuration.debug ? $log : absyncNoopLog; // The scope on which we broadcast all our relevant events. self.scope = $rootScope; // Keep a reference to $q. self.q = $q; // Prefix log messages with this string. self.logPrefix = "absync:" + name.toLocaleUpperCase() + " "; // If enabled, entities received in response to a create or update API call, will be put into the cache. // Otherwise, absync will wait for them to be published through the websocket channel. self.forceEarlyCacheUpdate = false; // Throws failures so that they can be handled outside of absync. self.throwFailures = true; // Expose the serializer/deserializer so that they can be adjusted at any time. self.serializer = serializeModel; self.deserializer = deserializeModel; // Tell absync to register an event listener for both our entity and its collection. // When we receive these events, we broadcast an equal Angular event on the root scope. // This way the user can already peek at the data (manipulating it is discouraged though). absync.on( configuration.entityName, self.__onEntityOnWebsocket.bind( self ) ); Eif( configuration.collectionName ) { absync.on( configuration.collectionName, self.__onCollectionOnWebsocket.bind( self ) ); } // Now we listen on the root scope for the same events we're firing above. // This is where our own absync synchronization logic kicks in. $rootScope.$on( configuration.entityName, self.__onEntityReceived.bind( self ) ); Eif( configuration.collectionName ) { $rootScope.$on( configuration.collectionName, self.__onCollectionReceived.bind( self ) ); } self.logInterface.info( self.logPrefix + "service was instantiated." ); } /** * Invoked when an entity is received on a websocket. * Translates the websocket event to an Angular event and broadcasts it on the scope. * @param {Object} message * @private */ CacheService.prototype.__onEntityOnWebsocket = function CacheService$onEntityOnWebsocket( message ) { var self = this; self.scope.$broadcast( configuration.entityName, message[ configuration.entityName ] ); }; /** * Invoked when a collection is received on a websocket. * Translates the websocket event to an Angular event and broadcasts it on the scope. * @param {Object} message * @private */ CacheService.prototype.__onCollectionOnWebsocket = function CacheService$onCollectionOnWebsocket( message ) { var self = this; self.scope.$broadcast( configuration.collectionName, message[ configuration.collectionName ] ); }; /** * Event handler for when the initial badge of raw data becomes available. * @param {Array<Object>|Object} rawData * @private */ CacheService.prototype.__onDataAvailable = function CacheService$onDataAvailable( rawData ) { var self = this; Eif( Array.isArray( self.entityCache ) ) { // The symbol self.entityCache is expected to be an empty array. // We initialize it in the constructor to an empty array and we don't expect any writes to have // happened to it. In case writes *did* happen, we assume that whoever wrote to it knows what // they're doing. angular.forEach( rawData[ configuration.collectionName ], deserializeCollectionEntry ); // Notify the rest of the application about a fresh collection. self.scope.$broadcast( "collectionNew", { service : self, cache : self.entityCache } ); } else { var deserialized = self.deserializer( rawData[ configuration.entityName ] ); self.__updateCacheWithEntity( deserialized ); } return self.entityCache; function deserializeCollectionEntry( rawEntity ) { self.entityCache.push( self.deserializer( rawEntity ) ); } }; /** * Event handler for when an entity is received on the root scope. * @param {Object} event The event object. * @param {Object} args The raw object as it was read from the wire. * @private */ CacheService.prototype.__onEntityReceived = function CacheService$onEntityReceived( event, args ) { var self = this; var _entityReceived = args; // Check if our raw entity cache was even initialized. // It's possible that it isn't, because websocket updates can be received before any manual requests // were made to the backend. Iif( !self.__entityCacheRaw || !self.__entityCacheRaw[ configuration.collectionName || configuration.entityName ] ) { // We ignore this update and just stack a new read request on top of any existing ones. // This makes sure that we load the freshest entity in an orderly fashion and lose the state we received // here, as we're getting the latest version of the entity. return self.read( _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…" ); self.__cacheMaintain( self.__entityCacheRaw[ configuration.collectionName || configuration.entityName ], _entityReceived, "delete", false ); return self.__removeEntityFromCache( _entityReceived.id ); } else { self.logInterface.debug( self.logPrefix + "Entity was updated on the server. Updating cache…" ); self.__cacheMaintain( self.__entityCacheRaw[ configuration.collectionName || configuration.entityName ], _entityReceived, "update", false ); return self.__updateCacheWithEntity( self.deserializer( _entityReceived ) ); } }; /** * Event handler for when a collection is received on the root scope. * @param {Object} event The event object. * @param {Array<Object>} args The raw collection as it was read from the wire. * @private */ CacheService.prototype.__onCollectionReceived = function CacheService$onCollectionReceived( event, args ) { var self = this; var _collectionReceived = args; // When we're receiving a full collection, all data we currently have in our cache is useless. // We reset the length of the array here, because assigning a new array would possibly conflict // with watchers placed on the original object. self.entityCache.length = 0; // Deserialize the received data and place the models in our cache. angular.forEach( _collectionReceived, addEntityToCache ); function addEntityToCache( entityReceived ) { var deserialized = self.deserializer( entityReceived ); self.__updateCacheWithEntity( deserialized ); } }; /** * Ensure that the cached collection is retrieved from the server. * @param {Boolean} [forceReload=false] Should the data be loaded, even if the service already has a local cache? * @returns {Promise<Array<configuration.model>>|IPromise<Array>|IPromise<void>|Q.Promise<Array<configuration.model>>|angular.IPromise<TResult>} */ CacheService.prototype.ensureLoaded = function CacheService$ensureLoaded( forceReload ) { var self = this; forceReload = forceReload === true; // We only perform any loading, if we don't have raw data cached yet, or if we're forced. if( null === self.__entityCacheRaw || forceReload ) { Iif( !configuration.collectionName || !configuration.collectionUri ) { if( configuration.entityName && configuration.entityUri ) { return self.httpInterface .get( self.allowBrowserCache.sync ? configuration.entityUri : self.__uncached( configuration.entityUri ) ) .then( onSingleEntityReceived, onSingleEntityRetrievalFailure ); } else { // If the user did not provide information necessary to work with a collection, immediately return // a promise for an empty collection. The user could still use read() to grab individual entities. return self.q.when( [] ); } } else { self.logInterface.info( self.logPrefix + "Retrieving '" + configuration.collectionName + "' collection…" ); return self.httpInterface .get( self.allowBrowserCache.sync ? configuration.collectionUri : self.__uncached( configuration.collectionUri ) ) .then( onCollectionReceived, onCollectionRetrievalFailure ); } } return self.q.when( self.entityCache ); /** * Invoked when the collection was received from the server. * @param {angular.IHttpPromiseCallbackArg|Object} serverResponse The reply sent from the server. */ function onCollectionReceived( serverResponse ) { Iif( !serverResponse.data[ configuration.collectionName ] ) { throw new Error( "The response from the server was not in the expected format. It should have a member named '" + configuration.collectionName + "'." ); } self.__entityCacheRaw = serverResponse.data; self.entityCache.splice( 0, self.entityCache.length ); return self.__onDataAvailable( serverResponse.data ); } /** * Invoked when there was an error while trying to retrieve the collection from the server. * @param {angular.IHttpPromiseCallbackArg|Object} serverResponse The reply sent from the server. */ function onCollectionRetrievalFailure( serverResponse ) { self.logInterface.error( self.logPrefix + "Unable to retrieve the collection from the server.", serverResponse ); self.__entityCacheRaw = null; self.scope.$emit( "absyncError", serverResponse ); if( self.throwFailures ) { throw serverResponse; } } /** * Invoked when the entity was received from the server. * @param {angular.IHttpPromiseCallbackArg|Object} serverResponse The reply sent from the server. */ function onSingleEntityReceived( serverResponse ) { if( !serverResponse.data[ configuration.entityName ] ) { throw new Error( "The response from the server was not in the expected format. It should have a member named '" + configuration.entityName + "'." ); } self.__entityCacheRaw = serverResponse.data; self.__onDataAvailable( serverResponse.data ); } /** * Invoked when there was an error while trying to retrieve the entity from the server. * @param {angular.IHttpPromiseCallbackArg|Object} serverResponse The reply sent from the server. */ function onSingleEntityRetrievalFailure( serverResponse ) { self.logInterface.error( self.logPrefix + "Unable to retrieve the entity from the server.", serverResponse ); self.__entityCacheRaw = null; self.scope.$emit( "absyncError", serverResponse ); if( self.throwFailures ) { throw serverResponse; } } }; /** * Pre-seed the cache with the given value. * Usually, you'd want to follow this up with a sync() to get fully in sync with the backend. * @param {Object|Array<Object>} cache * @returns {CacheService} */ CacheService.prototype.seed = function CacheService$seed( cache ) { var self = this; self.__entityCacheRaw = cache; return self.__onDataAvailable( self.__entityCacheRaw ); }; CacheService.prototype.sync = function CacheService$sync() { var self = this; self.__entityCacheRaw = null; return self.ensureLoaded( true ); }; /** * Read a single entity from the cache, or load it from the server if required. * The entity will be placed into the cache. * @param {String} id The ID of the entity to retrieve. * @param {Boolean} [forceReload=false] Should the entity be retrieved from the server, even if it is already in the cache? * @returns {Promise<configuration.model>|IPromise<TResult>|IPromise<void>|angular.IPromise<TResult>} */ CacheService.prototype.read = function CacheService$read( id, forceReload ) { var self = this; forceReload = forceReload === true; self.logInterface.debug( self.logPrefix + "Requesting entity '" + id + "' (forceReload:" + forceReload + ")…" ); Eif( !forceReload ) { // Check if the entity is in the cache and return instantly if found. for( var entityIndex = 0, entity = self.entityCache[ 0 ]; entityIndex < self.entityCache.length; ++entityIndex, entity = self.entityCache[ entityIndex ] ) { if( entity.id === id ) { self.logInterface.debug( self.logPrefix + "Requested entity '" + id + "' is served from cache." ); return self.q.when( entity ); } } } self.logInterface.debug( self.logPrefix + "Requested entity '" + id + "' is fetched from backend." ); return self.__requestEntity( id ) .then( onEntityRetrieved, onEntityRetrievalFailure ); /** * Invoked when the entity was retrieved from the server. * @param {angular.IHttpPromiseCallbackArg|Object} serverResponse The reply sent from the server. */ function onEntityRetrieved( serverResponse ) { Iif( !serverResponse.data[ configuration.entityName ] ) { throw new Error( "The response from the server was not in the expected format. It should have a member named '" + configuration.entityName + "'." ); } var rawEntity = serverResponse.data[ configuration.entityName ]; // Put the raw entity into our raw entity cache. // We keep the raw copy to allow caching of the raw data. self.__cacheMaintain( self.__entityCacheRaw[ configuration.collectionName || configuration.entityName ], rawEntity, "update", false ); // Deserialize the object and place it into the cache. // We do not need to check here if the object already exists in the cache. // While it could be possible that the same entity is retrieved multiple times, __updateCacheWithEntity // will not insert duplicates into the cache. var deserialized = self.deserializer( rawEntity ); self.__updateCacheWithEntity( deserialized ); return deserialized; } /** * Invoked when there was an error while trying to retrieve the entity from the server. * @param {angular.IHttpPromiseCallbackArg|Object} serverResponse The reply sent from the server. */ function onEntityRetrievalFailure( serverResponse ) { self.logInterface.error( self.logPrefix + "Unable to retrieve entity with ID '" + id + "' from the server.", serverResponse ); self.scope.$emit( "absyncError", serverResponse ); if( self.throwFailures ) { throw serverResponse; } } }; /** * Request an entity from the backend. * @param {String} id The ID of the entity. * @returns {Promise<configuration.model>|IPromise<TResult>|IPromise<void>|angular.IPromise<TResult>} * @private */ CacheService.prototype.__requestEntity = function CacheService$requestEntity( id ) { var self = this; Iif( self.enableRequestCache && self.__requestCache && self.__requestCache[ id ] ) { self.logInterface.debug( self.logPrefix + "Entity request '" + id + "' served from request cache." ); return self.__requestCache[ id ]; } // Make sure our raw entity cache exists. self.__entityCacheRaw = self.__entityCacheRaw || {}; self.__entityCacheRaw[ configuration.collectionName ] = self.__entityCacheRaw[ configuration.collectionName ] || []; var requestUri = configuration.entityUri + ( id ? ( "/" + id ) : "" ); // Grab the entity from the backend. var request = self.httpInterface .get( self.allowBrowserCache.request ? requestUri : self.__uncached( requestUri ) ) .then( removeRequestFromCache.bind( self, id ) ); Eif( self.enableRequestCache && self.__requestCache ) { self.__requestCache[ id ] = request; } return request; function removeRequestFromCache( id, serverResponse ) { delete self.__requestCache[ id ]; return serverResponse; } }; /** * Updates an entity and persists it to the backend and the cache. * @param {configuration.model} entity * @param {Boolean} [returnResult=false] Should the result of the query be returned? * @return {Promise<configuration.model>|IPromise<TResult>|angular.IPromise<TResult>} A promise that will be resolved with the updated entity. */ CacheService.prototype.update = function CacheService$update( entity, returnResult ) { var self = this; returnResult = returnResult || false; // First create a copy of the object, which has complex properties reduced to their respective IDs. var reduced = self.reduceComplex( entity ); // Now serialize the object. var serialized = self.serializer( reduced ); // Wrap the entity in a new object, with a single property, named after the entity type. var wrappedEntity = {}; wrappedEntity[ configuration.entityName ] = serialized; // Check if the entity has an "id" property, if it has, we will update. Otherwise, we create. if( "undefined" !== typeof entity.id ) { return self.httpInterface .put( configuration.entityUri + "/" + entity.id, wrappedEntity ) .then( afterEntityStored.bind( self, returnResult ), onEntityStorageFailure.bind( self ) ); } else { // Create a new entity return self.httpInterface .post( configuration.collectionUri, wrappedEntity ) .then( afterEntityStored.bind( self, returnResult ), onEntityStorageFailure.bind( self ) ); } }; CacheService.prototype.patch = function CacheService$patch( entity ) { var self = this; // First create a copy of the object, which has complex properties reduced to their respective IDs. var reduced = self.reduceComplex( entity ); // Now serialize the object. var serialized = self.serializer( reduced ); // Wrap the entity in a new object, with a single property, named after the entity type. var wrappedEntity = {}; wrappedEntity[ configuration.entityName ] = serialized; // Check if the entity has an "id" property, if it has, we will update. Otherwise, we create. if( "undefined" !== typeof entity.id ) { return self.httpInterface .patch( configuration.entityUri + "/" + entity.id, wrappedEntity ) .then( afterEntityStored.bind( self ), onEntityStorageFailure.bind( self ) ); } else { throw new Error( "Attempted to patch an entity that was never stored on the server." ); } }; /** * Creates a new entity and persists it to the backend and the cache. */ CacheService.prototype.create = CacheService.prototype.update; /** * Invoked when the entity was stored on the server. * @param {Boolean} returnResult Should we return the parsed entity that is contained in the response? * @param {angular.IHttpPromiseCallbackArg|Object} serverResponse The reply sent from the server. */ function afterEntityStored( returnResult, serverResponse ) { var self = this; // Writing an entity to the backend will usually invoke an update event to be // broadcast over websockets, where we would also retrieve the updated record. // We still put the updated record we receive here into the cache to ensure early consistency, if that is requested. if( !returnResult && !self.forceEarlyCacheUpdate ) { return; } if( 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 rawEntity; } } } /** * Invoked when there was an error while trying to store the entity on the server. * @param {angular.IHttpPromiseCallbackArg|Object} serverResponse The reply sent from the server. */ function onEntityStorageFailure( serverResponse ) { var self = this; self.logInterface.error( self.logPrefix + "Unable to store entity on the server.", serverResponse ); self.logInterface.error( serverResponse ); self.scope.$emit( "absyncError", serverResponse ); if( self.throwFailures ) { throw serverResponse; } } /** * Remove an entity from the cache and have it deleted on the backend. * @param {Object} entity */ CacheService.prototype.delete = function CacheService$delete( entity ) { var self = this; var entityId = entity.id; return self.httpInterface .delete( configuration.entityUri + "/" + entityId ) .then( onEntityDeleted ) .catch( onEntityDeletionFailed ); /** * Invoked when the entity was successfully deleted from the server. * @param {angular.IHttpPromiseCallbackArg|Object} serverResponse The reply sent from the server. */ function onEntityDeleted( serverResponse ) { self.__cacheMaintain( self.__entityCacheRaw[ configuration.collectionName || configuration.entityName ], entity, "delete", false ); return self.__removeEntityFromCache( entityId ); } /** * Invoked when there was an error while trying to delete the entity from the server. * @param {angular.IHttpPromiseCallbackArg|Object} serverResponse The reply sent from the server. */ function onEntityDeletionFailed( serverResponse ) { self.logInterface.error( serverResponse.data ); self.scope.$emit( "absyncError", serverResponse ); if( self.throwFailures ) { throw serverResponse; } } }; /** * Put an entity into the cache or update the existing record if the entity was already in the cache. * @param {Object} entityToCache * @private */ CacheService.prototype.__updateCacheWithEntity = function CacheService$updateCacheWithEntity( entityToCache ) { var self = this; self.logInterface.info( self.logPrefix + "Updating entity '" + ( entityToCache.id || self.name ) + "' in cache…", entityToCache ); return self.__cacheMaintain( self.entityCache, entityToCache, "update", true ); }; CacheService.prototype.__cacheMaintain = function CacheService$cacheMaintain( cache, entityToCache, operation, emit ) { var self = this; var entityIndex = 0; var entity = cache[ entityIndex ]; switch( operation ) { case "update": Iif( !Array.isArray( cache ) ) { if( emit ) { // Allow the user to intervene in the update process, before updating the entity. self.scope.$broadcast( "beforeEntityUpdated", { service : self, cache : cache, entity : cache, updated : entityToCache } ); } if( typeof cache.copyFrom === "function" ) { cache.copyFrom( entityToCache ); } else { angular.extend( cache, entityToCache ); } // After updating the entity, send another event to allow the user to react. self.scope.$broadcast( "entityUpdated", { service : self, cache : cache, entity : cache } ); return; } var found = false; for( angular.noop; entityIndex < cache.length; ++entityIndex, entity = cache[ entityIndex ] ) { if( entity.id === entityToCache.id ) { if( emit ) { // Allow the user to intervene in the update process, before updating the entity. self.scope.$broadcast( "beforeEntityUpdated", { service : self, cache : cache, entity : cache[ entityIndex ], updated : entityToCache } ); } // Use the "copyFrom" method on the entity, if it exists, otherwise use naive approach. var targetEntity = cache[ entityIndex ]; Iif( typeof targetEntity.copyFrom === "function" ) { targetEntity.copyFrom( entityToCache ); } else { angular.extend( targetEntity, entityToCache ); } found = true; if( emit ) { // After updating the entity, send another event to allow the user to react. self.scope.$broadcast( "entityUpdated", { service : self, cache : cache, entity : cache[ entityIndex ] } ); } break; } } // If the entity wasn't found in our records, it's a new entity. if( !found ) { if( emit ) { self.scope.$broadcast( "beforeEntityNew", { service : self, cache : cache, entity : entityToCache } ); } cache.push( entityToCache ); if( emit ) { self.scope.$broadcast( "entityNew", { service : self, cache : cache, entity : entityToCache } ); } } break; case "delete": // The "delete" operation is not expected to happen for single cached entities. for( angular.noop; entityIndex < cache.length; ++entityIndex, entity = cache[ entityIndex ] ) { Eif( entity.id === entityToCache.id ) { if( emit ) { // Before removing the entity, allow the user to react. self.scope.$broadcast( "beforeEntityRemoved", { service : self, cache : cache, entity : entity } ); } // Remove the entity from the cache. cache.splice( entityIndex, 1 ); if( emit ) { // Send another event to allow the user to take note of the removal. self.scope.$broadcast( "entityRemoved", { service : self, cache : cache, entity : entity } ); } break; } } break; } }; /** * Removes an entity from the internal cache. The entity is not removed from the backend. * @param {String} id The ID of the entity to remove from the cache. * @private */ CacheService.prototype.__removeEntityFromCache = function CacheService$removeEntityFromCache( id ) { var self = this; return self.__cacheMaintain( self.entityCache, { id : id }, "delete", true ); }; /** * Retrieve an associative array of all cached entities, which uses the ID of the entity records as the key in the array. * This is a convenience method that is not utilized internally. * @returns {Array<configuration.model>} */ CacheService.prototype.lookupTableById = function CacheService$lookupTableById() { var self = this; // TODO: Keep a copy of the lookup table and only update it when the cached data updates var lookupTable = []; for( var entityIndex = 0; entityIndex < self.entityCache.length; ++entityIndex ) { lookupTable[ self.entityCache[ entityIndex ].id ] = self.entityCache[ entityIndex ]; } return lookupTable; }; /** * Reduce instances of complex types within an entity with their respective IDs. * Note that no type checks are being performed. Every nested object with an "id" property is treated as a complex type. * @param {Object} entity The entity that should have its complex member reduced. * @param {Boolean} [arrayInsteadOfObject=false] true if the manipulated entity is an array; false if it's an object. * @returns {Object|Array} A copy of the input entity, with complex type instances replaced with their respective ID. */ CacheService.prototype.reduceComplex = function CacheService$reduceComplex( entity, arrayInsteadOfObject ) { var self = this; var result = arrayInsteadOfObject ? [] : {}; for( var propertyName in entity ) { if( !entity.hasOwnProperty( propertyName ) ) { continue; } // Recurse for nested arrays. if( Array.isArray( entity[ propertyName ] ) ) { result[ propertyName ] = self.reduceComplex( entity[ propertyName ], true ); continue; } // Replace complex type with its ID. if( entity[ propertyName ] && entity[ propertyName ].id ) { result[ propertyName ] = entity[ propertyName ].id; continue; } // Just copy over the plain property. result[ propertyName ] = entity[ propertyName ]; } return result; }; /** * Populate references to complex types in an instance. * @param {Object} entity The entity that should be manipulated. * @param {String} propertyName The name of the property of entity which should be populated. * @param {CacheService} cache An instance of another caching service that can provide the complex * type instances which are being referenced in entity. * @param {Object|Boolean} [options] A hash with options relating to the population process. * @param {Boolean} [options.force=false] If true, all complex types will be replaced with references to the * instances in cache; otherwise, only properties that are string representations of complex type IDs will be replaced. * @param {Boolean} [options.crossLink=false] If true, the entity will also be put into a relating property in the * foreign entity. * @param {String} [options.crossLinkProperty] The name of the property in the foreign type into which the entity * should be cross-linked. * @returns {IPromise<TResult>|IPromise<any[]>|IPromise<{}>|angular.IPromise<TResult>} */ CacheService.prototype.populateComplex = function CacheService$populateComplex( entity, propertyName, cache, options ) { var self = this; options = options || {}; Iif( typeof options === "boolean" ) { self.logInterface.warn( "Argument 'force' is deprecated. Provide an options hash instead." ); options = { force : options }; } options.force = options.force || false; options.crossLink = options.crossLink || false; options.crossLinkProperty = options.crossLinkProperty || ""; Iif( options.crossLink && !options.crossLinkProperty ) { self.logInterface.warn( "Option 'crossLink' given without 'crossLinkProperty'. Cross-linking will be disabled." ); options.crossLink = false; } // If the target property is an array, ... Iif( Array.isArray( entity[ propertyName ] ) ) { // ...map the elements in the array to promises. var promises = entity[ propertyName ].map( mapElementToPromise ); return self.q.all( promises ); } else { // We usually assume the properties to be strings (the ID of the referenced complex). Iif( typeof entity[ propertyName ] !== "string" ) { // If "force" is enabled, we check if this non-string property is an object and has an "id" member, which is a string. if( options.force && typeof entity[ propertyName ] === "object" && typeof entity[ propertyName ].id === "string" ) { // If that is true, then we replace the whole object with the ID and continue as usual. entity[ propertyName ] = entity[ propertyName ].id; } else { if( self.throwFailures ) { throw new Error( "The referenced entity did not have an 'id' property that would be expected." ); } return self.q.when( false ); } } // Treat the property as an ID and read the complex with that ID from the cache. return cache.read( entity[ propertyName ] ) .then( onComplexRetrieved ); } function mapElementToPromise( element, index ) { // We usually assume the properties to be strings (the ID of the referenced complex). if( typeof entity[ propertyName ][ index ] !== "string" ) { // If "force" is enabled, we check if this non-string property is an object and has an "id" member, which is a string. if( options.force && typeof entity[ propertyName ][ index ] === "object" && typeof entity[ propertyName ][ index ].id === "string" ) { // If that is true, then we replace the whole object with the ID and continue as usual. entity[ propertyName ][ index ] = entity[ propertyName ][ index ].id; } else { if( self.throwFailures ) { throw new Error( "The referenced entity did not have an 'id' property that would be expected." ); } return self.q.when( false ); } } // Treat the property as an ID and read the complex with that ID from the cache. return cache.read( entity[ propertyName ][ index ] ) .then( onComplexRetrieved ); function onComplexRetrieved( complex ) { // When the complex was retrieved, store it back into the array. entity[ propertyName ][ index ] = complex; if( options.crossLink ) { crossLink( complex, entity ); } return entity; } } function onComplexRetrieved( complex ) { // When the complex was retrieved, store it back into the entity. entity[ propertyName ] = complex; if( options.crossLink ) { crossLink( complex, entity ); } return entity; } function crossLink( complex, entity ) { // If cross-linking is enabled, put our entity into the foreign complex. Eif( Array.isArray( complex[ options.crossLinkProperty ] ) ) { // Check if the entity is already linked into the array. var entityIndex = complex[ options.crossLinkProperty ].indexOf( entity ); Iif( -1 < entityIndex ) { return; } // Check if the ID exists in the array. var idIndex = complex[ options.crossLinkProperty ].indexOf( entity.id ); Iif( -1 < idIndex ) { // Replace the ID with the entity. complex[ options.crossLinkProperty ][ idIndex ] = entity; return; } // Just push the element into the array. complex[ options.crossLinkProperty ].push( entity ); return; } complex[ options.crossLinkProperty ] = entity; } }; /** * 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; } function serializationNoop( model ) { return model; } |