-
Notifications
You must be signed in to change notification settings - Fork 24
ReferenceCache
JonathanMontane edited this page May 9, 2016
·
3 revisions
A ReferenceCache component acts as a caching middleware that contains possible resolutions of a Reference. At its core, a ReferenceCache is a Map of References that are derived from the initial cached reference.
/* if in src/ */
import { ReferenceCache } from './models/Reference'
ReferenceCache extends Immutable.Record using the following pattern:
class ReferenceCache extends Immutable.Record({
cached: null,
resolved: new Immutable.OrderedMap(),
final: null
})
-
ReferenceCache.cached
expects a Reference. This Reference is also stored inReferenceCache.resolved.initial
-
ReferenceCache.resolved
expects an Immutable.OrderedMap that contains all the resolved depths. This Reference is also virtually stored inReferenceCache.resolved[-1]
.
-
ReferenceCache.final
expects either aNumber
ornull
depending on whether the full resolution of theReferenceCache.cached
was achieved. It represents the depth at which the resolution is final.
-
ReferenceCache.resolve(references, depth = 0)
returns a new ReferenceCache also containing the newdepth
,resolution
pair. If an equivalentdepth
already exists in this ReferenceCache, it will be replaced. If the resolution atdepth
is equal to the resolution atdepth - 1
, theReferenceCache.final
is set todepth - 1
Please note that this example is abstract, and is there purely to represent the different interactions with ReferenceCache
. The data may be non-sensical.
import references from './samples/references-examples'
import referenceContainers from './samples/referenceContainers-examples'
import { ReferenceCache } from './models/Reference'
let cache = new ReferenceCache({
cached: references[0],
resolved: new Immutable.OrderedMap({
-1: references[0]
}),
final: -1
})
cache = cache
.set('cached', references[1])
.set('resolved', new Immutable.OrderedMap({
-1: references[1],
0: references[2] // Note: for your own sanity, avoid doing this
}))
.set('final', 0)
cache = cache
.resolve(referenceContainers[0], 3)
.resolve(referenceContainers[1], 5) // Note: will use the cached data from the first resolve.