@momentum-design/components 0.25.1 → 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.
- package/dist/browser/index.js +69 -69
- package/dist/browser/index.js.map +4 -4
- package/dist/components/icon/icon.component.js +18 -17
- package/dist/components/icon/icon.utils.d.ts +19 -1
- package/dist/components/icon/icon.utils.js +64 -6
- package/dist/components/iconprovider/iconprovider.component.d.ts +35 -10
- package/dist/components/iconprovider/iconprovider.component.js +18 -15
- package/dist/components/iconprovider/iconprovider.context.d.ts +2 -2
- package/dist/components/iconprovider/iconprovider.context.js +0 -3
- package/dist/custom-elements.json +31 -13
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/react/iconprovider/index.d.ts +7 -3
- package/dist/react/iconprovider/index.js +7 -3
- package/dist/utils/icon-cache/index.d.ts +32 -0
- package/dist/utils/icon-cache/index.js +54 -0
- package/package.json +1 -1
@@ -101,28 +101,29 @@ class Icon extends Component {
|
|
101
101
|
*/
|
102
102
|
async getIconData() {
|
103
103
|
if (this.iconProviderContext.value) {
|
104
|
-
const { fileExtension, url,
|
104
|
+
const { fileExtension, url, cacheName, cacheStrategy } = this.iconProviderContext.value;
|
105
105
|
if (url && fileExtension && this.name) {
|
106
|
-
// abort the
|
107
|
-
//
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
this.
|
113
|
-
|
114
|
-
}
|
115
|
-
this.abortController = new AbortController();
|
106
|
+
// function to abort the fetch request and create a new signal
|
107
|
+
// (directly passing the abortcontroller to the fetch request per reference
|
108
|
+
// will not work due to JS call-by-sharing behavior)
|
109
|
+
const renewSignal = () => {
|
110
|
+
this.abortController.abort();
|
111
|
+
this.abortController = new AbortController();
|
112
|
+
return this.abortController.signal;
|
113
|
+
};
|
116
114
|
try {
|
117
|
-
// fetch icon
|
118
|
-
const iconData = await dynamicSVGImport(
|
115
|
+
// fetch icon data (including caching logic)
|
116
|
+
const iconData = await dynamicSVGImport({
|
117
|
+
url,
|
118
|
+
name: this.name,
|
119
|
+
fileExtension,
|
120
|
+
cacheName,
|
121
|
+
cacheStrategy,
|
122
|
+
renewSignal,
|
123
|
+
});
|
119
124
|
// parse the fetched icon string to an html element and set the attributes
|
120
125
|
const iconElement = this.prepareIconElement(iconData);
|
121
126
|
this.handleIconLoadedSuccess(iconElement);
|
122
|
-
if (shouldCache) {
|
123
|
-
// store the fetched icon string in the iconsCache map
|
124
|
-
iconsCache.set(this.name, iconData);
|
125
|
-
}
|
126
127
|
}
|
127
128
|
catch (error) {
|
128
129
|
this.handleIconLoadedFailure(error);
|
@@ -1,5 +1,20 @@
|
|
1
|
+
import type { CacheStrategy } from '../../utils/icon-cache';
|
2
|
+
interface Args {
|
3
|
+
url: string;
|
4
|
+
name: string;
|
5
|
+
fileExtension: string;
|
6
|
+
cacheStrategy?: CacheStrategy;
|
7
|
+
cacheName?: string;
|
8
|
+
renewSignal: () => AbortSignal;
|
9
|
+
}
|
1
10
|
/**
|
2
11
|
* Fetches a dynamic SVG icon based on the provided `url`, `name` and `fileExtension`.
|
12
|
+
* The fetch is aborted if the signal is aborted.
|
13
|
+
*
|
14
|
+
* This function also includes the logic to cache the fetched icon using the In Memory Cache or Web Cache API.
|
15
|
+
* If the `cacheStrategy` is set to `web-cache-api` or `in-memory-cache` and `cacheName` is provided,
|
16
|
+
* the fetched icon will be cached using the respective cache.
|
17
|
+
*
|
3
18
|
* It will throw an error if the response is not ok.
|
4
19
|
*
|
5
20
|
* @param url - The base url of the icon
|
@@ -7,8 +22,11 @@
|
|
7
22
|
* @param fileExtension - The file extension of the icon
|
8
23
|
* @param signal - The signal to abort the fetch.
|
9
24
|
* It is used to cancel the fetch when the component is disconnected or updated.
|
25
|
+
* @param cacheStrategy - The cache strategy to use.
|
26
|
+
* @param cacheName - The cache name to use.
|
27
|
+
*
|
10
28
|
* @returns Response string from the fetch
|
11
29
|
* @throws Error if the response is not ok
|
12
30
|
*/
|
13
|
-
declare const dynamicSVGImport: (url
|
31
|
+
declare const dynamicSVGImport: ({ url, name, fileExtension, cacheStrategy, cacheName, renewSignal, }: Args) => Promise<string>;
|
14
32
|
export { dynamicSVGImport };
|
@@ -1,6 +1,25 @@
|
|
1
|
-
|
1
|
+
import { iconsCache } from '../../utils/icon-cache';
|
2
|
+
/**
|
3
|
+
* Utility function for fetching the icon from the provided `request`.
|
4
|
+
* It will throw an error if the response is not ok.
|
5
|
+
* @param request - The request object to fetch the icon
|
6
|
+
* @returns Promise<Response> - The response from the fetch
|
7
|
+
* @throws Error if the response is not ok
|
8
|
+
*/
|
9
|
+
const fetchIcon = async (request) => fetch(request).then((response) => {
|
10
|
+
if (!response.ok) {
|
11
|
+
throw new Error('There was a problem while fetching the icon!');
|
12
|
+
}
|
13
|
+
return response;
|
14
|
+
});
|
2
15
|
/**
|
3
16
|
* Fetches a dynamic SVG icon based on the provided `url`, `name` and `fileExtension`.
|
17
|
+
* The fetch is aborted if the signal is aborted.
|
18
|
+
*
|
19
|
+
* This function also includes the logic to cache the fetched icon using the In Memory Cache or Web Cache API.
|
20
|
+
* If the `cacheStrategy` is set to `web-cache-api` or `in-memory-cache` and `cacheName` is provided,
|
21
|
+
* the fetched icon will be cached using the respective cache.
|
22
|
+
*
|
4
23
|
* It will throw an error if the response is not ok.
|
5
24
|
*
|
6
25
|
* @param url - The base url of the icon
|
@@ -8,14 +27,53 @@
|
|
8
27
|
* @param fileExtension - The file extension of the icon
|
9
28
|
* @param signal - The signal to abort the fetch.
|
10
29
|
* It is used to cancel the fetch when the component is disconnected or updated.
|
30
|
+
* @param cacheStrategy - The cache strategy to use.
|
31
|
+
* @param cacheName - The cache name to use.
|
32
|
+
*
|
11
33
|
* @returns Response string from the fetch
|
12
34
|
* @throws Error if the response is not ok
|
13
35
|
*/
|
14
|
-
const dynamicSVGImport = async (url, name, fileExtension,
|
15
|
-
|
16
|
-
|
17
|
-
|
36
|
+
const dynamicSVGImport = async ({ url, name, fileExtension, cacheStrategy, cacheName, renewSignal, }) => {
|
37
|
+
// abort the previous fetch request if it is still pending
|
38
|
+
// and create a new signal
|
39
|
+
const signal = renewSignal();
|
40
|
+
const request = new Request(`${url}/${name}.${fileExtension}`, {
|
41
|
+
signal,
|
42
|
+
});
|
43
|
+
// if there is no cache defined (cacheName and cacheStrategy properly set),
|
44
|
+
// fetch the icon and return the response
|
45
|
+
if (!cacheName || !cacheStrategy || !['in-memory-cache', 'web-cache-api'].includes(cacheStrategy)) {
|
46
|
+
return fetchIcon(request).then((response) => response.text());
|
18
47
|
}
|
19
|
-
return
|
48
|
+
return iconsCache(cacheName, cacheStrategy).then((iconsCache) => iconsCache
|
49
|
+
.get(request)
|
50
|
+
.then((responseFromCache) => {
|
51
|
+
// **If entry in cache, return**
|
52
|
+
if (responseFromCache) {
|
53
|
+
return responseFromCache;
|
54
|
+
}
|
55
|
+
// **Otherwise, fetch and cache if successful**
|
56
|
+
// Both fetchIcon() and iconsCache.set() "consume" the request,
|
57
|
+
// so we need to make a copy.
|
58
|
+
// (see https://developer.mozilla.org/en-US/docs/Web/API/Request/clone)
|
59
|
+
return fetchIcon(request.clone()).then((response) => {
|
60
|
+
var _a;
|
61
|
+
// This avoids caching responses that we know are errors
|
62
|
+
// (i.e. HTTP status code of 4xx or 5xx).
|
63
|
+
if (response.status < 400 && response.headers.has('content-type')) {
|
64
|
+
// Call .clone() on the response to save copy to cache.
|
65
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/Request/clone
|
66
|
+
return (_a = iconsCache.set) === null || _a === void 0 ? void 0 : _a.call(iconsCache, request, response.clone()).then(() => response.text());
|
67
|
+
}
|
68
|
+
return response.text();
|
69
|
+
});
|
70
|
+
})
|
71
|
+
.catch((error) => {
|
72
|
+
// Note that a HTTP error response (e.g. 404) will NOT trigger
|
73
|
+
// an exception.
|
74
|
+
// It will return a normal response object that has the appropriate
|
75
|
+
// error code set.
|
76
|
+
throw new Error(`Error in caching the Icon ${name}, ${error}`);
|
77
|
+
}));
|
20
78
|
};
|
21
79
|
export { dynamicSVGImport };
|
@@ -8,11 +8,15 @@ import IconProviderContext from './iconprovider.context';
|
|
8
8
|
* that only a url has to be passed in from which the icons will be
|
9
9
|
* fetched.
|
10
10
|
*
|
11
|
-
* If `
|
12
|
-
* in
|
11
|
+
* If `cacheStrategy` is provided, the IconProvider will cache the icons
|
12
|
+
* in the selected cache (either web-api-cache or in-memory-cache),
|
13
|
+
* to avoid fetching the same icon multiple times over the network.
|
13
14
|
* This is useful when the same icon is used multiple times in the application.
|
14
|
-
*
|
15
|
+
* To consider:
|
16
|
+
* - The `in-memory-cache` is not persisted and will be lost when the
|
15
17
|
* IconProvider is removed from the DOM.
|
18
|
+
* - The `web-api-cache` is persisted, but only works in https environments
|
19
|
+
* (https://developer.mozilla.org/en-US/docs/Web/API/Cache).
|
16
20
|
*
|
17
21
|
* @tagname mdc-iconprovider
|
18
22
|
*
|
@@ -32,11 +36,15 @@ declare class IconProvider extends Provider<IconProviderContext> {
|
|
32
36
|
* that only a url has to be passed in from which the icons will be
|
33
37
|
* fetched.
|
34
38
|
*
|
35
|
-
* If `
|
36
|
-
* in
|
39
|
+
* If `cacheStrategy` is provided, the IconProvider will cache the icons
|
40
|
+
* in the selected cache (either web-api-cache or in-memory-cache),
|
41
|
+
* to avoid fetching the same icon multiple times over the network.
|
37
42
|
* This is useful when the same icon is used multiple times in the application.
|
38
|
-
*
|
43
|
+
* To consider:
|
44
|
+
* - The `in-memory-cache` is not persisted and will be lost when the
|
39
45
|
* IconProvider is removed from the DOM.
|
46
|
+
* - The `web-api-cache` is persisted, but only works in https environments
|
47
|
+
* (https://developer.mozilla.org/en-US/docs/Web/API/Cache).
|
40
48
|
*
|
41
49
|
* @tagname mdc-iconprovider
|
42
50
|
*
|
@@ -65,12 +73,29 @@ declare class IconProvider extends Provider<IconProviderContext> {
|
|
65
73
|
*/
|
66
74
|
size?: number;
|
67
75
|
/**
|
68
|
-
*
|
69
|
-
* in a Map to avoid fetching the same icon multiple times
|
76
|
+
* Icons Cache Strategy to use
|
70
77
|
*
|
71
|
-
*
|
78
|
+
* Choose `in-memory-cache` to cache icons in a JS cache (in-memory cache).
|
79
|
+
* Choose `web-cache-api` to cache icons using the Web Cache API.
|
80
|
+
*
|
81
|
+
* NOTE: `cache-name` must be provided if `cache-strategy` is provided.
|
82
|
+
*
|
83
|
+
* If not provided or invalid value provided, the icons will not be cached.
|
84
|
+
* @default undefined
|
85
|
+
*/
|
86
|
+
cacheStrategy?: 'in-memory-cache' | 'web-cache-api';
|
87
|
+
/**
|
88
|
+
* Icons Cache Name to use
|
89
|
+
*
|
90
|
+
* If provided, Icons inside the provider will be cached in the
|
91
|
+
* cache (determined by `cache-strategy`) with the provided name.
|
92
|
+
*
|
93
|
+
* NOTE: `cache-name` requires `cache-strategy` to be set.
|
94
|
+
*
|
95
|
+
* If not provided, the icons will not be cached.
|
96
|
+
* @default undefined
|
72
97
|
*/
|
73
|
-
|
98
|
+
cacheName?: string;
|
74
99
|
private updateValuesInContext;
|
75
100
|
protected updateContext(): void;
|
76
101
|
}
|
@@ -19,11 +19,15 @@ import { ALLOWED_FILE_EXTENSIONS, DEFAULTS, ALLOWED_LENGTH_UNITS } from './iconp
|
|
19
19
|
* that only a url has to be passed in from which the icons will be
|
20
20
|
* fetched.
|
21
21
|
*
|
22
|
-
* If `
|
23
|
-
* in
|
22
|
+
* If `cacheStrategy` is provided, the IconProvider will cache the icons
|
23
|
+
* in the selected cache (either web-api-cache or in-memory-cache),
|
24
|
+
* to avoid fetching the same icon multiple times over the network.
|
24
25
|
* This is useful when the same icon is used multiple times in the application.
|
25
|
-
*
|
26
|
+
* To consider:
|
27
|
+
* - The `in-memory-cache` is not persisted and will be lost when the
|
26
28
|
* IconProvider is removed from the DOM.
|
29
|
+
* - The `web-api-cache` is persisted, but only works in https environments
|
30
|
+
* (https://developer.mozilla.org/en-US/docs/Web/API/Cache).
|
27
31
|
*
|
28
32
|
* @tagname mdc-iconprovider
|
29
33
|
*
|
@@ -52,13 +56,6 @@ class IconProvider extends Provider {
|
|
52
56
|
* @default 1
|
53
57
|
*/
|
54
58
|
this.size = DEFAULTS.SIZE;
|
55
|
-
/**
|
56
|
-
* If the IconProvider should cache the icons
|
57
|
-
* in a Map to avoid fetching the same icon multiple times
|
58
|
-
*
|
59
|
-
* @default false
|
60
|
-
*/
|
61
|
-
this.shouldCache = DEFAULTS.SHOULD_CACHE;
|
62
59
|
}
|
63
60
|
/**
|
64
61
|
* Context object of the IconProvider, to be consumed by child components
|
@@ -78,7 +75,8 @@ class IconProvider extends Provider {
|
|
78
75
|
}
|
79
76
|
this.context.value.url = this.url;
|
80
77
|
this.context.value.size = this.size;
|
81
|
-
this.context.value.
|
78
|
+
this.context.value.cacheName = this.cacheName;
|
79
|
+
this.context.value.cacheStrategy = this.cacheStrategy;
|
82
80
|
if (this.lengthUnit && ALLOWED_LENGTH_UNITS.includes(this.lengthUnit)) {
|
83
81
|
this.context.value.lengthUnit = this.lengthUnit;
|
84
82
|
}
|
@@ -93,7 +91,8 @@ class IconProvider extends Provider {
|
|
93
91
|
|| this.context.value.url !== this.url
|
94
92
|
|| this.context.value.lengthUnit !== this.lengthUnit
|
95
93
|
|| this.context.value.size !== this.size
|
96
|
-
|| this.context.value.
|
94
|
+
|| this.context.value.cacheName !== this.cacheName
|
95
|
+
|| this.context.value.cacheStrategy !== this.cacheStrategy) {
|
97
96
|
this.updateValuesInContext();
|
98
97
|
this.context.updateObservers();
|
99
98
|
}
|
@@ -116,7 +115,11 @@ __decorate([
|
|
116
115
|
__metadata("design:type", Number)
|
117
116
|
], IconProvider.prototype, "size", void 0);
|
118
117
|
__decorate([
|
119
|
-
property({ type:
|
120
|
-
__metadata("design:type",
|
121
|
-
], IconProvider.prototype, "
|
118
|
+
property({ type: String, attribute: 'cache-strategy' }),
|
119
|
+
__metadata("design:type", String)
|
120
|
+
], IconProvider.prototype, "cacheStrategy", void 0);
|
121
|
+
__decorate([
|
122
|
+
property({ type: String, attribute: 'cache-name' }),
|
123
|
+
__metadata("design:type", String)
|
124
|
+
], IconProvider.prototype, "cacheName", void 0);
|
122
125
|
export default IconProvider;
|
@@ -3,8 +3,8 @@ declare class IconProviderContext {
|
|
3
3
|
url?: string;
|
4
4
|
lengthUnit?: string;
|
5
5
|
size?: number;
|
6
|
-
|
7
|
-
|
6
|
+
cacheName?: string;
|
7
|
+
cacheStrategy?: 'in-memory-cache' | 'web-cache-api';
|
8
8
|
static readonly context: {
|
9
9
|
__context__: IconProviderContext;
|
10
10
|
};
|
@@ -1,9 +1,6 @@
|
|
1
1
|
import { createContext } from '@lit/context';
|
2
2
|
import { TAG_NAME } from './iconprovider.constants';
|
3
3
|
class IconProviderContext {
|
4
|
-
constructor() {
|
5
|
-
this.iconsCache = new Map();
|
6
|
-
}
|
7
4
|
}
|
8
5
|
// create typed lit context as part of the IconProviderContext
|
9
6
|
IconProviderContext.context = createContext(TAG_NAME);
|
@@ -3533,7 +3533,7 @@
|
|
3533
3533
|
"declarations": [
|
3534
3534
|
{
|
3535
3535
|
"kind": "class",
|
3536
|
-
"description": "IconProvider component, which allows to be consumed from sub components\n(see `providerUtils.consume` for how to consume)\n\nBundling icons will be up to the consumer of this component, such\nthat only a url has to be passed in from which the icons will be\nfetched.\n\nIf `
|
3536
|
+
"description": "IconProvider component, which allows to be consumed from sub components\n(see `providerUtils.consume` for how to consume)\n\nBundling icons will be up to the consumer of this component, such\nthat only a url has to be passed in from which the icons will be\nfetched.\n\nIf `cacheStrategy` is provided, the IconProvider will cache the icons\nin the selected cache (either web-api-cache or in-memory-cache),\nto avoid fetching the same icon multiple times over the network.\nThis is useful when the same icon is used multiple times in the application.\nTo consider:\n- The `in-memory-cache` is not persisted and will be lost when the\nIconProvider is removed from the DOM.\n- The `web-api-cache` is persisted, but only works in https environments\n(https://developer.mozilla.org/en-US/docs/Web/API/Cache).",
|
3537
3537
|
"name": "IconProvider",
|
3538
3538
|
"slots": [
|
3539
3539
|
{
|
@@ -3594,14 +3594,23 @@
|
|
3594
3594
|
},
|
3595
3595
|
{
|
3596
3596
|
"kind": "field",
|
3597
|
-
"name": "
|
3597
|
+
"name": "cacheStrategy",
|
3598
3598
|
"type": {
|
3599
|
-
"text": "
|
3599
|
+
"text": "'in-memory-cache' | 'web-cache-api' | undefined"
|
3600
3600
|
},
|
3601
|
-
"description": "
|
3602
|
-
"default": "
|
3603
|
-
"attribute": "
|
3604
|
-
|
3601
|
+
"description": "Icons Cache Strategy to use\n\nChoose `in-memory-cache` to cache icons in a JS cache (in-memory cache).\nChoose `web-cache-api` to cache icons using the Web Cache API.\n\nNOTE: `cache-name` must be provided if `cache-strategy` is provided.\n\nIf not provided or invalid value provided, the icons will not be cached.",
|
3602
|
+
"default": "undefined",
|
3603
|
+
"attribute": "cache-strategy"
|
3604
|
+
},
|
3605
|
+
{
|
3606
|
+
"kind": "field",
|
3607
|
+
"name": "cacheName",
|
3608
|
+
"type": {
|
3609
|
+
"text": "string | undefined"
|
3610
|
+
},
|
3611
|
+
"description": "Icons Cache Name to use\n\nIf provided, Icons inside the provider will be cached in the\ncache (determined by `cache-strategy`) with the provided name.\n\nNOTE: `cache-name` requires `cache-strategy` to be set.\n\nIf not provided, the icons will not be cached.",
|
3612
|
+
"default": "undefined",
|
3613
|
+
"attribute": "cache-name"
|
3605
3614
|
},
|
3606
3615
|
{
|
3607
3616
|
"kind": "method",
|
@@ -3656,13 +3665,22 @@
|
|
3656
3665
|
"fieldName": "size"
|
3657
3666
|
},
|
3658
3667
|
{
|
3659
|
-
"name": "
|
3668
|
+
"name": "cache-strategy",
|
3660
3669
|
"type": {
|
3661
|
-
"text": "
|
3670
|
+
"text": "'in-memory-cache' | 'web-cache-api' | undefined"
|
3662
3671
|
},
|
3663
|
-
"description": "
|
3664
|
-
"default": "
|
3665
|
-
"fieldName": "
|
3672
|
+
"description": "Icons Cache Strategy to use\n\nChoose `in-memory-cache` to cache icons in a JS cache (in-memory cache).\nChoose `web-cache-api` to cache icons using the Web Cache API.\n\nNOTE: `cache-name` must be provided if `cache-strategy` is provided.\n\nIf not provided or invalid value provided, the icons will not be cached.",
|
3673
|
+
"default": "undefined",
|
3674
|
+
"fieldName": "cacheStrategy"
|
3675
|
+
},
|
3676
|
+
{
|
3677
|
+
"name": "cache-name",
|
3678
|
+
"type": {
|
3679
|
+
"text": "string | undefined"
|
3680
|
+
},
|
3681
|
+
"description": "Icons Cache Name to use\n\nIf provided, Icons inside the provider will be cached in the\ncache (determined by `cache-strategy`) with the provided name.\n\nNOTE: `cache-name` requires `cache-strategy` to be set.\n\nIf not provided, the icons will not be cached.",
|
3682
|
+
"default": "undefined",
|
3683
|
+
"fieldName": "cacheName"
|
3666
3684
|
}
|
3667
3685
|
],
|
3668
3686
|
"superclass": {
|
@@ -3670,7 +3688,7 @@
|
|
3670
3688
|
"module": "/src/models"
|
3671
3689
|
},
|
3672
3690
|
"tagName": "mdc-iconprovider",
|
3673
|
-
"jsDoc": "/**\n * IconProvider component, which allows to be consumed from sub components\n * (see `providerUtils.consume` for how to consume)\n *\n * Bundling icons will be up to the consumer of this component, such\n * that only a url has to be passed in from which the icons will be\n * fetched.\n *\n * If `
|
3691
|
+
"jsDoc": "/**\n * IconProvider component, which allows to be consumed from sub components\n * (see `providerUtils.consume` for how to consume)\n *\n * Bundling icons will be up to the consumer of this component, such\n * that only a url has to be passed in from which the icons will be\n * fetched.\n *\n * If `cacheStrategy` is provided, the IconProvider will cache the icons\n * in the selected cache (either web-api-cache or in-memory-cache),\n * to avoid fetching the same icon multiple times over the network.\n * This is useful when the same icon is used multiple times in the application.\n * To consider:\n * - The `in-memory-cache` is not persisted and will be lost when the\n * IconProvider is removed from the DOM.\n * - The `web-api-cache` is persisted, but only works in https environments\n * (https://developer.mozilla.org/en-US/docs/Web/API/Cache).\n *\n * @tagname mdc-iconprovider\n *\n * @slot - children\n */",
|
3674
3692
|
"customElement": true
|
3675
3693
|
}
|
3676
3694
|
],
|
package/dist/index.d.ts
CHANGED
@@ -18,6 +18,8 @@ import Radio from './components/radio';
|
|
18
18
|
import VirtualizedList from './components/virtualizedlist';
|
19
19
|
import Tab from './components/tab';
|
20
20
|
import FormfieldGroup from './components/formfieldgroup';
|
21
|
+
import { inMemoryCache, webAPIIconsCache } from './utils/icon-cache';
|
21
22
|
import type { TextType } from './components/text/text.types';
|
22
23
|
export { ThemeProvider, Icon, IconProvider, Avatar, Badge, Presence, Text, Button, Bullet, Marker, Divider, AvatarButton, Input, Link, Toggle, Checkbox, Radio, VirtualizedList, Tab, FormfieldGroup, };
|
23
24
|
export type { TextType, };
|
25
|
+
export { inMemoryCache, webAPIIconsCache };
|
package/dist/index.js
CHANGED
@@ -18,4 +18,6 @@ import Radio from './components/radio';
|
|
18
18
|
import VirtualizedList from './components/virtualizedlist';
|
19
19
|
import Tab from './components/tab';
|
20
20
|
import FormfieldGroup from './components/formfieldgroup';
|
21
|
+
import { inMemoryCache, webAPIIconsCache } from './utils/icon-cache';
|
21
22
|
export { ThemeProvider, Icon, IconProvider, Avatar, Badge, Presence, Text, Button, Bullet, Marker, Divider, AvatarButton, Input, Link, Toggle, Checkbox, Radio, VirtualizedList, Tab, FormfieldGroup, };
|
23
|
+
export { inMemoryCache, webAPIIconsCache };
|
@@ -7,11 +7,15 @@ import Component from '../../components/iconprovider';
|
|
7
7
|
* that only a url has to be passed in from which the icons will be
|
8
8
|
* fetched.
|
9
9
|
*
|
10
|
-
* If `
|
11
|
-
* in
|
10
|
+
* If `cacheStrategy` is provided, the IconProvider will cache the icons
|
11
|
+
* in the selected cache (either web-api-cache or in-memory-cache),
|
12
|
+
* to avoid fetching the same icon multiple times over the network.
|
12
13
|
* This is useful when the same icon is used multiple times in the application.
|
13
|
-
*
|
14
|
+
* To consider:
|
15
|
+
* - The `in-memory-cache` is not persisted and will be lost when the
|
14
16
|
* IconProvider is removed from the DOM.
|
17
|
+
* - The `web-api-cache` is persisted, but only works in https environments
|
18
|
+
* (https://developer.mozilla.org/en-US/docs/Web/API/Cache).
|
15
19
|
*
|
16
20
|
* @tagname mdc-iconprovider
|
17
21
|
*
|
@@ -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 `
|
14
|
-
* in
|
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
|
-
*
|
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
|
*
|
@@ -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