1 | const TYPE_VALUE = 'value';
|
2 | const TYPE_ERROR = 'error';
|
3 |
|
4 | export default function createIconSourceCache() {
|
5 | const cache = new Map();
|
6 |
|
7 | const setValue = (key, value) =>
|
8 | cache.set(key, { type: TYPE_VALUE, data: value });
|
9 |
|
10 | const setError = (key, error) =>
|
11 | cache.set(key, { type: TYPE_ERROR, data: error });
|
12 |
|
13 | const has = key => cache.has(key);
|
14 |
|
15 | const get = key => {
|
16 | if (!cache.has(key)) {
|
17 | return undefined;
|
18 | }
|
19 | const { type, data } = cache.get(key);
|
20 | if (type === TYPE_ERROR) {
|
21 | throw data;
|
22 | }
|
23 | return data;
|
24 | };
|
25 |
|
26 | return { setValue, setError, has, get };
|
27 | }
|