-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathCache.mjs
70 lines (62 loc) · 2.51 KB
/
Cache.mjs
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
// @ts-check
/**
* Cache store.
* @see {@link CacheEventMap `CacheEventMap`} for a map of possible events.
*/
export default class Cache extends EventTarget {
/**
* @param {CacheStore} [store] Initial {@link Cache.store cache store}.
* Defaults to `{}`. Useful for hydrating cache data from a server side
* render prior to the initial client side render.
*/
constructor(store = {}) {
super();
if (typeof store !== "object" || !store || Array.isArray(store))
throw new TypeError("Constructor argument 1 `store` must be an object.");
/**
* Store of cache {@link CacheKey keys} and associated
* {@link CacheValue values}.
* @type {CacheStore}
*/
this.store = store;
}
}
/**
* Map of possible {@linkcode Cache} events. Note that the keys don’t match the
* dispatched event names that dynamically contain the associated
* {@link CacheKey cache key}.
* @typedef {object} CacheEventMap
* @prop {CustomEvent<CacheEventSetDetail>} set Signals that a
* {@link Cache.store cache store} entry was set. The event name starts with
* the {@link CacheKey cache key} of the set entry, followed by `/set`.
* @prop {CustomEvent} stale Signals that a {@link Cache.store cache store}
* entry is now stale (often due to a mutation) and should probably be
* reloaded. The event name starts with the
* {@link CacheKey cache key} of the stale entry, followed by `/stale`.
* @prop {CustomEvent} prune Signals that a {@link Cache.store cache store}
* entry will be deleted unless the event is canceled via
* `event.preventDefault()`. The event name starts with the
* {@link CacheKey cache key} of the entry being pruned, followed by `/prune`.
* @prop {CustomEvent} delete Signals that a {@link Cache.store cache store}
* entry was deleted. The event name starts with the
* {@link CacheKey cache key} of the deleted entry, followed by `/delete`.
*/
/**
* @typedef {object} CacheEventSetDetail
* @prop {CacheValue} cacheValue The set {@link CacheValue cache value}.
*/
/**
* Unique key to access a {@link CacheValue cache value}.
* @typedef {string} CacheKey
*/
/**
* {@link Cache.store Cache store} value. If server side rendering, it should
* be JSON serializable for client hydration. It should contain information
* about any errors that occurred during loading so they can be rendered, and if
* server side rendering, be hydrated on the client.
* @typedef {unknown} CacheValue
*/
/**
* Cache store.
* @typedef {{ [cacheKey: CacheKey]: CacheValue }} CacheStore
*/