react-instantsearch-core 7.0.3 → 7.2.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.
@@ -7,7 +7,7 @@
7
7
 
8
8
  var React__default = 'default' in React ? React['default'] : React;
9
9
 
10
- var version = '7.0.3';
10
+ var version = '7.2.0';
11
11
 
12
12
  // Copyright Joyent, Inc. and other Node contributors.
13
13
  //
@@ -4327,7 +4327,7 @@
4327
4327
 
4328
4328
  var SearchResults_1 = SearchResults;
4329
4329
 
4330
- var version$1 = '3.14.1';
4330
+ var version$1 = '3.14.2';
4331
4331
 
4332
4332
  var escapeFacetValue$3 = escapeFacetValue_1.escapeFacetValue;
4333
4333
 
@@ -8045,6 +8045,11 @@
8045
8045
  return stableValue;
8046
8046
  }
8047
8047
 
8048
+ var useKey = 'use';
8049
+
8050
+ // @TODO: Remove this file and import directly from React when available.
8051
+ var use = React[useKey];
8052
+
8048
8053
  /**
8049
8054
  * `useLayoutEffect` that doesn't show a warning when server-side rendering.
8050
8055
  *
@@ -8052,11 +8057,19 @@
8052
8057
  */
8053
8058
  var useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
8054
8059
 
8060
+ var InstantSearchRSCContext = /*#__PURE__*/React.createContext(null);
8061
+
8062
+ function useRSCContext() {
8063
+ return React.useContext(InstantSearchRSCContext);
8064
+ }
8065
+
8055
8066
  function useWidget(_ref) {
8067
+ var _waitingForResultsRef;
8056
8068
  var widget = _ref.widget,
8057
8069
  parentIndex = _ref.parentIndex,
8058
8070
  props = _ref.props,
8059
8071
  shouldSsr = _ref.shouldSsr;
8072
+ var waitingForResultsRef = useRSCContext();
8060
8073
  var prevPropsRef = React.useRef(props);
8061
8074
  React.useEffect(function () {
8062
8075
  prevPropsRef.current = props;
@@ -8116,9 +8129,20 @@
8116
8129
  });
8117
8130
  };
8118
8131
  }, [parentIndex, widget, shouldAddWidgetEarly, search, props]);
8119
- if (shouldAddWidgetEarly) {
8132
+ if (shouldAddWidgetEarly || (waitingForResultsRef === null || waitingForResultsRef === void 0 ? void 0 : (_waitingForResultsRef = waitingForResultsRef.current) === null || _waitingForResultsRef === void 0 ? void 0 : _waitingForResultsRef.status) === 'pending') {
8120
8133
  parentIndex.addWidgets([widget]);
8121
8134
  }
8135
+ if (typeof window === 'undefined' && waitingForResultsRef !== null && waitingForResultsRef !== void 0 && waitingForResultsRef.current &&
8136
+ // We need the widgets contained in the index to be added before we trigger the search request.
8137
+ widget.$$type !== 'ais.index') {
8138
+ var _search$helper;
8139
+ use(waitingForResultsRef.current);
8140
+ // If we made a second request because of DynamicWidgets, we need to wait for the second result,
8141
+ // except for DynamicWidgets itself which needs to render its children after the first result.
8142
+ if (widget.$$type !== 'ais.dynamicWidgets' && (_search$helper = search.helper) !== null && _search$helper !== void 0 && _search$helper.lastResults) {
8143
+ use(waitingForResultsRef.current);
8144
+ }
8145
+ }
8122
8146
  }
8123
8147
 
8124
8148
  var _excluded$1 = ["instantSearchInstance", "widgetParams"],
@@ -8799,7 +8823,7 @@
8799
8823
  return widgets.indexOf(widget) === -1;
8800
8824
  });
8801
8825
  if (localInstantSearchInstance && Boolean(widgets.length)) {
8802
- var _nextState = widgets.reduce(function (state, widget) {
8826
+ var cleanedState = widgets.reduce(function (state, widget) {
8803
8827
  // the `dispose` method exists at this point we already assert it
8804
8828
  var next = widget.dispose({
8805
8829
  helper: helper,
@@ -8808,14 +8832,23 @@
8808
8832
  });
8809
8833
  return next || state;
8810
8834
  }, helper.state);
8835
+ var newState = localInstantSearchInstance.future.preserveSharedStateOnUnmount ? getLocalWidgetsSearchParameters(localWidgets, {
8836
+ uiState: localUiState,
8837
+ initialSearchParameters: new algoliasearchHelper_1.SearchParameters({
8838
+ index: this.getIndexName()
8839
+ })
8840
+ }) : getLocalWidgetsSearchParameters(localWidgets, {
8841
+ uiState: getLocalWidgetsUiState(localWidgets, {
8842
+ searchParameters: cleanedState,
8843
+ helper: helper
8844
+ }),
8845
+ initialSearchParameters: cleanedState
8846
+ });
8811
8847
  localUiState = getLocalWidgetsUiState(localWidgets, {
8812
- searchParameters: _nextState,
8848
+ searchParameters: newState,
8813
8849
  helper: helper
8814
8850
  });
8815
- helper.setState(getLocalWidgetsSearchParameters(localWidgets, {
8816
- uiState: localUiState,
8817
- initialSearchParameters: _nextState
8818
- }));
8851
+ helper.setState(newState);
8819
8852
  if (localWidgets.length) {
8820
8853
  localInstantSearchInstance.scheduleSearch();
8821
8854
  }
@@ -8881,7 +8914,7 @@
8881
8914
  return mainHelper.searchForFacetValues(facetName, facetValue, maxFacetHits, state);
8882
8915
  };
8883
8916
  derivedHelper = mainHelper.derive(function () {
8884
- return mergeSearchParameters.apply(void 0, _toConsumableArray$1(resolveSearchParameters(_this3)));
8917
+ return mergeSearchParameters.apply(void 0, [mainHelper.state].concat(_toConsumableArray$1(resolveSearchParameters(_this3))));
8885
8918
  });
8886
8919
  var indexInitialResults = (_instantSearchInstanc = instantSearchInstance._initialResults) === null || _instantSearchInstanc === void 0 ? void 0 : _instantSearchInstanc[this.getIndexId()];
8887
8920
  if (indexInitialResults) {
@@ -9535,7 +9568,7 @@
9535
9568
  },
9536
9569
  started: function started() {
9537
9570
  insightsClient('addAlgoliaAgent', 'insights-middleware');
9538
- helper = instantSearchInstance.helper;
9571
+ helper = instantSearchInstance.mainHelper;
9539
9572
  initialParameters = {
9540
9573
  userToken: helper.state.userToken,
9541
9574
  clickAnalytics: helper.state.clickAnalytics
@@ -11262,7 +11295,7 @@
11262
11295
  };
11263
11296
  }
11264
11297
 
11265
- var version$2 = '4.56.11';
11298
+ var version$2 = '4.58.0';
11266
11299
 
11267
11300
  function _typeof$j(obj) {
11268
11301
  "@babel/helpers - typeof";
@@ -11425,6 +11458,10 @@
11425
11458
  /**
11426
11459
  * Global options for an InstantSearch instance.
11427
11460
  */
11461
+ var INSTANTSEARCH_FUTURE_DEFAULTS = {
11462
+ preserveSharedStateOnUnmount: false
11463
+ };
11464
+
11428
11465
  /**
11429
11466
  * The actual implementation of the InstantSearch. This is
11430
11467
  * created using the `instantsearch` factory function.
@@ -11443,6 +11480,7 @@
11443
11480
  _defineProperty$e(_assertThisInitialized(_this), "indexName", void 0);
11444
11481
  _defineProperty$e(_assertThisInitialized(_this), "insightsClient", void 0);
11445
11482
  _defineProperty$e(_assertThisInitialized(_this), "onStateChange", null);
11483
+ _defineProperty$e(_assertThisInitialized(_this), "future", void 0);
11446
11484
  _defineProperty$e(_assertThisInitialized(_this), "helper", void 0);
11447
11485
  _defineProperty$e(_assertThisInitialized(_this), "mainHelper", void 0);
11448
11486
  _defineProperty$e(_assertThisInitialized(_this), "mainIndex", void 0);
@@ -11515,7 +11553,9 @@
11515
11553
  _options$insightsClie = options.insightsClient,
11516
11554
  insightsClient = _options$insightsClie === void 0 ? null : _options$insightsClie,
11517
11555
  _options$onStateChang = options.onStateChange,
11518
- onStateChange = _options$onStateChang === void 0 ? null : _options$onStateChang;
11556
+ onStateChange = _options$onStateChang === void 0 ? null : _options$onStateChang,
11557
+ _options$future = options.future,
11558
+ future = _options$future === void 0 ? _objectSpread$c(_objectSpread$c({}, INSTANTSEARCH_FUTURE_DEFAULTS), options.future || {}) : _options$future;
11519
11559
  if (searchClient === null) {
11520
11560
  throw new Error(withUsage$3('The `searchClient` option is required.'));
11521
11561
  }
@@ -11529,6 +11569,7 @@
11529
11569
  throw new Error(withUsage$3('The `insightsClient` option should be a function.'));
11530
11570
  }
11531
11571
  _this.client = searchClient;
11572
+ _this.future = future;
11532
11573
  _this.insightsClient = insightsClient;
11533
11574
  _this.indexName = indexName;
11534
11575
  _this.helper = null;
@@ -11988,8 +12029,10 @@
11988
12029
  var forceUpdate = useForceUpdate();
11989
12030
  var serverContext = useInstantSearchServerContext();
11990
12031
  var serverState = useInstantSearchSSRContext();
12032
+ var waitingForResultsRef = useRSCContext();
11991
12033
  var initialResults = serverState === null || serverState === void 0 ? void 0 : serverState.initialResults;
11992
12034
  var prevPropsRef = React.useRef(props);
12035
+ var shouldRenderAtOnce = serverContext || initialResults || waitingForResultsRef;
11993
12036
  var searchRef = React.useRef(null);
11994
12037
  // As we need to render on mount with SSR, using the local ref above in `StrictMode` will
11995
12038
  // create and start two instances of InstantSearch. To avoid this, we instead discard it and use
@@ -12013,7 +12056,7 @@
12013
12056
  }, 0);
12014
12057
  };
12015
12058
  search._schedule.queue = [];
12016
- if (serverContext || initialResults) {
12059
+ if (shouldRenderAtOnce) {
12017
12060
  // InstantSearch.js has a private Initial Results API that lets us inject
12018
12061
  // results on the search instance.
12019
12062
  // On the server, we default the initial results to an empty object so that
@@ -12027,7 +12070,7 @@
12027
12070
  // On the server, we start the search early to compute the search parameters.
12028
12071
  // On SSR, we start the search early to directly catch up with the lifecycle
12029
12072
  // and render.
12030
- if (serverContext || initialResults) {
12073
+ if (shouldRenderAtOnce) {
12031
12074
  search.start();
12032
12075
  }
12033
12076
  if (serverContext) {
@@ -12070,6 +12113,10 @@
12070
12113
  _search._stalledSearchDelay = (_props$stalledSearchD = props.stalledSearchDelay) !== null && _props$stalledSearchD !== void 0 ? _props$stalledSearchD : 200;
12071
12114
  prevPropsRef.current = props;
12072
12115
  }
12116
+ if (!dequal(prevProps.future, props.future)) {
12117
+ _search.future = _objectSpread2(_objectSpread2({}, INSTANTSEARCH_FUTURE_DEFAULTS), props.future);
12118
+ prevPropsRef.current = props;
12119
+ }
12073
12120
 
12074
12121
  // Updating the `routing` prop is not supported because InstantSearch.js
12075
12122
  // doesn't let us change it. This might not be a problem though, because `routing`
@@ -12141,8 +12188,8 @@
12141
12188
  * which is either `nodejs` or `edge`.
12142
12189
  */
12143
12190
  function getNextVersion() {
12144
- var _next, _process$env;
12145
- return typeof window !== 'undefined' && ((_next = window.next) === null || _next === void 0 ? void 0 : _next.version) || (typeof process !== 'undefined' ? (_process$env = process.env) === null || _process$env === void 0 ? void 0 : _process$env.NEXT_RUNTIME : undefined);
12191
+ var _next2, _process$env;
12192
+ return typeof window !== 'undefined' && ((_next2 = window.next) === null || _next2 === void 0 ? void 0 : _next2.version) || (typeof process !== 'undefined' ? (_process$env = process.env) === null || _process$env === void 0 ? void 0 : _process$env.NEXT_RUNTIME : undefined);
12146
12193
  }
12147
12194
 
12148
12195
  var _excluded$6 = ["children"];
@@ -17233,6 +17280,13 @@
17233
17280
  uiState = _useState2[0],
17234
17281
  setLocalUiState = _useState2[1];
17235
17282
  var indexUiState = uiState[indexId];
17283
+ var _useState3 = React.useState(function () {
17284
+ return search.renderState;
17285
+ }),
17286
+ _useState4 = _slicedToArray$3(_useState3, 2),
17287
+ renderState = _useState4[0],
17288
+ setRenderState = _useState4[1];
17289
+ var indexRenderState = renderState[indexId] || {};
17236
17290
  var setUiState = React.useCallback(function (nextUiState) {
17237
17291
  search.setUiState(nextUiState);
17238
17292
  }, [search]);
@@ -17242,6 +17296,7 @@
17242
17296
  React.useEffect(function () {
17243
17297
  function handleRender() {
17244
17298
  setLocalUiState(search.getUiState());
17299
+ setRenderState(search.renderState);
17245
17300
  }
17246
17301
  search.addListener('render', handleRender);
17247
17302
  return function () {
@@ -17252,7 +17307,9 @@
17252
17307
  uiState: uiState,
17253
17308
  setUiState: setUiState,
17254
17309
  indexUiState: indexUiState,
17255
- setIndexUiState: setIndexUiState
17310
+ setIndexUiState: setIndexUiState,
17311
+ renderState: renderState,
17312
+ indexRenderState: indexRenderState
17256
17313
  };
17257
17314
  }
17258
17315
 
@@ -17264,7 +17321,9 @@
17264
17321
  uiState = _useSearchState.uiState,
17265
17322
  setUiState = _useSearchState.setUiState,
17266
17323
  indexUiState = _useSearchState.indexUiState,
17267
- setIndexUiState = _useSearchState.setIndexUiState;
17324
+ setIndexUiState = _useSearchState.setIndexUiState,
17325
+ renderState = _useSearchState.renderState,
17326
+ indexRenderState = _useSearchState.indexRenderState;
17268
17327
  var _useSearchResults = useSearchResults(),
17269
17328
  results = _useSearchResults.results,
17270
17329
  scopedResults = _useSearchResults.scopedResults;
@@ -17297,6 +17356,8 @@
17297
17356
  setUiState: setUiState,
17298
17357
  indexUiState: indexUiState,
17299
17358
  setIndexUiState: setIndexUiState,
17359
+ renderState: renderState,
17360
+ indexRenderState: indexRenderState,
17300
17361
  addMiddlewares: addMiddlewares,
17301
17362
  refresh: refresh,
17302
17363
  status: search.status,
@@ -17304,6 +17365,33 @@
17304
17365
  };
17305
17366
  }
17306
17367
 
17368
+ // This is needed in order to work with RSC Suspense, perhaps they will later provide a wrapper.
17369
+
17370
+ function isStatefulPromise(promise) {
17371
+ return 'status' in promise;
17372
+ }
17373
+ function wrapPromiseWithState(promise) {
17374
+ if (isStatefulPromise(promise)) {
17375
+ return promise;
17376
+ }
17377
+ var pendingPromise = promise;
17378
+ pendingPromise.status = 'pending';
17379
+ pendingPromise.then(function (value) {
17380
+ if (pendingPromise.status === 'pending') {
17381
+ var fulfilledPromise = pendingPromise;
17382
+ fulfilledPromise.status = 'fulfilled';
17383
+ fulfilledPromise.value = value;
17384
+ }
17385
+ }, function (reason) {
17386
+ if (pendingPromise.status === 'pending') {
17387
+ var rejectedPromise = pendingPromise;
17388
+ rejectedPromise.status = 'rejected';
17389
+ rejectedPromise.reason = reason;
17390
+ }
17391
+ });
17392
+ return promise;
17393
+ }
17394
+
17307
17395
  function _typeof$D(obj) {
17308
17396
  "@babel/helpers - typeof";
17309
17397
 
@@ -17501,6 +17589,7 @@
17501
17589
  exports.DynamicWidgets = DynamicWidgets;
17502
17590
  exports.Index = Index;
17503
17591
  exports.InstantSearch = InstantSearch$1;
17592
+ exports.InstantSearchRSCContext = InstantSearchRSCContext;
17504
17593
  exports.InstantSearchSSRProvider = InstantSearchSSRProvider;
17505
17594
  exports.InstantSearchServerContext = InstantSearchServerContext;
17506
17595
  exports.getServerState = getServerState;
@@ -17516,11 +17605,13 @@
17516
17605
  exports.useHitsPerPage = useHitsPerPage;
17517
17606
  exports.useInfiniteHits = useInfiniteHits;
17518
17607
  exports.useInstantSearch = useInstantSearch;
17608
+ exports.useInstantSearchContext = useInstantSearchContext;
17519
17609
  exports.useMenu = useMenu;
17520
17610
  exports.useNumericMenu = useNumericMenu;
17521
17611
  exports.usePagination = usePagination;
17522
17612
  exports.usePoweredBy = usePoweredBy;
17523
17613
  exports.useQueryRules = useQueryRules;
17614
+ exports.useRSCContext = useRSCContext;
17524
17615
  exports.useRange = useRange;
17525
17616
  exports.useRefinementList = useRefinementList;
17526
17617
  exports.useSearchBox = useSearchBox;
@@ -17528,6 +17619,7 @@
17528
17619
  exports.useStats = useStats;
17529
17620
  exports.useToggleRefinement = useToggleRefinement;
17530
17621
  exports.version = version;
17622
+ exports.wrapPromiseWithState = wrapPromiseWithState;
17531
17623
 
17532
17624
  Object.defineProperty(exports, '__esModule', { value: true });
17533
17625