Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195import type { ErrorInfo, ReactNode } from "react";import { type ErrorBoundaryResource, FLAG_ERROR_BOUNDARY, Instance, UpdateState, WrappedError,} from "./types.ts";import { reconcile, setCurrentInstance } from "./reconcile.ts";import { commit } from "./commit.ts";import type { Host } from "../host-types.ts";import { ERROR_BOUNDARY_TYPE, ROOT_TYPE } from "./symbols.ts";import { DEV } from "./dev.ts";import { getTaskName } from "./debug.ts";
var v8Stack = DEV && new Error().stack?.includes(" at ");var getErrorInfo: ( e: unknown, node: Instance<unknown> | undefined,) => ErrorInfo = DEV ? (e, node) => { let componentStack = ""; let componentStackForConsole = ""; while (node?.__parent) { const task = getTaskName(node.__type, node.__props); componentStackForConsole += (componentStackForConsole ? "\n" : "") + (v8Stack ? " at " : "") + (v8Stack ? // https://github.com/ChromeDevTools/devtools-frontend/blob/da3f2298be13437ad3e661d2b01b6267dc6a92f0/front_end/models/stack_trace/DetailedErrorStackParser.ts#L133 task.replaceAll(" [as ", " \\x5bas ") : task.replaceAll("@", "\\x40")) + (v8Stack ? " (native)" : "@anonymous:0:0"); componentStack += (componentStack ? "\n" : "") + " in " + task; node = node.__parent; } if (typeof e === "object" && (e as Error | undefined)?.stack) { let stack = (e as Error).stack!; let str = e + ""; const stackArray = ( stack.startsWith(str) ? stack.slice(str.length + 1) : ((str = ""), stack) ) .trimEnd() .split("\n"); const callComponentIndex = stackArray.findIndex((e) => e.includes("$callComponent"), ); if (callComponentIndex !== -1) { stackArray.length = callComponentIndex; } (e as Error).stack = [ ...(str ? [str] : []), ...stackArray, ...(componentStackForConsole ? [componentStackForConsole] : []), ].join("\n"); } return { componentStack: componentStack }; } : () => ({});var reportGlobalError: (error: unknown) => void = typeof reportError === "function" ? reportError : (error: unknown) => setTimeout(() => { throw error; });export var queueUpdate = <NodeType>(root: Root<NodeType>) => { if (!root.__updates) { root.__schedule(); root.__updates = true; }};var flushUpdates = <NodeType>(root: Root<NodeType>) => { root.__updates = false; const node = root.__pendingNode; let wipInstance = new Instance<NodeType>( ROOT_TYPE, { children: node }, "", 0, false, ); wipInstance.__hostNode = root.__root; const update = new UpdateState( (wipInstance.__hostParent = wipInstance), (wipInstance.__root = root), ); for (const updater of root.__pendingState) { updater(update); } const prevPendingState = root.__pendingState; root.__pendingState = []; try { reconcile( wipInstance, root.__instance, update, false, false, false, new Set(), ); commit(wipInstance, root.__host, update); if (DEV && root.__pendingState.length) { throw new TypeError("You may not update state during render."); } } catch (e: unknown) { root.__pendingState = prevPendingState; if (e instanceof WrappedError) { let node: Instance<NodeType> | undefined = e.__instance satisfies Instance<unknown> as Instance<NodeType>; e = e.__value; let info = getErrorInfo(e, node as Instance<unknown> | undefined); while ((node = node.__parent)) { if (node.__flags & FLAG_ERROR_BOUNDARY) { const resetCurrentInstance = setCurrentInstance( wipInstance, root.__instance, update.__pendingState, ); try { for (const res of node.__resources!) { if (res.$$typeof === ERROR_BOUNDARY_TYPE) { (res as ErrorBoundaryResource).fn(e, info); return; } } } catch (error) { e = error; } finally { resetCurrentInstance(); } } } } reportGlobalError(e); return; } root.__instance = wipInstance;};export class Root<NodeType> { __host: Host<NodeType>; __root: NodeType; __instance: Instance<NodeType> | undefined; __childIds: number = 1; __pendingState: ((update: UpdateState<NodeType>) => void)[] = []; __pendingNode: ReactNode; /** * is update queued? * prop name chosen to keep our mangled props 1 char */ __updates: boolean = false; constructor(root: NodeType, host: Host<NodeType>) { this.__root = root; this.__host = host; } render(node: ReactNode) { this.__pendingNode = node; queueUpdate(this); } __schedule() { const schedule = this.__schedule; queueMicrotask( () => this.__schedule === schedule && flushUpdates<NodeType>(this), ); } flushSync(cb: () => void): undefined { const prev_schedule = this.__schedule; if (this.__updates) { this.__updates = false; queueUpdate(this); } let errors: unknown[] | undefined; this.__schedule = () => {}; try { cb(); } catch (e) { (errors ||= []).push(e); } while (this.__updates) { try { flushUpdates<NodeType>(this); } catch (e) { (errors ||= []).push(e); } } this.__schedule = prev_schedule; if (errors) { throw errors.length === 1 ? errors[0] : new AggregateError(errors); } }}