react-instantsearch-core

7.25.07.26.0
dist/cjs/lib/useInstantSearchApi.js
~dist/cjs/lib/useInstantSearchApi.jsModified
+174−197
Index: package/dist/cjs/lib/useInstantSearchApi.js
===================================================================
--- package/dist/cjs/lib/useInstantSearchApi.js
+++ package/dist/cjs/lib/useInstantSearchApi.js
@@ -1,220 +1,197 @@
-"use strict";
+'use strict';
 
-Object.defineProperty(exports, "__esModule", {
-  value: true
-});
-exports.useInstantSearchApi = useInstantSearchApi;
-var _InstantSearch = _interopRequireWildcard(require("instantsearch.js/cjs/lib/InstantSearch"));
-var _react = require("react");
-var _shim = require("use-sync-external-store/shim");
-var _version = _interopRequireDefault(require("../version"));
-var _dequal = require("./dequal");
-var _useForceUpdate = require("./useForceUpdate");
-var _useInstantSearchServerContext = require("./useInstantSearchServerContext");
-var _useInstantSearchSSRContext = require("./useInstantSearchSSRContext");
-var _useRSCContext2 = require("./useRSCContext");
-var _warn = require("./warn");
-function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
-function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
-function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
-function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
-function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
-function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
-function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
-function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
-var defaultUserAgents = ["react (".concat(_react.version, ")"), "react-instantsearch (".concat(_version.default, ")"), "react-instantsearch-core (".concat(_version.default, ")")];
-var serverUserAgent = "react-instantsearch-server (".concat(_version.default, ")");
+var _object_spread = require('@swc/helpers/_/_object_spread');
+var _to_consumable_array = require('@swc/helpers/_/_to_consumable_array');
+var InstantSearch = require('instantsearch.js/cjs/lib/InstantSearch');
+var React = require('react');
+var shim = require('use-sync-external-store/shim');
+var version = require('../version.js');
+var dequal = require('./dequal.js');
+var useForceUpdate = require('./useForceUpdate.js');
+var useInstantSearchServerContext = require('./useInstantSearchServerContext.js');
+var useInstantSearchSSRContext = require('./useInstantSearchSSRContext.js');
+var useRSCContext = require('./useRSCContext.js');
+
+var defaultUserAgents = [
+    "react (".concat(React.version, ")"),
+    "react-instantsearch (".concat(version.default, ")"),
+    "react-instantsearch-core (".concat(version.default, ")")
+];
+var serverUserAgent = "react-instantsearch-server (".concat(version.default, ")");
 var nextUserAgent = function nextUserAgent(nextVersion) {
-  return nextVersion ? "next.js (".concat(nextVersion, ")") : null;
+    return nextVersion ? "next.js (".concat(nextVersion, ")") : null;
 };
 function useInstantSearchApi(props) {
-  var forceUpdate = (0, _useForceUpdate.useForceUpdate)();
-  var serverContext = (0, _useInstantSearchServerContext.useInstantSearchServerContext)();
-  var serverState = (0, _useInstantSearchSSRContext.useInstantSearchSSRContext)();
-  var _useRSCContext = (0, _useRSCContext2.useRSCContext)(),
-    waitForResultsRef = _useRSCContext.waitForResultsRef;
-  var initialResults = serverState === null || serverState === void 0 ? void 0 : serverState.initialResults;
-  var prevPropsRef = (0, _react.useRef)(props);
-  var shouldRenderAtOnce = serverContext || initialResults || waitForResultsRef;
-  var searchRef = (0, _react.useRef)(null);
-  // As we need to render on mount with SSR, using the local ref above in `StrictMode` will
-  // create and start two instances of InstantSearch. To avoid this, we instead discard it and use
-  // an upward ref from `InstantSearchSSRContext` as it has already been mounted a second time at this point.
-  if (serverState !== null && serverState !== void 0 && serverState.ssrSearchRef) {
-    searchRef = serverState.ssrSearchRef;
-  }
-  if (searchRef.current === null) {
-    // We don't use the `instantsearch()` function because it comes with other
-    // top-level APIs that we don't need.
-    // See https://github.com/algolia/instantsearch/blob/5b529f43d8acc680f85837eaaa41f7fd03a3f833/src/index.es.ts#L63-L86
-    var search = new _InstantSearch.default(props);
-    search._schedule = function _schedule(cb) {
-      search._schedule.queue.push(cb);
-      clearTimeout(search._schedule.timer);
-      search._schedule.timer = setTimeout(function () {
-        search._schedule.queue.forEach(function (callback) {
-          callback();
-        });
+    var forceUpdate = useForceUpdate.useForceUpdate();
+    var serverContext = useInstantSearchServerContext.useInstantSearchServerContext();
+    var serverState = useInstantSearchSSRContext.useInstantSearchSSRContext();
+    var waitForResultsRef = useRSCContext.useRSCContext().waitForResultsRef;
+    var initialResults = serverState === null || serverState === void 0 ? void 0 : serverState.initialResults;
+    var prevPropsRef = React.useRef(props);
+    var shouldRenderAtOnce = serverContext || initialResults || waitForResultsRef;
+    var searchRef = React.useRef(null);
+    // As we need to render on mount with SSR, using the local ref above in `StrictMode` will
+    // create and start two instances of InstantSearch. To avoid this, we instead discard it and use
+    // an upward ref from `InstantSearchSSRContext` as it has already been mounted a second time at this point.
+    if (serverState === null || serverState === void 0 ? void 0 : serverState.ssrSearchRef) {
+        searchRef = serverState.ssrSearchRef;
+    }
+    if (searchRef.current === null) {
+        // We don't use the `instantsearch()` function because it comes with other
+        // top-level APIs that we don't need.
+        // See https://github.com/algolia/instantsearch/blob/5b529f43d8acc680f85837eaaa41f7fd03a3f833/src/index.es.ts#L63-L86
+        var search = new InstantSearch(props);
+        search._schedule = function _schedule(cb) {
+            search._schedule.queue.push(cb);
+            clearTimeout(search._schedule.timer);
+            search._schedule.timer = setTimeout(function() {
+                search._schedule.queue.forEach(function(callback) {
+                    callback();
+                });
+                search._schedule.queue = [];
+            }, 0);
+        };
         search._schedule.queue = [];
-      }, 0);
-    };
-    search._schedule.queue = [];
-    if (shouldRenderAtOnce) {
-      // InstantSearch.js has a private Initial Results API that lets us inject
-      // results on the search instance.
-      // On the server, we default the initial results to an empty object so that
-      // InstantSearch.js doesn't schedule a search that isn't used, leading to
-      // an additional network request. (This is equivalent to monkey-patching
-      // `scheduleSearch` to a noop.)
-      search._initialResults = initialResults || {};
-      // We don't rely on the `defer` to reset the schedule search, but will call
-      // `search._resetScheduleSearch()` manually in the effect after children
-      // mount in `InstantSearch`.
-      search._manuallyResetScheduleSearch = true;
+        if (shouldRenderAtOnce) {
+            // InstantSearch.js has a private Initial Results API that lets us inject
+            // results on the search instance.
+            // On the server, we default the initial results to an empty object so that
+            // InstantSearch.js doesn't schedule a search that isn't used, leading to
+            // an additional network request. (This is equivalent to monkey-patching
+            // `scheduleSearch` to a noop.)
+            search._initialResults = initialResults || {};
+            // We don't rely on the `defer` to reset the schedule search, but will call
+            // `search._resetScheduleSearch()` manually in the effect after children
+            // mount in `InstantSearch`.
+            search._manuallyResetScheduleSearch = true;
+        }
+        addAlgoliaAgents(props.searchClient, _to_consumable_array._(defaultUserAgents).concat([
+            serverContext && serverUserAgent,
+            nextUserAgent(getNextVersion())
+        ]));
+        // On the server, we start the search early to compute the search parameters.
+        // On SSR, we start the search early to directly catch up with the lifecycle
+        // and render.
+        if (shouldRenderAtOnce) {
+            search.start();
+        }
+        if (serverContext) {
+            // We notify `getServerState()` of the InstantSearch internals to retrieve
+            // the server state and pass it to the render on SSR.
+            serverContext.notifyServer({
+                search: search
+            });
+        }
+        warnNextRouter(props.routing);
+        searchRef.current = search;
     }
-    addAlgoliaAgents(props.searchClient, [].concat(defaultUserAgents, [serverContext && serverUserAgent, nextUserAgent(getNextVersion())]));
-
-    // On the server, we start the search early to compute the search parameters.
-    // On SSR, we start the search early to directly catch up with the lifecycle
-    // and render.
-    if (shouldRenderAtOnce) {
-      search.start();
-    }
-    if (serverContext) {
-      // We notify `getServerState()` of the InstantSearch internals to retrieve
-      // the server state and pass it to the render on SSR.
-      serverContext.notifyServer({
-        search: search
-      });
-    }
-    warnNextRouter(props.routing);
-    warnNextAppDir(Boolean(waitForResultsRef));
-    searchRef.current = search;
-  }
-  {
-    var _search = searchRef.current;
-    var prevProps = prevPropsRef.current;
-    if (prevProps.indexName !== props.indexName) {
-      _search.helper.setIndex(props.indexName || '').search();
-      prevPropsRef.current = props;
-    }
-    if (prevProps.searchClient !== props.searchClient) {
-      process.env.NODE_ENV === 'development' ? (0, _warn.warn)(false, 'The `searchClient` prop of `<InstantSearch>` changed between renders, which may cause more search requests than necessary. If this is an unwanted behavior, please provide a stable reference: https://www.algolia.com/doc/api-reference/widgets/instantsearch/react/#widget-param-searchclient') : void 0;
-      addAlgoliaAgents(props.searchClient, [].concat(defaultUserAgents, [serverContext && serverUserAgent]));
-      _search.mainHelper.setClient(props.searchClient).search();
-      prevPropsRef.current = props;
-    }
-    if (prevProps.onStateChange !== props.onStateChange) {
-      _search.onStateChange = props.onStateChange;
-      prevPropsRef.current = props;
-    }
-    if (prevProps.searchFunction !== props.searchFunction) {
-      // Updating the `searchFunction` to `undefined` is not supported by
-      // InstantSearch.js, so it will throw an error.
-      // This is a fair behavior until we add an update API in InstantSearch.js.
-      _search._searchFunction = props.searchFunction;
-      prevPropsRef.current = props;
-    }
-    if (prevProps.stalledSearchDelay !== props.stalledSearchDelay) {
-      var _props$stalledSearchD;
-      // The default `stalledSearchDelay` in InstantSearch.js is 200ms.
-      // We need to reset it when it's undefined to get back to the original value.
-      _search._stalledSearchDelay = (_props$stalledSearchD = props.stalledSearchDelay) !== null && _props$stalledSearchD !== void 0 ? _props$stalledSearchD : 200;
-      prevPropsRef.current = props;
-    }
-    if (!(0, _dequal.dequal)(prevProps.future, props.future)) {
-      _search.future = _objectSpread(_objectSpread({}, _InstantSearch.INSTANTSEARCH_FUTURE_DEFAULTS), props.future);
-      prevPropsRef.current = props;
-    }
-
+    {
+        var search1 = searchRef.current;
+        var prevProps = prevPropsRef.current;
+        if (prevProps.indexName !== props.indexName) {
+            search1.helper.setIndex(props.indexName || '').search();
+            prevPropsRef.current = props;
+        }
+        if (prevProps.searchClient !== props.searchClient) {
+            addAlgoliaAgents(props.searchClient, _to_consumable_array._(defaultUserAgents).concat([
+                serverContext && serverUserAgent
+            ]));
+            search1.mainHelper.setClient(props.searchClient).search();
+            prevPropsRef.current = props;
+        }
+        if (prevProps.onStateChange !== props.onStateChange) {
+            search1.onStateChange = props.onStateChange;
+            prevPropsRef.current = props;
+        }
+        if (prevProps.searchFunction !== props.searchFunction) {
+            // Updating the `searchFunction` to `undefined` is not supported by
+            // InstantSearch.js, so it will throw an error.
+            // This is a fair behavior until we add an update API in InstantSearch.js.
+            search1._searchFunction = props.searchFunction;
+            prevPropsRef.current = props;
+        }
+        if (prevProps.stalledSearchDelay !== props.stalledSearchDelay) {
+            var _props_stalledSearchDelay;
+            // The default `stalledSearchDelay` in InstantSearch.js is 200ms.
+            // We need to reset it when it's undefined to get back to the original value.
+            search1._stalledSearchDelay = (_props_stalledSearchDelay = props.stalledSearchDelay) !== null && _props_stalledSearchDelay !== void 0 ? _props_stalledSearchDelay : 200;
+            prevPropsRef.current = props;
+        }
+        if (!dequal.dequal(prevProps.future, props.future)) {
+            search1.future = _object_spread._({}, InstantSearch.INSTANTSEARCH_FUTURE_DEFAULTS, props.future);
+            prevPropsRef.current = props;
+        }
     // Updating the `routing` prop is not supported because InstantSearch.js
     // doesn't let us change it. This might not be a problem though, because `routing`
     // shouldn't need to be dynamic.
     // If we find scenarios where `routing` needs to change, we can always expose
     // it privately on the InstantSearch instance. Another way would be to
     // manually inject the routing middleware in this library, and not rely
     // on the provided `routing` prop.
-  }
-  var cleanupTimerRef = (0, _react.useRef)(null);
-  var store = (0, _shim.useSyncExternalStore)((0, _react.useCallback)(function () {
-    var search = searchRef.current;
-
-    // Scenario 1: the component mounts.
-    if (cleanupTimerRef.current === null) {
-      // On SSR, the instance is already started so we don't start it again.
-      if (!search.started) {
-        search.start();
-        forceUpdate();
-      }
     }
-    // Scenario 2: the component updates.
-    else {
-      // We cancel the previous cleanup function because we don't want to
-      // dispose the search during an update.
-      clearTimeout(cleanupTimerRef.current);
-      search._preventWidgetCleanup = false;
-    }
-    return function () {
-      if (serverState !== null && serverState !== void 0 && serverState.ssrSearchRef) {
-        return;
-      }
-      function cleanup() {
-        search.dispose();
-      }
-      clearTimeout(search._schedule.timer);
-      // We clean up only when the component that uses this subscription unmounts,
-      // but not when it updates, because it would dispose the instance, which
-      // would remove all the widgets and break routing.
-      // Executing the cleanup function in a `setTimeout()` lets us cancel it
-      // in the next effect.
-      // (There might be better ways to do this.)
-      cleanupTimerRef.current = setTimeout(cleanup);
-      // We need to prevent the `useWidget` cleanup function so that widgets
-      // are not removed before the instance is disposed, triggering
-      // an unwanted search request.
-      search._preventWidgetCleanup = true;
-    };
-  }, [forceUpdate, serverState]), function () {
-    return searchRef.current;
-  }, function () {
-    return searchRef.current;
-  });
-  return store;
+    var cleanupTimerRef = React.useRef(null);
+    var store = shim.useSyncExternalStore(React.useCallback(function() {
+        var search = searchRef.current;
+        // Scenario 1: the component mounts.
+        if (cleanupTimerRef.current === null) {
+            // On SSR, the instance is already started so we don't start it again.
+            if (!search.started) {
+                search.start();
+                forceUpdate();
+            }
+        } else {
+            // We cancel the previous cleanup function because we don't want to
+            // dispose the search during an update.
+            clearTimeout(cleanupTimerRef.current);
+            search._preventWidgetCleanup = false;
+        }
+        return function() {
+            if (serverState === null || serverState === void 0 ? void 0 : serverState.ssrSearchRef) {
+                return;
+            }
+            function cleanup() {
+                search.dispose();
+            }
+            clearTimeout(search._schedule.timer);
+            // We clean up only when the component that uses this subscription unmounts,
+            // but not when it updates, because it would dispose the instance, which
+            // would remove all the widgets and break routing.
+            // Executing the cleanup function in a `setTimeout()` lets us cancel it
+            // in the next effect.
+            // (There might be better ways to do this.)
+            cleanupTimerRef.current = setTimeout(cleanup);
+            // We need to prevent the `useWidget` cleanup function so that widgets
+            // are not removed before the instance is disposed, triggering
+            // an unwanted search request.
+            search._preventWidgetCleanup = true;
+        };
+    }, [
+        forceUpdate,
+        serverState
+    ]), function() {
+        return searchRef.current;
+    }, function() {
+        return searchRef.current;
+    });
+    return store;
 }
 function addAlgoliaAgents(searchClient, userAgents) {
-  if (typeof searchClient.addAlgoliaAgent !== 'function') {
-    return;
-  }
-  userAgents.filter(Boolean).forEach(function (userAgent) {
-    searchClient.addAlgoliaAgent(userAgent);
-  });
+    if (typeof searchClient.addAlgoliaAgent !== 'function') {
+        return;
+    }
+    userAgents.filter(Boolean).forEach(function(userAgent) {
+        searchClient.addAlgoliaAgent(userAgent);
+    });
 }
 function warnNextRouter(routing) {
-  if (process.env.NODE_ENV === 'development') {
-    var _routing$router;
-    if (!routing || typeof window === 'undefined' || !('__NEXT_DATA__' in window)) {
-      return;
-    }
-    var isUsingNextRouter =
-    // @ts-expect-error: _isNextRouter is only set on the Next.js router
-    routing !== true && (routing === null || routing === void 0 ? void 0 : (_routing$router = routing.router) === null || _routing$router === void 0 ? void 0 : _routing$router._isNextRouter);
-    process.env.NODE_ENV === 'development' ? (0, _warn.warn)(isUsingNextRouter, "\nYou are using Next.js with InstantSearch without the \"react-instantsearch-router-nextjs\" package.\nThis package is recommended to make the routing work correctly with Next.js.\nPlease check its usage instructions: https://github.com/algolia/instantsearch/tree/master/packages/react-instantsearch-router-nextjs\n\nYou can ignore this warning if you are using a custom router that suits your needs, it won't be outputted in production builds.") : void 0;
-  }
 }
-function warnNextAppDir(isRscContextDefined) {
-  var _next;
-  if (!(process.env.NODE_ENV === 'development') || typeof window === 'undefined' || isRscContextDefined) {
-    return;
-  }
-  process.env.NODE_ENV === 'development' ? (0, _warn.warn)(Boolean((_next = window.next) === null || _next === void 0 ? void 0 : _next.appDir) === false, "\nWe've detected you are using Next.js with the App Router.\nWe released a package called \"react-instantsearch-nextjs\" that makes SSR work with the App Router.\nPlease check its usage instructions: https://www.algolia.com/doc/guides/building-search-ui/going-further/server-side-rendering/react/#with-nextjs-app-router\n\nThis warning will not be outputted in production builds.") : void 0;
-}
-
 /**
  * Gets the version of Next.js if it is available in the `window` object,
  * otherwise it returns the NEXT_RUNTIME environment variable (in SSR),
  * which is either `nodejs` or `edge`.
- */
-function getNextVersion() {
-  var _next2, _process$env;
-  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);
-}
\ No newline at end of file
+ */ function getNextVersion() {
+    var _window_next, _process_env;
+    return typeof window !== 'undefined' && ((_window_next = window.next) === null || _window_next === void 0 ? void 0 : _window_next.version) || (typeof process !== 'undefined' ? (_process_env = process.env) === null || _process_env === void 0 ? void 0 : _process_env.NEXT_RUNTIME : undefined);
+}
+
+exports.useInstantSearchApi = useInstantSearchApi;