/* Common code for components Handle cache invalidation based on keywords */ const setupCacheInvalidation = ( component: any, { keywords = [] as string[], attributes = ["dataSrc"] } = {} ) => { const setup = () => { if (keywords && attributes) { if (component.caching === undefined) { component.caching = 0; } if (component.hasCachedDatas === undefined) { component.hasCachedDatas = false; } component.cacheListener = (e: Event) => { const resource = e.detail.id || e.detail.resource["@id"]; if (keywords.some((keyword) => resource?.includes(keyword))) { for (const attribute of attributes) { if (component[attribute] && resource !== component[attribute]) { window.sibStore.clearCache(component[attribute]); } } component.caching++; component.hasCachedDatas = false; component.requestUpdate(); } }; component._subscriptions.add(["save", component.cacheListener]); component._subscribe(); } }; if (document.readyState !== "complete") { document.addEventListener("DOMContentLoaded", setup); } else { setup(); } }; export default setupCacheInvalidation;