/* Common code for components Handle cache invalidation based on keywords */ const setupCacheOnResourceReady = (component: any, { keywords = [] as string[] } = {}) => { const setup = () => { if (!keywords.length) return; if (component.caching === undefined) { component.caching = 0; } if (component.hasCachedDatas === undefined) { component.hasCachedDatas = false; } let updateScheduled = false; const keywordSet = new Set(keywords); component.resourceCacheListener = (e: Event) => { const resource = e.detail.id || e.detail.resource?.["@id"]; if (!resource) return; const isMatch = Array.from(keywordSet).some((keyword) => { const idx = resource.indexOf(keyword); return idx !== -1 && (idx === 0 || resource[idx - 1] === "/"); }); if (isMatch) { component.caching++; component.hasCachedDatas = false; if (!updateScheduled) { updateScheduled = true; requestAnimationFrame(() => { component.requestUpdate(); updateScheduled = false; }); } } }; component._subscriptions.add(["resourceReady", component.resourceCacheListener]); component._subscribe(); }; if (document.readyState !== "complete") { const listener = () => { if (document.readyState === "complete") { document.removeEventListener("readystatechange", listener); setup(); } }; document.addEventListener("readystatechange", listener); } else { setup(); } }; export default setupCacheOnResourceReady;