goodeggs / angular-cached-resource

An AngularJS module to interact with RESTful resources, even when browser is offline
MIT License
216 stars 29 forks source link

$promise and $httpPromise not always defined on cached objects inside an Array #66

Open makebbekus opened 9 years ago

makebbekus commented 9 years ago

Currently, when you read a single value from the cache, $promise and $httpPromise are set on the object:

    instance = new CachedResource
      $promise:     cacheDeferred.promise
      $httpPromise: httpDeferred.promise

However, when you read an array value from the cache, it's more complicated, and currently $promise and $httpPromise are only set on the Array and not the objects in the array.

@serhalp and I took a pass at fixing this, but it's pretty nuanced. Here's our current thinking about read_array_cache.coffee:

Currently the order is:

  1. Flush writes
  2. Kick off an async HTTP request and sometime later resolve the array $promise and $httpPromise
  3. If we have local data, load it from the cache and resolve the array $promise

Because of the async nature of the HTTP request, there are a couple states where we'd need to manage promises on the individual objects in the arrays:

  1. We have local data so the Array $promise is resolved, but we're waiting on the array $httpPromise, so we want the objects in the array to have their own $promise resolved with the cached object and an $httpPromise that is not resolved
  2. When the HTTP data comes back from the server the Array $promise and $httpPromise are resolved, so we want the objects in the array to have $promise resolved (either it was already resolved with local data in step 3 or if there was no local data it now gets resolved with fresh data) and $httpPromise resolved with the modified data (server + local via modifyObjectInPlace)