UNPKG

react-instantsearch-core

Version:
108 lines (105 loc) 4.33 kB
import { _ } from '@swc/helpers/esm/_object_without_properties.js'; import { _ as _$1 } from '@swc/helpers/esm/_sliced_to_array.js'; import { _ as _$2 } from '@swc/helpers/esm/_to_consumable_array.js'; import { createFeedContainer } from 'instantsearch.js/es/connectors/feeds/FeedContainer.js'; import React__default, { useRef, useEffect } from 'react'; import { useFeeds } from '../connectors/useFeeds.js'; import { IndexContext } from '../lib/IndexContext.js'; import { useIndexContext } from '../lib/useIndexContext.js'; import { useInstantSearchContext } from '../lib/useInstantSearchContext.js'; function Feeds(_0) { var renderFeed = _0.renderFeed, props = _(_0, [ "renderFeed" ]); var feedIDs = useFeeds(props, { $$widgetType: 'ais.feeds' }).feedIDs; var parentIndex = useIndexContext(); var instantSearchInstance = useInstantSearchContext(); var feedContainersRef = useRef(new Map()); var removalTimerRef = useRef(null); var pendingRemovalsRef = useRef(new Map()); // Create and register new FeedContainers synchronously so SSR and the first // client render can provide the matching feed index context to children. var toAdd = []; feedIDs.forEach(function(feedID) { if (!feedContainersRef.current.has(feedID)) { var pendingContainer = pendingRemovalsRef.current.get(feedID); if (pendingContainer) { pendingRemovalsRef.current.delete(feedID); feedContainersRef.current.set(feedID, pendingContainer); return; } var container = createFeedContainer(feedID, parentIndex, instantSearchInstance); feedContainersRef.current.set(feedID, container); toAdd.push(container); } }); if (toAdd.length > 0) { parentIndex.addWidgets(toAdd); } // Remove containers that are no longer in feedIDs (deferred to match useWidget pattern). useEffect(function() { var containers = feedContainersRef.current; var activeSet = new Set(feedIDs); var toRemove = []; containers.forEach(function(container, id) { if (!activeSet.has(id)) { toRemove.push([ id, container ]); containers.delete(id); } }); if (toRemove.length > 0) { toRemove.forEach(function(param) { var _param = _$1(param, 2), id = _param[0], container = _param[1]; pendingRemovalsRef.current.set(id, container); }); if (removalTimerRef.current !== null) { clearTimeout(removalTimerRef.current); } removalTimerRef.current = setTimeout(function() { var widgetsToRemove = Array.from(pendingRemovalsRef.current.values()); pendingRemovalsRef.current.clear(); removalTimerRef.current = null; if (widgetsToRemove.length > 0) { parentIndex.removeWidgets(widgetsToRemove); } }, 0); } }, [ feedIDs, parentIndex ]); useEffect(function() { return function() { if (removalTimerRef.current !== null) { clearTimeout(removalTimerRef.current); removalTimerRef.current = null; } var containers = feedContainersRef.current; var toRemove = Array.from(new Set(_$2(containers.values()).concat(_$2(pendingRemovalsRef.current.values())))); pendingRemovalsRef.current.clear(); containers.clear(); if (toRemove.length > 0) { parentIndex.removeWidgets(toRemove); } }; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); return /*#__PURE__*/ React__default.createElement(React__default.Fragment, null, feedIDs.map(function(feedID) { var container = feedContainersRef.current.get(feedID); if (!container) { return null; } return /*#__PURE__*/ React__default.createElement(IndexContext.Provider, { key: feedID, value: container }, renderFeed({ feedID: feedID })); })); } export { Feeds };