@momentum-design/components 0.25.2 → 0.25.3

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.
@@ -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.3"
41
41
  }