@momentum-design/components 0.25.2 → 0.25.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,11 +10,15 @@ import { TAG_NAME } from '../../components/iconprovider/iconprovider.constants';
10
10
  * that only a url has to be passed in from which the icons will be
11
11
  * fetched.
12
12
  *
13
- * If `shouldCache` is set to true, the IconProvider will cache the icons
14
- * in a Map to avoid fetching the same icon multiple times over the network.
13
+ * If `cacheStrategy` is provided, the IconProvider will cache the icons
14
+ * in the selected cache (either web-api-cache or in-memory-cache),
15
+ * to avoid fetching the same icon multiple times over the network.
15
16
  * This is useful when the same icon is used multiple times in the application.
16
- * Keep in mind that this cache is not persisted and will be lost when the
17
+ * To consider:
18
+ * - The `in-memory-cache` is not persisted and will be lost when the
17
19
  * IconProvider is removed from the DOM.
20
+ * - The `web-api-cache` is persisted, but only works in https environments
21
+ * (https://developer.mozilla.org/en-US/docs/Web/API/Cache).
18
22
  *
19
23
  * @tagname mdc-iconprovider
20
24
  *
@@ -4,8 +4,8 @@ export { default as Badge } from './badge';
4
4
  export { default as Bullet } from './bullet';
5
5
  export { default as Button } from './button';
6
6
  export { default as Buttonsimple } from './buttonsimple';
7
- export { default as Divider } from './divider';
8
7
  export { default as Checkbox } from './checkbox';
8
+ export { default as Divider } from './divider';
9
9
  export { default as FormfieldGroup } from './formfieldgroup';
10
10
  export { default as FormfieldWrapper } from './formfieldwrapper';
11
11
  export { default as Icon } from './icon';
@@ -4,8 +4,8 @@ export { default as Badge } from './badge';
4
4
  export { default as Bullet } from './bullet';
5
5
  export { default as Button } from './button';
6
6
  export { default as Buttonsimple } from './buttonsimple';
7
- export { default as Divider } from './divider';
8
7
  export { default as Checkbox } from './checkbox';
8
+ export { default as Divider } from './divider';
9
9
  export { default as FormfieldGroup } from './formfieldgroup';
10
10
  export { default as FormfieldWrapper } from './formfieldwrapper';
11
11
  export { default as Icon } from './icon';
@@ -0,0 +1,32 @@
1
+ export type CacheStrategy = 'in-memory-cache' | 'web-cache-api';
2
+ /**
3
+ * Cache, using In-Memory Cache (Map)
4
+ * @param name - name of the cache (used as an identifier)
5
+ * @returns Object with set, get and delete methods
6
+ */
7
+ export declare const inMemoryCache: (name: string) => Promise<{
8
+ set(request: Request, response: Response): Promise<void>;
9
+ get(request: Request): Promise<string | undefined>;
10
+ delete(request: Request): Promise<void>;
11
+ }>;
12
+ /**
13
+ * Cache, using Web API Cache
14
+ * @param name - name of the cache (used as an identifier)
15
+ * @returns Object with set, get and delete methods
16
+ */
17
+ export declare const webAPIIconsCache: (name: string) => Promise<{
18
+ set: (request: Request, response: Response) => Promise<void>;
19
+ get: (request: Request) => Promise<string | undefined>;
20
+ delete: (request: Request) => Promise<void>;
21
+ }>;
22
+ /**
23
+ * Function to return the cache based on the cache strategy
24
+ * @param cacheName - name of the cache to be used
25
+ * @param cacheStrategy - strategy to be used for caching
26
+ * @returns the cache based on the strategy
27
+ */
28
+ export declare const iconsCache: (cacheName: string, cacheStrategy: CacheStrategy) => Promise<{
29
+ set(request: Request, response: Response): Promise<void>;
30
+ get(request: Request): Promise<string | undefined>;
31
+ delete(request: Request): Promise<void>;
32
+ }>;
@@ -0,0 +1,54 @@
1
+ const registry = {};
2
+ /**
3
+ * Cache, using In-Memory Cache (Map)
4
+ * @param name - name of the cache (used as an identifier)
5
+ * @returns Object with set, get and delete methods
6
+ */
7
+ export const inMemoryCache = async (name) => {
8
+ if (registry[name] === undefined) {
9
+ registry[name] = new Map();
10
+ }
11
+ return {
12
+ async set(request, response) {
13
+ registry[name].set(request.url, await response.text());
14
+ },
15
+ async get(request) {
16
+ return registry[name].get(request.url);
17
+ },
18
+ async delete(request) {
19
+ registry[name].delete(request.url);
20
+ },
21
+ };
22
+ };
23
+ /**
24
+ * Cache, using Web API Cache
25
+ * @param name - name of the cache (used as an identifier)
26
+ * @returns Object with set, get and delete methods
27
+ */
28
+ export const webAPIIconsCache = async (name) => {
29
+ const cache = await caches.open(name);
30
+ return {
31
+ set: async (request, response) => {
32
+ await cache.put(request, response);
33
+ },
34
+ get: async (request) => {
35
+ const response = await cache.match(request);
36
+ return response === null || response === void 0 ? void 0 : response.text();
37
+ },
38
+ delete: async (request) => {
39
+ await cache.delete(request);
40
+ },
41
+ };
42
+ };
43
+ /**
44
+ * Function to return the cache based on the cache strategy
45
+ * @param cacheName - name of the cache to be used
46
+ * @param cacheStrategy - strategy to be used for caching
47
+ * @returns the cache based on the strategy
48
+ */
49
+ export const iconsCache = async (cacheName, cacheStrategy) => {
50
+ if (cacheStrategy === 'in-memory-cache') {
51
+ return inMemoryCache(cacheName);
52
+ }
53
+ return webAPIIconsCache(cacheName);
54
+ };
package/package.json CHANGED
@@ -37,5 +37,5 @@
37
37
  "lit": "^3.2.0",
38
38
  "uuid": "^11.0.5"
39
39
  },
40
- "version": "0.25.2"
40
+ "version": "0.25.4"
41
41
  }