instantsearch.js 4.57.0 → 4.58.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.
package/README.md CHANGED
@@ -20,7 +20,6 @@ InstantSearch.js is a vanilla JavaScript library that lets you create an instant
20
20
  <!-- START doctoc generated TOC please keep comment here to allow auto update -->
21
21
  <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
22
22
 
23
-
24
23
  - [Why](#why)
25
24
  - [Getting started](#getting-started)
26
25
  - [Installation](#installation)
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.default = exports.INSTANTSEARCH_FUTURE_DEFAULTS = void 0;
7
7
  var _events = _interopRequireDefault(require("@algolia/events"));
8
8
  var _algoliasearchHelper = _interopRequireDefault(require("algoliasearch-helper"));
9
9
  var _createInsightsMiddleware = require("../middlewares/createInsightsMiddleware");
@@ -43,15 +43,21 @@ function defaultCreateURL() {
43
43
  /**
44
44
  * Global options for an InstantSearch instance.
45
45
  */
46
+ var INSTANTSEARCH_FUTURE_DEFAULTS = {
47
+ preserveSharedStateOnUnmount: false
48
+ };
49
+
46
50
  /**
47
51
  * The actual implementation of the InstantSearch. This is
48
52
  * created using the `instantsearch` factory function.
49
53
  * It emits the 'render' event every time a search is done
50
54
  */
55
+ exports.INSTANTSEARCH_FUTURE_DEFAULTS = INSTANTSEARCH_FUTURE_DEFAULTS;
51
56
  var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
52
57
  _inherits(InstantSearch, _EventEmitter);
53
58
  var _super = _createSuper(InstantSearch);
54
59
  function InstantSearch(options) {
60
+ var _options$future2;
55
61
  var _this;
56
62
  _classCallCheck(this, InstantSearch);
57
63
  _this = _super.call(this);
@@ -61,6 +67,7 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
61
67
  _defineProperty(_assertThisInitialized(_this), "indexName", void 0);
62
68
  _defineProperty(_assertThisInitialized(_this), "insightsClient", void 0);
63
69
  _defineProperty(_assertThisInitialized(_this), "onStateChange", null);
70
+ _defineProperty(_assertThisInitialized(_this), "future", void 0);
64
71
  _defineProperty(_assertThisInitialized(_this), "helper", void 0);
65
72
  _defineProperty(_assertThisInitialized(_this), "mainHelper", void 0);
66
73
  _defineProperty(_assertThisInitialized(_this), "mainIndex", void 0);
@@ -133,7 +140,9 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
133
140
  _options$insightsClie = options.insightsClient,
134
141
  insightsClient = _options$insightsClie === void 0 ? null : _options$insightsClie,
135
142
  _options$onStateChang = options.onStateChange,
136
- onStateChange = _options$onStateChang === void 0 ? null : _options$onStateChang;
143
+ onStateChange = _options$onStateChang === void 0 ? null : _options$onStateChang,
144
+ _options$future = options.future,
145
+ future = _options$future === void 0 ? _objectSpread(_objectSpread({}, INSTANTSEARCH_FUTURE_DEFAULTS), options.future || {}) : _options$future;
137
146
  if (searchClient === null) {
138
147
  throw new Error(withUsage('The `searchClient` option is required.'));
139
148
  }
@@ -150,7 +159,14 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
150
159
  process.env.NODE_ENV === 'development' ? (0, _utils.warning)(!options.searchParameters, "The `searchParameters` option is deprecated and will not be supported in InstantSearch.js 4.x.\n\nYou can replace it with the `configure` widget:\n\n```\nsearch.addWidgets([\n configure(".concat(JSON.stringify(options.searchParameters, null, 2), ")\n]);\n```\n\nSee ").concat((0, _utils.createDocumentationLink)({
151
160
  name: 'configure'
152
161
  }))) : void 0;
162
+ if (process.env.NODE_ENV === 'development' && ((_options$future2 = options.future) === null || _options$future2 === void 0 ? void 0 : _options$future2.preserveSharedStateOnUnmount) === undefined) {
163
+ // eslint-disable-next-line no-console
164
+ console.info("Starting from the next major version, InstantSearch will change how widgets state is preserved when they are removed. InstantSearch will keep the state of unmounted widgets to be usable by other widgets with the same attribute.\n\nWe recommend setting `future.preserveSharedStateOnUnmount` to true to adopt this change today.\nTo stay with the current behaviour and remove this warning, set the option to false.\n\nSee documentation: ".concat((0, _utils.createDocumentationLink)({
165
+ name: 'instantsearch'
166
+ }), "#widget-param-future\n "));
167
+ }
153
168
  _this.client = searchClient;
169
+ _this.future = future;
154
170
  _this.insightsClient = insightsClient;
155
171
  _this.indexName = indexName;
156
172
  _this.helper = null;
@@ -4,5 +4,5 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _default = '4.57.0';
7
+ var _default = '4.58.0';
8
8
  exports.default = _default;
@@ -141,7 +141,7 @@ function createInsightsMiddleware() {
141
141
  },
142
142
  started: function started() {
143
143
  insightsClient('addAlgoliaAgent', 'insights-middleware');
144
- helper = instantSearchInstance.helper;
144
+ helper = instantSearchInstance.mainHelper;
145
145
  initialParameters = {
146
146
  userToken: helper.state.userToken,
147
147
  clickAnalytics: helper.state.clickAnalytics
@@ -214,7 +214,7 @@ var index = function index(widgetParams) {
214
214
  return widgets.indexOf(widget) === -1;
215
215
  });
216
216
  if (localInstantSearchInstance && Boolean(widgets.length)) {
217
- var _nextState = widgets.reduce(function (state, widget) {
217
+ var cleanedState = widgets.reduce(function (state, widget) {
218
218
  // the `dispose` method exists at this point we already assert it
219
219
  var next = widget.dispose({
220
220
  helper: helper,
@@ -223,14 +223,23 @@ var index = function index(widgetParams) {
223
223
  });
224
224
  return next || state;
225
225
  }, helper.state);
226
+ var newState = localInstantSearchInstance.future.preserveSharedStateOnUnmount ? getLocalWidgetsSearchParameters(localWidgets, {
227
+ uiState: localUiState,
228
+ initialSearchParameters: new _algoliasearchHelper.default.SearchParameters({
229
+ index: this.getIndexName()
230
+ })
231
+ }) : getLocalWidgetsSearchParameters(localWidgets, {
232
+ uiState: getLocalWidgetsUiState(localWidgets, {
233
+ searchParameters: cleanedState,
234
+ helper: helper
235
+ }),
236
+ initialSearchParameters: cleanedState
237
+ });
226
238
  localUiState = getLocalWidgetsUiState(localWidgets, {
227
- searchParameters: _nextState,
239
+ searchParameters: newState,
228
240
  helper: helper
229
241
  });
230
- helper.setState(getLocalWidgetsSearchParameters(localWidgets, {
231
- uiState: localUiState,
232
- initialSearchParameters: _nextState
233
- }));
242
+ helper.setState(newState);
234
243
  if (localWidgets.length) {
235
244
  localInstantSearchInstance.scheduleSearch();
236
245
  }
@@ -296,7 +305,7 @@ var index = function index(widgetParams) {
296
305
  return mainHelper.searchForFacetValues(facetName, facetValue, maxFacetHits, state);
297
306
  };
298
307
  derivedHelper = mainHelper.derive(function () {
299
- return _utils.mergeSearchParameters.apply(void 0, _toConsumableArray((0, _utils.resolveSearchParameters)(_this3)));
308
+ return _utils.mergeSearchParameters.apply(void 0, [mainHelper.state].concat(_toConsumableArray((0, _utils.resolveSearchParameters)(_this3))));
300
309
  });
301
310
  var indexInitialResults = (_instantSearchInstanc = instantSearchInstance._initialResults) === null || _instantSearchInstanc === void 0 ? void 0 : _instantSearchInstanc[this.getIndexId()];
302
311
  if (indexInitialResults) {
@@ -2363,6 +2363,7 @@ declare class InstantSearch<TUiState extends UiState = UiState, TRouteState = TU
2363
2363
  indexName: string;
2364
2364
  insightsClient: InsightsClient | null;
2365
2365
  onStateChange: InstantSearchOptions<TUiState>['onStateChange'] | null;
2366
+ future: NonNullable<InstantSearchOptions<TUiState>['future']>;
2366
2367
  helper: AlgoliaSearchHelper | null;
2367
2368
  mainHelper: AlgoliaSearchHelper | null;
2368
2369
  mainIndex: IndexWidget;
@@ -2604,6 +2605,18 @@ declare type InstantSearchOptions<TUiState extends UiState = UiState, TRouteStat
2604
2605
  * @deprecated This property will be still supported in 4.x releases, but not further. It is replaced by the `insights` middleware. For more information, visit https://www.algolia.com/doc/guides/getting-insights-and-analytics/search-analytics/click-through-and-conversions/how-to/send-click-and-conversion-events-with-instantsearch/js/
2605
2606
  */
2606
2607
  insightsClient?: InsightsClient;
2608
+ future?: {
2609
+ /**
2610
+ * Changes the way `dispose` is used in InstantSearch lifecycle.
2611
+ *
2612
+ * If `false` (by default), each widget unmounting will remove its state as well, even if there are multiple widgets reading that UI State.
2613
+ *
2614
+ * If `true`, each widget unmounting will only remove its own state if it's the last of its type. This allows for dynamically adding and removing widgets without losing their state.
2615
+ *
2616
+ * @default false
2617
+ */
2618
+ preserveSharedStateOnUnmount?: boolean;
2619
+ };
2607
2620
  };
2608
2621
 
2609
2622
  declare type InstantSearchStatus = 'idle' | 'loading' | 'stalled' | 'error';
@@ -1,4 +1,4 @@
1
- /*! InstantSearch.js 4.57.0 | © Algolia, Inc. and contributors; MIT License | https://github.com/algolia/instantsearch */
1
+ /*! InstantSearch.js 4.58.0 | © Algolia, Inc. and contributors; MIT License | https://github.com/algolia/instantsearch */
2
2
  (function (global, factory) {
3
3
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
4
4
  typeof define === 'function' && define.amd ? define(factory) :
@@ -12665,7 +12665,7 @@
12665
12665
  },
12666
12666
  started: function started() {
12667
12667
  insightsClient('addAlgoliaAgent', 'insights-middleware');
12668
- helper = instantSearchInstance.helper;
12668
+ helper = instantSearchInstance.mainHelper;
12669
12669
  initialParameters = {
12670
12670
  userToken: helper.state.userToken,
12671
12671
  clickAnalytics: helper.state.clickAnalytics
@@ -14272,7 +14272,7 @@
14272
14272
  return widgets.indexOf(widget) === -1;
14273
14273
  });
14274
14274
  if (localInstantSearchInstance && Boolean(widgets.length)) {
14275
- var _nextState = widgets.reduce(function (state, widget) {
14275
+ var cleanedState = widgets.reduce(function (state, widget) {
14276
14276
  // the `dispose` method exists at this point we already assert it
14277
14277
  var next = widget.dispose({
14278
14278
  helper: helper,
@@ -14281,14 +14281,23 @@
14281
14281
  });
14282
14282
  return next || state;
14283
14283
  }, helper.state);
14284
+ var newState = localInstantSearchInstance.future.preserveSharedStateOnUnmount ? getLocalWidgetsSearchParameters(localWidgets, {
14285
+ uiState: localUiState,
14286
+ initialSearchParameters: new algoliasearchHelper_1.SearchParameters({
14287
+ index: this.getIndexName()
14288
+ })
14289
+ }) : getLocalWidgetsSearchParameters(localWidgets, {
14290
+ uiState: getLocalWidgetsUiState(localWidgets, {
14291
+ searchParameters: cleanedState,
14292
+ helper: helper
14293
+ }),
14294
+ initialSearchParameters: cleanedState
14295
+ });
14284
14296
  localUiState = getLocalWidgetsUiState(localWidgets, {
14285
- searchParameters: _nextState,
14297
+ searchParameters: newState,
14286
14298
  helper: helper
14287
14299
  });
14288
- helper.setState(getLocalWidgetsSearchParameters(localWidgets, {
14289
- uiState: localUiState,
14290
- initialSearchParameters: _nextState
14291
- }));
14300
+ helper.setState(newState);
14292
14301
  if (localWidgets.length) {
14293
14302
  localInstantSearchInstance.scheduleSearch();
14294
14303
  }
@@ -14354,7 +14363,7 @@
14354
14363
  return mainHelper.searchForFacetValues(facetName, facetValue, maxFacetHits, state);
14355
14364
  };
14356
14365
  derivedHelper = mainHelper.derive(function () {
14357
- return mergeSearchParameters.apply(void 0, _toConsumableArray(resolveSearchParameters(_this3)));
14366
+ return mergeSearchParameters.apply(void 0, [mainHelper.state].concat(_toConsumableArray(resolveSearchParameters(_this3))));
14358
14367
  });
14359
14368
  var indexInitialResults = (_instantSearchInstanc = instantSearchInstance._initialResults) === null || _instantSearchInstanc === void 0 ? void 0 : _instantSearchInstanc[this.getIndexId()];
14360
14369
  if (indexInitialResults) {
@@ -14617,7 +14626,7 @@
14617
14626
  };
14618
14627
  }
14619
14628
 
14620
- var version$1 = '4.57.0';
14629
+ var version$1 = '4.58.0';
14621
14630
 
14622
14631
  var withUsage$r = createDocumentationMessageGenerator({
14623
14632
  name: 'instantsearch'
@@ -14632,6 +14641,10 @@
14632
14641
  /**
14633
14642
  * Global options for an InstantSearch instance.
14634
14643
  */
14644
+ var INSTANTSEARCH_FUTURE_DEFAULTS = {
14645
+ preserveSharedStateOnUnmount: false
14646
+ };
14647
+
14635
14648
  /**
14636
14649
  * The actual implementation of the InstantSearch. This is
14637
14650
  * created using the `instantsearch` factory function.
@@ -14641,6 +14654,7 @@
14641
14654
  _inherits(InstantSearch, _EventEmitter);
14642
14655
  var _super = _createSuper(InstantSearch);
14643
14656
  function InstantSearch(options) {
14657
+ var _options$future2;
14644
14658
  var _this;
14645
14659
  _classCallCheck(this, InstantSearch);
14646
14660
  _this = _super.call(this);
@@ -14650,6 +14664,7 @@
14650
14664
  _defineProperty(_assertThisInitialized(_this), "indexName", void 0);
14651
14665
  _defineProperty(_assertThisInitialized(_this), "insightsClient", void 0);
14652
14666
  _defineProperty(_assertThisInitialized(_this), "onStateChange", null);
14667
+ _defineProperty(_assertThisInitialized(_this), "future", void 0);
14653
14668
  _defineProperty(_assertThisInitialized(_this), "helper", void 0);
14654
14669
  _defineProperty(_assertThisInitialized(_this), "mainHelper", void 0);
14655
14670
  _defineProperty(_assertThisInitialized(_this), "mainIndex", void 0);
@@ -14722,7 +14737,9 @@
14722
14737
  _options$insightsClie = options.insightsClient,
14723
14738
  insightsClient = _options$insightsClie === void 0 ? null : _options$insightsClie,
14724
14739
  _options$onStateChang = options.onStateChange,
14725
- onStateChange = _options$onStateChang === void 0 ? null : _options$onStateChang;
14740
+ onStateChange = _options$onStateChang === void 0 ? null : _options$onStateChang,
14741
+ _options$future = options.future,
14742
+ future = _options$future === void 0 ? _objectSpread2(_objectSpread2({}, INSTANTSEARCH_FUTURE_DEFAULTS), options.future || {}) : _options$future;
14726
14743
  if (searchClient === null) {
14727
14744
  throw new Error(withUsage$r('The `searchClient` option is required.'));
14728
14745
  }
@@ -14739,7 +14756,14 @@
14739
14756
  _warning(!options.searchParameters, "The `searchParameters` option is deprecated and will not be supported in InstantSearch.js 4.x.\n\nYou can replace it with the `configure` widget:\n\n```\nsearch.addWidgets([\n configure(".concat(JSON.stringify(options.searchParameters, null, 2), ")\n]);\n```\n\nSee ").concat(createDocumentationLink({
14740
14757
  name: 'configure'
14741
14758
  }))) ;
14759
+ if ( ((_options$future2 = options.future) === null || _options$future2 === void 0 ? void 0 : _options$future2.preserveSharedStateOnUnmount) === undefined) {
14760
+ // eslint-disable-next-line no-console
14761
+ console.info("Starting from the next major version, InstantSearch will change how widgets state is preserved when they are removed. InstantSearch will keep the state of unmounted widgets to be usable by other widgets with the same attribute.\n\nWe recommend setting `future.preserveSharedStateOnUnmount` to true to adopt this change today.\nTo stay with the current behaviour and remove this warning, set the option to false.\n\nSee documentation: ".concat(createDocumentationLink({
14762
+ name: 'instantsearch'
14763
+ }), "#widget-param-future\n "));
14764
+ }
14742
14765
  _this.client = searchClient;
14766
+ _this.future = future;
14743
14767
  _this.insightsClient = insightsClient;
14744
14768
  _this.indexName = indexName;
14745
14769
  _this.helper = null;
@@ -16110,7 +16134,6 @@
16110
16134
  var _objectWithoutProperties$1 = unwrapExports(objectWithoutProperties);
16111
16135
 
16112
16136
  var _excluded$9 = ["parts", "highlightedTagName", "nonHighlightedTagName", "separator", "className", "classNames"];
16113
- // This is a minimal subset of the actual types from the `JSX` namespace.
16114
16137
 
16115
16138
  function createHighlightPartComponent(_ref) {
16116
16139
  var createElement = _ref.createElement;
@@ -16134,18 +16157,20 @@
16134
16157
  createElement: createElement,
16135
16158
  Fragment: Fragment
16136
16159
  });
16137
- return function Highlight(_ref4) {
16138
- var parts = _ref4.parts,
16139
- _ref4$highlightedTagN = _ref4.highlightedTagName,
16140
- highlightedTagName = _ref4$highlightedTagN === void 0 ? 'mark' : _ref4$highlightedTagN,
16141
- _ref4$nonHighlightedT = _ref4.nonHighlightedTagName,
16142
- nonHighlightedTagName = _ref4$nonHighlightedT === void 0 ? 'span' : _ref4$nonHighlightedT,
16143
- _ref4$separator = _ref4.separator,
16144
- separator = _ref4$separator === void 0 ? ', ' : _ref4$separator,
16145
- className = _ref4.className,
16146
- _ref4$classNames = _ref4.classNames,
16147
- classNames = _ref4$classNames === void 0 ? {} : _ref4$classNames,
16148
- props = _objectWithoutProperties$1(_ref4, _excluded$9);
16160
+ return function Highlight(userProps) {
16161
+ // Not destructured in function signature, to make sure it's not exposed in
16162
+ // the type definition.
16163
+ var parts = userProps.parts,
16164
+ _userProps$highlighte = userProps.highlightedTagName,
16165
+ highlightedTagName = _userProps$highlighte === void 0 ? 'mark' : _userProps$highlighte,
16166
+ _userProps$nonHighlig = userProps.nonHighlightedTagName,
16167
+ nonHighlightedTagName = _userProps$nonHighlig === void 0 ? 'span' : _userProps$nonHighlig,
16168
+ _userProps$separator = userProps.separator,
16169
+ separator = _userProps$separator === void 0 ? ', ' : _userProps$separator,
16170
+ className = userProps.className,
16171
+ _userProps$classNames = userProps.classNames,
16172
+ classNames = _userProps$classNames === void 0 ? {} : _userProps$classNames,
16173
+ props = _objectWithoutProperties$1(userProps, _excluded$9);
16149
16174
 
16150
16175
  return createElement("span", _extends$1({}, props, {
16151
16176
  className: cx(classNames.root, className)