react-instantsearch-core 7.31.0 → 7.32.0

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.
@@ -0,0 +1,116 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "Feeds", {
7
+ enumerable: true,
8
+ get: function() {
9
+ return Feeds;
10
+ }
11
+ });
12
+ var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
13
+ var _object_without_properties = require("@swc/helpers/_/_object_without_properties");
14
+ var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
15
+ var _to_consumable_array = require("@swc/helpers/_/_to_consumable_array");
16
+ var _FeedContainer = require("instantsearch.js/cjs/connectors/feeds/FeedContainer");
17
+ var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
18
+ var _useFeeds = require("../connectors/useFeeds");
19
+ var _IndexContext = require("../lib/IndexContext");
20
+ var _useIndexContext = require("../lib/useIndexContext");
21
+ var _useInstantSearchContext = require("../lib/useInstantSearchContext");
22
+ function Feeds(_0) {
23
+ var renderFeed = _0.renderFeed, props = _object_without_properties._(_0, [
24
+ "renderFeed"
25
+ ]);
26
+ var feedIDs = (0, _useFeeds.useFeeds)(props, {
27
+ $$widgetType: 'ais.feeds'
28
+ }).feedIDs;
29
+ var parentIndex = (0, _useIndexContext.useIndexContext)();
30
+ var instantSearchInstance = (0, _useInstantSearchContext.useInstantSearchContext)();
31
+ var feedContainersRef = (0, _react.useRef)(new Map());
32
+ var removalTimerRef = (0, _react.useRef)(null);
33
+ var pendingRemovalsRef = (0, _react.useRef)(new Map());
34
+ // Create and register new FeedContainers synchronously so SSR and the first
35
+ // client render can provide the matching feed index context to children.
36
+ var toAdd = [];
37
+ feedIDs.forEach(function(feedID) {
38
+ if (!feedContainersRef.current.has(feedID)) {
39
+ var pendingContainer = pendingRemovalsRef.current.get(feedID);
40
+ if (pendingContainer) {
41
+ pendingRemovalsRef.current.delete(feedID);
42
+ feedContainersRef.current.set(feedID, pendingContainer);
43
+ return;
44
+ }
45
+ var container = (0, _FeedContainer.createFeedContainer)(feedID, parentIndex, instantSearchInstance);
46
+ feedContainersRef.current.set(feedID, container);
47
+ toAdd.push(container);
48
+ }
49
+ });
50
+ if (toAdd.length > 0) {
51
+ parentIndex.addWidgets(toAdd);
52
+ }
53
+ // Remove containers that are no longer in feedIDs (deferred to match useWidget pattern).
54
+ (0, _react.useEffect)(function() {
55
+ var containers = feedContainersRef.current;
56
+ var activeSet = new Set(feedIDs);
57
+ var toRemove = [];
58
+ containers.forEach(function(container, id) {
59
+ if (!activeSet.has(id)) {
60
+ toRemove.push([
61
+ id,
62
+ container
63
+ ]);
64
+ containers.delete(id);
65
+ }
66
+ });
67
+ if (toRemove.length > 0) {
68
+ toRemove.forEach(function(param) {
69
+ var _param = _sliced_to_array._(param, 2), id = _param[0], container = _param[1];
70
+ pendingRemovalsRef.current.set(id, container);
71
+ });
72
+ if (removalTimerRef.current !== null) {
73
+ clearTimeout(removalTimerRef.current);
74
+ }
75
+ removalTimerRef.current = setTimeout(function() {
76
+ var widgetsToRemove = Array.from(pendingRemovalsRef.current.values());
77
+ pendingRemovalsRef.current.clear();
78
+ removalTimerRef.current = null;
79
+ if (widgetsToRemove.length > 0) {
80
+ parentIndex.removeWidgets(widgetsToRemove);
81
+ }
82
+ }, 0);
83
+ }
84
+ }, [
85
+ feedIDs,
86
+ parentIndex
87
+ ]);
88
+ (0, _react.useEffect)(function() {
89
+ return function() {
90
+ if (removalTimerRef.current !== null) {
91
+ clearTimeout(removalTimerRef.current);
92
+ removalTimerRef.current = null;
93
+ }
94
+ var containers = feedContainersRef.current;
95
+ var toRemove = Array.from(new Set(_to_consumable_array._(containers.values()).concat(_to_consumable_array._(pendingRemovalsRef.current.values()))));
96
+ pendingRemovalsRef.current.clear();
97
+ containers.clear();
98
+ if (toRemove.length > 0) {
99
+ parentIndex.removeWidgets(toRemove);
100
+ }
101
+ };
102
+ // eslint-disable-next-line react-hooks/exhaustive-deps
103
+ }, []);
104
+ return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, feedIDs.map(function(feedID) {
105
+ var container = feedContainersRef.current.get(feedID);
106
+ if (!container) {
107
+ return null;
108
+ }
109
+ return /*#__PURE__*/ _react.default.createElement(_IndexContext.IndexContext.Provider, {
110
+ key: feedID,
111
+ value: container
112
+ }, renderFeed({
113
+ feedID: feedID
114
+ }));
115
+ }));
116
+ }
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "useFeeds", {
7
+ enumerable: true,
8
+ get: function() {
9
+ return useFeeds;
10
+ }
11
+ });
12
+ var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
13
+ var _connectFeeds = /*#__PURE__*/ _interop_require_default._(require("instantsearch.js/cjs/connectors/feeds/connectFeeds"));
14
+ var _useConnector = require("../hooks/useConnector");
15
+ function useFeeds(props, additionalWidgetProperties) {
16
+ return (0, _useConnector.useConnector)(_connectFeeds.default, props, additionalWidgetProperties);
17
+ }
package/dist/cjs/index.js CHANGED
@@ -14,6 +14,7 @@ var _interop_require_default = require("@swc/helpers/_/_interop_require_default"
14
14
  var _version = /*#__PURE__*/ _interop_require_default._(require("./version"));
15
15
  _export_star._(require("./components/Configure"), exports);
16
16
  _export_star._(require("./components/DynamicWidgets"), exports);
17
+ _export_star._(require("./components/Feeds"), exports);
17
18
  _export_star._(require("./components/Index"), exports);
18
19
  _export_star._(require("./components/InstantSearch"), exports);
19
20
  _export_star._(require("./components/InstantSearchServerContext"), exports);
@@ -25,6 +26,7 @@ _export_star._(require("./connectors/useClearRefinements"), exports);
25
26
  _export_star._(require("./connectors/useConfigure"), exports);
26
27
  _export_star._(require("./connectors/useCurrentRefinements"), exports);
27
28
  _export_star._(require("./connectors/useDynamicWidgets"), exports);
29
+ _export_star._(require("./connectors/useFeeds"), exports);
28
30
  _export_star._(require("./connectors/useFrequentlyBoughtTogether"), exports);
29
31
  _export_star._(require("./connectors/useGeoSearch"), exports);
30
32
  _export_star._(require("./connectors/useHierarchicalMenu"), exports);
@@ -9,6 +9,7 @@ Object.defineProperty(exports, "useWidget", {
9
9
  return useWidget;
10
10
  }
11
11
  });
12
+ var _utils = require("instantsearch.js/cjs/lib/utils");
12
13
  var _react = require("react");
13
14
  var _dequal = require("./dequal");
14
15
  var _use = require("./use");
@@ -99,9 +100,10 @@ function useWidget(param) {
99
100
  if ((waitForResultsRef === null || waitForResultsRef === void 0 ? void 0 : waitForResultsRef.current) && !skipSuspense) {
100
101
  var _search_helper;
101
102
  (0, _use.use)(waitForResultsRef.current);
102
- // If we made a second request because of DynamicWidgets, we need to wait for the second result,
103
- // except for DynamicWidgets itself which needs to render its children after the first result.
104
- if (widget.$$type !== 'ais.dynamicWidgets' && ((_search_helper = search.helper) === null || _search_helper === void 0 ? void 0 : _search_helper.lastResults)) {
103
+ // If we made a second request because of two-pass widgets, we need to
104
+ // wait for the second result except for the two-pass widgets themselves
105
+ // which need to render their children after the first result.
106
+ if (!(0, _utils.isTwoPassWidget)(widget) && ((_search_helper = search.helper) === null || _search_helper === void 0 ? void 0 : _search_helper.lastResults)) {
105
107
  (0, _use.use)(waitForResultsRef.current);
106
108
  }
107
109
  }
@@ -40,12 +40,9 @@ function getServerState(children, param) {
40
40
  notifyServer: createNotifyServer()
41
41
  }).then(function(serverState) {
42
42
  var shouldRefetch = false;
43
- // <DynamicWidgets> requires another query to retrieve the dynamic widgets
44
- // to render.
43
+ // Two-pass widgets require another query to discover and mount child widgets.
45
44
  (0, _utils.walkIndex)(searchRef.current.mainIndex, function(index) {
46
- shouldRefetch = shouldRefetch || index.getWidgets().some(function(widget) {
47
- return widget.$$type === 'ais.dynamicWidgets';
48
- });
45
+ shouldRefetch = shouldRefetch || index.getWidgets().some(_utils.isTwoPassWidget);
49
46
  });
50
47
  if (shouldRefetch) {
51
48
  (0, _utils.resetWidgetId)();
@@ -9,4 +9,4 @@ Object.defineProperty(exports, "default", {
9
9
  return _default;
10
10
  }
11
11
  });
12
- var _default = '7.31.0';
12
+ var _default = '7.32.0';
@@ -0,0 +1,9 @@
1
+ import React from 'react';
2
+ import type { FeedsConnectorParams } from 'instantsearch.js/es/connectors/feeds/connectFeeds';
3
+ import type { ReactNode } from 'react';
4
+ export type FeedsProps = FeedsConnectorParams & {
5
+ renderFeed: ({ feedID }: {
6
+ feedID: string;
7
+ }) => ReactNode;
8
+ };
9
+ export declare function Feeds({ renderFeed, ...props }: FeedsProps): React.JSX.Element;
@@ -0,0 +1,107 @@
1
+ import { _ } from '@swc/helpers/esm/_object_without_properties.js';
2
+ import { _ as _$1 } from '@swc/helpers/esm/_sliced_to_array.js';
3
+ import { _ as _$2 } from '@swc/helpers/esm/_to_consumable_array.js';
4
+ import { createFeedContainer } from 'instantsearch.js/es/connectors/feeds/FeedContainer.js';
5
+ import React__default, { useRef, useEffect } from 'react';
6
+ import { useFeeds } from '../connectors/useFeeds.js';
7
+ import { IndexContext } from '../lib/IndexContext.js';
8
+ import { useIndexContext } from '../lib/useIndexContext.js';
9
+ import { useInstantSearchContext } from '../lib/useInstantSearchContext.js';
10
+
11
+ function Feeds(_0) {
12
+ var renderFeed = _0.renderFeed, props = _(_0, [
13
+ "renderFeed"
14
+ ]);
15
+ var feedIDs = useFeeds(props, {
16
+ $$widgetType: 'ais.feeds'
17
+ }).feedIDs;
18
+ var parentIndex = useIndexContext();
19
+ var instantSearchInstance = useInstantSearchContext();
20
+ var feedContainersRef = useRef(new Map());
21
+ var removalTimerRef = useRef(null);
22
+ var pendingRemovalsRef = useRef(new Map());
23
+ // Create and register new FeedContainers synchronously so SSR and the first
24
+ // client render can provide the matching feed index context to children.
25
+ var toAdd = [];
26
+ feedIDs.forEach(function(feedID) {
27
+ if (!feedContainersRef.current.has(feedID)) {
28
+ var pendingContainer = pendingRemovalsRef.current.get(feedID);
29
+ if (pendingContainer) {
30
+ pendingRemovalsRef.current.delete(feedID);
31
+ feedContainersRef.current.set(feedID, pendingContainer);
32
+ return;
33
+ }
34
+ var container = createFeedContainer(feedID, parentIndex, instantSearchInstance);
35
+ feedContainersRef.current.set(feedID, container);
36
+ toAdd.push(container);
37
+ }
38
+ });
39
+ if (toAdd.length > 0) {
40
+ parentIndex.addWidgets(toAdd);
41
+ }
42
+ // Remove containers that are no longer in feedIDs (deferred to match useWidget pattern).
43
+ useEffect(function() {
44
+ var containers = feedContainersRef.current;
45
+ var activeSet = new Set(feedIDs);
46
+ var toRemove = [];
47
+ containers.forEach(function(container, id) {
48
+ if (!activeSet.has(id)) {
49
+ toRemove.push([
50
+ id,
51
+ container
52
+ ]);
53
+ containers.delete(id);
54
+ }
55
+ });
56
+ if (toRemove.length > 0) {
57
+ toRemove.forEach(function(param) {
58
+ var _param = _$1(param, 2), id = _param[0], container = _param[1];
59
+ pendingRemovalsRef.current.set(id, container);
60
+ });
61
+ if (removalTimerRef.current !== null) {
62
+ clearTimeout(removalTimerRef.current);
63
+ }
64
+ removalTimerRef.current = setTimeout(function() {
65
+ var widgetsToRemove = Array.from(pendingRemovalsRef.current.values());
66
+ pendingRemovalsRef.current.clear();
67
+ removalTimerRef.current = null;
68
+ if (widgetsToRemove.length > 0) {
69
+ parentIndex.removeWidgets(widgetsToRemove);
70
+ }
71
+ }, 0);
72
+ }
73
+ }, [
74
+ feedIDs,
75
+ parentIndex
76
+ ]);
77
+ useEffect(function() {
78
+ return function() {
79
+ if (removalTimerRef.current !== null) {
80
+ clearTimeout(removalTimerRef.current);
81
+ removalTimerRef.current = null;
82
+ }
83
+ var containers = feedContainersRef.current;
84
+ var toRemove = Array.from(new Set(_$2(containers.values()).concat(_$2(pendingRemovalsRef.current.values()))));
85
+ pendingRemovalsRef.current.clear();
86
+ containers.clear();
87
+ if (toRemove.length > 0) {
88
+ parentIndex.removeWidgets(toRemove);
89
+ }
90
+ };
91
+ // eslint-disable-next-line react-hooks/exhaustive-deps
92
+ }, []);
93
+ return /*#__PURE__*/ React__default.createElement(React__default.Fragment, null, feedIDs.map(function(feedID) {
94
+ var container = feedContainersRef.current.get(feedID);
95
+ if (!container) {
96
+ return null;
97
+ }
98
+ return /*#__PURE__*/ React__default.createElement(IndexContext.Provider, {
99
+ key: feedID,
100
+ value: container
101
+ }, renderFeed({
102
+ feedID: feedID
103
+ }));
104
+ }));
105
+ }
106
+
107
+ export { Feeds };
@@ -0,0 +1,4 @@
1
+ import type { AdditionalWidgetProperties } from '../hooks/useConnector';
2
+ import type { FeedsConnectorParams } from 'instantsearch.js/es/connectors/feeds/connectFeeds';
3
+ export type UseFeedsProps = FeedsConnectorParams;
4
+ export declare function useFeeds(props: UseFeedsProps, additionalWidgetProperties?: AdditionalWidgetProperties): import("instantsearch.js/es/connectors/feeds/connectFeeds").FeedsRenderState;
@@ -0,0 +1,8 @@
1
+ import connectFeeds from 'instantsearch.js/es/connectors/feeds/connectFeeds.js';
2
+ import { useConnector } from '../hooks/useConnector.js';
3
+
4
+ function useFeeds(props, additionalWidgetProperties) {
5
+ return useConnector(connectFeeds, props, additionalWidgetProperties);
6
+ }
7
+
8
+ export { useFeeds };
@@ -1,6 +1,7 @@
1
1
  export { default as version } from './version';
2
2
  export * from './components/Configure';
3
3
  export * from './components/DynamicWidgets';
4
+ export * from './components/Feeds';
4
5
  export * from './components/Index';
5
6
  export * from './components/InstantSearch';
6
7
  export * from './components/InstantSearchServerContext';
@@ -12,6 +13,7 @@ export * from './connectors/useClearRefinements';
12
13
  export * from './connectors/useConfigure';
13
14
  export * from './connectors/useCurrentRefinements';
14
15
  export * from './connectors/useDynamicWidgets';
16
+ export * from './connectors/useFeeds';
15
17
  export * from './connectors/useFrequentlyBoughtTogether';
16
18
  export * from './connectors/useGeoSearch';
17
19
  export * from './connectors/useHierarchicalMenu';
package/dist/es/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export { default as version } from './version.js';
2
2
  export { Configure } from './components/Configure.js';
3
3
  export { DynamicWidgets } from './components/DynamicWidgets.js';
4
+ export { Feeds } from './components/Feeds.js';
4
5
  export { Index } from './components/Index.js';
5
6
  export { InstantSearch } from './components/InstantSearch.js';
6
7
  export { InstantSearchServerContext } from './components/InstantSearchServerContext.js';
@@ -12,6 +13,7 @@ export { useClearRefinements } from './connectors/useClearRefinements.js';
12
13
  export { useConfigure } from './connectors/useConfigure.js';
13
14
  export { useCurrentRefinements } from './connectors/useCurrentRefinements.js';
14
15
  export { useDynamicWidgets } from './connectors/useDynamicWidgets.js';
16
+ export { useFeeds } from './connectors/useFeeds.js';
15
17
  export { useFrequentlyBoughtTogether } from './connectors/useFrequentlyBoughtTogether.js';
16
18
  export { useGeoSearch } from './connectors/useGeoSearch.js';
17
19
  export { useHierarchicalMenu } from './connectors/useHierarchicalMenu.js';
@@ -1,3 +1,4 @@
1
+ import { isTwoPassWidget } from 'instantsearch.js/es/lib/utils/index.js';
1
2
  import { useRef, useEffect } from 'react';
2
3
  import { dequal } from './dequal.js';
3
4
  import { use } from './use.js';
@@ -88,9 +89,10 @@ function useWidget(param) {
88
89
  if ((waitForResultsRef === null || waitForResultsRef === void 0 ? void 0 : waitForResultsRef.current) && !skipSuspense) {
89
90
  var _search_helper;
90
91
  use(waitForResultsRef.current);
91
- // If we made a second request because of DynamicWidgets, we need to wait for the second result,
92
- // except for DynamicWidgets itself which needs to render its children after the first result.
93
- if (widget.$$type !== 'ais.dynamicWidgets' && ((_search_helper = search.helper) === null || _search_helper === void 0 ? void 0 : _search_helper.lastResults)) {
92
+ // If we made a second request because of two-pass widgets, we need to
93
+ // wait for the second result except for the two-pass widgets themselves
94
+ // which need to render their children after the first result.
95
+ if (!isTwoPassWidget(widget) && ((_search_helper = search.helper) === null || _search_helper === void 0 ? void 0 : _search_helper.lastResults)) {
94
96
  use(waitForResultsRef.current);
95
97
  }
96
98
  }
@@ -1,5 +1,5 @@
1
1
  import { waitForResults, getInitialResults } from 'instantsearch.js/es/lib/server.js';
2
- import { resetWidgetId, walkIndex } from 'instantsearch.js/es/lib/utils/index.js';
2
+ import { resetWidgetId, walkIndex, isTwoPassWidget } from 'instantsearch.js/es/lib/utils/index.js';
3
3
  import React__default from 'react';
4
4
  import { InstantSearchServerContext } from '../components/InstantSearchServerContext.js';
5
5
  import { InstantSearchSSRProvider } from '../components/InstantSearchSSRProvider.js';
@@ -31,12 +31,9 @@ import { InstantSearchSSRProvider } from '../components/InstantSearchSSRProvider
31
31
  notifyServer: createNotifyServer()
32
32
  }).then(function(serverState) {
33
33
  var shouldRefetch = false;
34
- // <DynamicWidgets> requires another query to retrieve the dynamic widgets
35
- // to render.
34
+ // Two-pass widgets require another query to discover and mount child widgets.
36
35
  walkIndex(searchRef.current.mainIndex, function(index) {
37
- shouldRefetch = shouldRefetch || index.getWidgets().some(function(widget) {
38
- return widget.$$type === 'ais.dynamicWidgets';
39
- });
36
+ shouldRefetch = shouldRefetch || index.getWidgets().some(isTwoPassWidget);
40
37
  });
41
38
  if (shouldRefetch) {
42
39
  resetWidgetId();
@@ -1,2 +1,2 @@
1
- declare const _default: "7.31.0";
1
+ declare const _default: "7.32.0";
2
2
  export default _default;
@@ -1,3 +1,3 @@
1
- var version = '7.31.0';
1
+ var version = '7.32.0';
2
2
 
3
3
  export { version as default };