text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.useObservableEagerState = void 0;
  var react_1 = require("react");
  var helpers_1 = require("./helpers");
  /**
   * Optimized for safely getting synchronous values from hot or pure observables
   * without triggering extra initial re-rendering.
   *
   * ⚠ If the observable is cold and with side effects
   * they will be performed at least twice!
   *
   * By default this hook will subscribe to the observable at least twice.
   * The first time is for getting synchronous value to prevent extra initial re-rendering.
   * In concurrent mode this may happen more than one time.
   *
   * @template TState State.
   *
   * @param state$ An observable of state value.
   */
  function useObservableEagerState(state$) {
      var forceUpdate = helpers_1.useForceUpdate();
      var state$Ref = react_1.useRef(state$);
      var errorRef = react_1.useRef();
      var isAsyncEmissionRef = react_1.useRef(false);
      var didSyncEmit = react_1.useRef(false);
      var _a = react_1.useState(function () {
          var state;
          state$
              .subscribe({
              next: function (value) {
                  didSyncEmit.current = true;
                  state = value;
              },
              error: function (error) {
                  errorRef.current = error;
              }
          })
              .unsubscribe();
          return state;
      }), state = _a[0], setState = _a[1];
      // update the latest observable
      // synchronously after render being committed
      helpers_1.useIsomorphicLayoutEffect(function () {
          state$Ref.current = state$;
      });
      react_1.useEffect(function () {
          errorRef.current = null;
          // keep in closure for checking staleness
          var input$ = state$Ref.current;
          var subscription = input$.subscribe({
              next: function (value) {
                  if (input$ !== state$Ref.current) {
                      // stale observable
                      return;
                  }
                  if (isAsyncEmissionRef.current) {
                      // ignore synchronous value
                      // prevent initial re-rendering
                      setState(value);
                  }
              },
              error: function (error) {
                  if (input$ !== state$Ref.current) {
                      // stale observable
                      return;
                  }
                  errorRef.current = error;
                  forceUpdate();
              }
          });
          isAsyncEmissionRef.current = true;
          return function () {
              subscription.unsubscribe();
          };
      }, [state$]);
      if (errorRef.current) {
          // Let error boundary catch the error
          throw errorRef.current;
      }
      if (didSyncEmit.current) {
          react_1.useDebugValue(state);
          return state;
      }
      else {
          throw new Error('Observable did not synchronously emit a value.');
      }
  }
  exports.useObservableEagerState = useObservableEagerState;
  //# sourceMappingURL=use-observable-eager-state.js.map