{"version":3,"file":"lazy.mjs","names":[],"sources":["../src/lazy/components/lazy-observer.tsx","../src/lazy/deprecated.ts"],"sourcesContent":["import type { InferLazy, Lazy, LazyArray } from \"../lazy\";\nimport { observer } from \"mobx-react-lite\";\nimport type React from \"react\";\nimport { useSlowLoading, type SlowLoadingOptions } from \"../../util/use-slow-loading\";\n\nfunction ThrowError({ error }: { error: unknown }): never {\n  throw error;\n}\n\ntype LO = Lazy | LazyArray;\n\ntype ObserveTuple<O extends LO[]> = {\n  [K in keyof O]: InferLazy<O[K]>;\n};\n\nexport interface LazyObserverBaseProps {\n  placeholder?: React.ReactNode;\n  /**\n   * Hold the `placeholder` back until the wait is long enough to be worth showing, and then keep it\n   * up long enough to read — see `useSlowLoading`. On by default, so a fast load renders no\n   * placeholder at all rather than flashing one.\n   *\n   * The clock runs off the *combined* gate, not per-lazy: it starts when the first value is missing\n   * and resets once they are all present.\n   *\n   * Pass `false` to render the placeholder the instant anything is missing, or an object to override\n   * the timings — a dashboard tile and a full-page route need not agree.\n   */\n  sustain?: boolean | SlowLoadingOptions;\n}\n\nexport interface LazyObserverTupleProps<O extends LO[]> extends LazyObserverBaseProps {\n  observe: [...O];\n  children: (...value: ObserveTuple<O>) => React.ReactNode;\n}\n\nexport interface LazyObserverSingleProps<O extends LO> extends LazyObserverBaseProps {\n  observe: O;\n  children: (value: InferLazy<O>) => React.ReactNode;\n}\n\nconst NEVER: SlowLoadingOptions = { after: 0, minDuration: 0 };\n\nconst LazyObserverImpl = observer(function LazyObserverImpl(props: {\n  observe: LO | LO[];\n  placeholder?: React.ReactNode;\n  sustain?: boolean | SlowLoadingOptions;\n  children: (...values: any[]) => React.ReactNode;\n}) {\n  const { observe, placeholder, sustain, children } = props;\n  const lazies = [observe].flat() as LO[];\n\n  // Reading `loaded` is also what registers observation, so this gate is what drives the loads.\n  const pending = !lazies.every((o) => o.loaded);\n\n  // A failure only reaches the boundary when there is nothing to render. A refresh that fails while\n  // a value is on screen keeps that screen: throwing there would destroy working data over a\n  // background request, and the error stays readable on the lazy for anyone who wants to surface it.\n  const fatal = lazies.find((o) => o.error !== undefined && !o.loaded);\n\n  const showPlaceholder = useSlowLoading(\n    pending,\n    sustain === false ? NEVER : sustain === true || sustain === undefined ? undefined : sustain,\n  );\n\n  if (fatal) return <ThrowError error={fatal.error} />;\n  // The placeholder outranks the children, not just the pending state: once it is up it stays for\n  // its minimum duration even though the value has already landed. Swapping to content the instant\n  // the data arrives is the flash this exists to prevent.\n  if (showPlaceholder) return placeholder;\n  // Pending, but not for long enough to be worth mentioning — render nothing rather than flash.\n  if (pending) return null;\n  return children(...lazies.map((o) => o.value));\n});\n\n/**\n * Renders `children` once every observed lazy holds a value, a `placeholder` while they don't, and\n * re-throws a failure that leaves nothing to render so an error boundary can take over.\n *\n * Two behaviours worth knowing, both aimed at not destroying a screen that is working:\n *\n * - The gate is `loaded`, not `fetching`. A reload that keeps its value renders `children`\n *   throughout, so a refresh never blanks the page.\n * - Only a failure with **nothing loaded** is thrown. A failed refresh keeps rendering the value it\n *   still has.\n */\nexport const LazyObserver = LazyObserverImpl as {\n  <O extends LO[]>(props: LazyObserverTupleProps<O>): React.ReactNode;\n  <O extends Lazy>(props: LazyObserverSingleProps<O>): React.ReactNode;\n};\n","/**\n * The pre-rename spellings, kept so existing code keeps compiling.\n *\n * `lazyObservable` said \"observable\" twice — once in the name and once in the fact that everything\n * in this library is one — and the extra ten characters showed up at every call site. The short\n * forms are the official names now; these are here only so a codebase can migrate a file at a time.\n *\n * **Delete this file at 1.0**, along with its line in `index.ts`. Nothing else imports it: the\n * library itself uses the short names throughout, so removal is the file and the export, and every\n * remaining consumer of an old name breaks loudly at compile time rather than silently.\n */\nimport {\n  lazy,\n  lazyArray,\n  type InferLazy,\n  type Lazy,\n  type LazyApi,\n  type LazyArray,\n  type LazyArrayOptions,\n  type LazyOptions,\n  type LazyOptionsWithInitialValue,\n  type LoadedLazy,\n  type LoadedLazyArray,\n} from \"./lazy\";\n\n/** @deprecated Renamed to `lazy`. Removed at 1.0. */\nexport const lazyObservable = lazy;\n\n/** @deprecated Renamed to `lazyArray`. Removed at 1.0. */\nexport const lazyObservableArray = lazyArray;\n\n/** @deprecated Renamed to `Lazy`. Removed at 1.0. */\nexport type LazyObservable<T = any> = Lazy<T>;\n\n/** @deprecated Renamed to `LazyArray`. Removed at 1.0. */\nexport type LazyObservableArray<T = any> = LazyArray<T>;\n\n/** @deprecated Renamed to `LazyApi`. Removed at 1.0. */\nexport type LazyObservableApi<T> = LazyApi<T>;\n\n/** @deprecated Renamed to `LazyOptions`. Removed at 1.0. */\nexport type LazyObservableOptions = LazyOptions;\n\n/** @deprecated Renamed to `LazyOptionsWithInitialValue`. Removed at 1.0. */\nexport type LazyObservableOptionsWithInitialValue<T> = LazyOptionsWithInitialValue<T>;\n\n/** @deprecated Renamed to `LazyArrayOptions`. Removed at 1.0. */\nexport type LazyObservableArrayOptions<T> = LazyArrayOptions<T>;\n\n/** @deprecated Renamed to `LoadedLazy`. Removed at 1.0. */\nexport type LoadedLazyObservable<T = any> = LoadedLazy<T>;\n\n/** @deprecated Renamed to `LoadedLazyArray`. Removed at 1.0. */\nexport type LoadedLazyObservableArray<T = any> = LoadedLazyArray<T>;\n\n/** @deprecated Renamed to `InferLazy`. Removed at 1.0. */\nexport type InferLazyObservable<O> = InferLazy<O>;\n"],"mappings":";;;;;;AAKA,SAAS,WAAW,EAAE,SAAoC;CACxD,MAAM;AACR;AAkCA,MAAM,QAA4B;CAAE,OAAO;CAAG,aAAa;AAAE;AAE7D,MAAM,mBAAmB,SAAS,SAAS,iBAAiB,OAKzD;CACD,MAAM,EAAE,SAAS,aAAa,SAAS,aAAa;CACpD,MAAM,SAAS,CAAC,OAAO,CAAC,CAAC,KAAK;CAG9B,MAAM,UAAU,CAAC,OAAO,OAAO,MAAM,EAAE,MAAM;CAK7C,MAAM,QAAQ,OAAO,MAAM,MAAM,EAAE,UAAU,UAAa,CAAC,EAAE,MAAM;CAEnE,MAAM,kBAAkB,eACtB,SACA,YAAY,QAAQ,QAAQ,YAAY,QAAQ,YAAY,SAAY,SAAY,OACtF;CAEA,IAAI,OAAO,OAAO,oBAAC,YAAD,EAAY,OAAO,MAAM,MAAQ;CAInD,IAAI,iBAAiB,OAAO;CAE5B,IAAI,SAAS,OAAO;CACpB,OAAO,SAAS,GAAG,OAAO,KAAK,MAAM,EAAE,KAAK,CAAC;AAC/C,CAAC;;;;;;;;;;;;AAaD,MAAa,eAAe;;;;;;;;;;;;;;;;AC5D5B,MAAa,iBAAiB;;AAG9B,MAAa,sBAAsB"}