UNPKG

942 Btext/coffeescriptView Raw
1module.exports = (providerParams) ->
2 buildCachedResourceClass = require('./build_cached_resource_class')
3 Cache = require('./cache')(providerParams)
4
5 class CachedResourceManager
6 constructor: ($resource, $timeout, $q) ->
7 @byKey = {}
8 @build = angular.bind(@, buildCachedResourceClass, $resource, $timeout, $q, providerParams)
9
10 keys: ->
11 Object.keys @byKey
12
13 add: ->
14 args = Array::slice.call arguments
15 CachedResource = @build(args)
16
17 @byKey[CachedResource.$key] = CachedResource
18 CachedResource.$writes.flush()
19
20 CachedResource
21
22 flushQueues: ->
23 CachedResource.$writes.flush() for key, CachedResource of @byKey
24
25 clearCache: ({exceptFor, clearPendingWrites} = {}) ->
26 exceptFor ?= []
27 for key, CachedResource of @byKey when key not in exceptFor
28 CachedResource.$clearCache({clearPendingWrites})
29
30 clearUndefined: ->
31 Cache.clear exceptFor: @keys()