| 1 |
= cockpit.js: Object Caching |
| 2 |
|
| 3 |
If the same information is displayed by multiple components in Cockpit, |
| 4 |
`+cockpit.cache()+` provides a way to share data between them. The |
| 5 |
shared data should be simple objects, arrays, and values, and not |
| 6 |
contain functions or other objects. |
| 7 |
|
| 8 |
[[cockpit-cache-func]] |
| 9 |
== cockpit.cache() |
| 10 |
|
| 11 |
.... |
| 12 |
cache = cockpit.cache(key, provider, consumer) |
| 13 |
.... |
| 14 |
|
| 15 |
Create a new cache object. The `+key+` should be a globally unique |
| 16 |
string that describes the data being cached. This string must describe |
| 17 |
the data, across all machines and all versions of cockpit. It is |
| 18 |
customary to include a version number in the `+key+` string. |
| 19 |
|
| 20 |
.... |
| 21 |
function provider(result, key) { |
| 22 |
result("myvalue"); |
| 23 |
|
| 24 |
return { |
| 25 |
close: function() { |
| 26 |
/* closed */ |
| 27 |
} |
| 28 |
}; |
| 29 |
} |
| 30 |
.... |
| 31 |
|
| 32 |
The `+provider+` is a function that will be invoked to start retrieving |
| 33 |
data for the cache. It will be passed a `+result+` function as its first |
| 34 |
argument. The `+result+` should be invoked whenever new data is |
| 35 |
available. The `+key+` argument matches the key string the cache was |
| 36 |
created with. |
| 37 |
|
| 38 |
The `+provider+` can return an object with a `+close+` method. This |
| 39 |
method will be invoked when the cache no longer needs data from the |
| 40 |
provider. |
| 41 |
|
| 42 |
.... |
| 43 |
function consumer(value, key) { |
| 44 |
/* ... */ |
| 45 |
} |
| 46 |
.... |
| 47 |
|
| 48 |
The `+consumer+` is a function that will be passed new values when they |
| 49 |
are available, whether they come from the `+provider+` or a source in a |
| 50 |
different component/frame. |
| 51 |
|
| 52 |
[[cockpit-cache-close]] |
| 53 |
== cache.close() |
| 54 |
|
| 55 |
.... |
| 56 |
cache.close() |
| 57 |
.... |
| 58 |
|
| 59 |
Close a cache and stop calling its `+consumer+`. If the `+provider+` was |
| 60 |
invoked, then the `+close()+` method it returned will be invoked. |