react-instantsearch-core
Version:
⚡ Lightning-fast search for React, by Algolia
117 lines (115 loc) • 4.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "Feeds", {
enumerable: true,
get: function() {
return Feeds;
}
});
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _object_without_properties = require("@swc/helpers/_/_object_without_properties");
var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _to_consumable_array = require("@swc/helpers/_/_to_consumable_array");
var _FeedContainer = require("instantsearch.js/cjs/connectors/feeds/FeedContainer");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _useFeeds = require("../connectors/useFeeds");
var _IndexContext = require("../lib/IndexContext");
var _useIndexContext = require("../lib/useIndexContext");
var _useInstantSearchContext = require("../lib/useInstantSearchContext");
function Feeds(_0) {
var renderFeed = _0.renderFeed, props = _object_without_properties._(_0, [
"renderFeed"
]);
var feedIDs = (0, _useFeeds.useFeeds)(props, {
$$widgetType: 'ais.feeds'
}).feedIDs;
var parentIndex = (0, _useIndexContext.useIndexContext)();
var instantSearchInstance = (0, _useInstantSearchContext.useInstantSearchContext)();
var feedContainersRef = (0, _react.useRef)(new Map());
var removalTimerRef = (0, _react.useRef)(null);
var pendingRemovalsRef = (0, _react.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 = (0, _FeedContainer.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).
(0, _react.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 = _sliced_to_array._(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
]);
(0, _react.useEffect)(function() {
return function() {
if (removalTimerRef.current !== null) {
clearTimeout(removalTimerRef.current);
removalTimerRef.current = null;
}
var containers = feedContainersRef.current;
var toRemove = Array.from(new Set(_to_consumable_array._(containers.values()).concat(_to_consumable_array._(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.IndexContext.Provider, {
key: feedID,
value: container
}, renderFeed({
feedID: feedID
}));
}));
}