Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156import { EffectStage, FLAG_NEEDS_INSERT, getLastHostChild, getPrevHostSibling, Instance, isHostNode, UpdateState,} from "./types.ts";import { DEV } from "./dev.ts";import type { Host } from "../host-types.ts";import { TEXT_TYPE } from "./symbols.ts";
var expectHostNode = <NodeType>( self: Instance<NodeType>, reason: string,): NodeType => { const hostNode = self.__hostNode; if (DEV && hostNode === undefined) { throw new TypeError( "instance was expected to have host node because " + reason + ". this is a bug.", ); } return hostNode!;};
export var commit = <NodeType>( root: Instance<NodeType>, host: Host<NodeType>, update: UpdateState<NodeType>,) => { const collectedEffects: Array< Array<{ __stage: EffectStage; __value: () => void }> > = []; const effectsToCleanUp: Array< Array<{ __stage: EffectStage; __value: () => void }> > = []; for (const deleted of update.__deleted) { if (DEV) { /* console.log( "deleting " + getTaskName(deleted.__type, deleted.__props as any), ); */ } if (isHostNode(deleted)) { if (deleted.__hostNode) { host.removeNode( expectHostNode(deleted.__hostParent!, "child has host node"), deleted.__hostNode, ); } } deleted.__effects && effectsToCleanUp.push( deleted.__effects.map((e) => ({ __stage: e.__stage, __value: e.__cleanup.bind(e, deleted), })), ); } commitInner(root, host, update, collectedEffects);
queueMicrotask(() => [effectsToCleanUp, collectedEffects].map((e) => e .flat() .sort((a, b) => a.__stage - b.__stage) .map((e) => host.callEffect(e.__stage === EffectStage.Async, e.__value), ), ), );};
var noop = () => {};
var commitInner = <NodeType>( self: Instance<NodeType>, host: Host<NodeType>, update: UpdateState<NodeType>, collectedEffects: Array<Array<{ __stage: EffectStage; __value: () => void }>>,) => { const effects = self.__effects; if (effects) { // > useInsertionEffect may run either before or after the DOM has been updated. You shouldn’t rely on the DOM being updated at any particular time. // > - https://react.dev/reference/react/useInsertionEffect // this means i can polyfill class components on top of it collectedEffects.push( effects.map((e): { __stage: EffectStage; __value: () => void } => ({ __stage: e.__stage, __value: /* EffectStage.Insertion = 0 */ e.__stage ? e.__commit.bind(e, self) : // this one needs to be inline (host.callEffect(false, e.__commit.bind(e, self)), noop), })), ); } create(self, host); const updates = update.__updates.get(self); if (updates) { if (self.__type === TEXT_TYPE) { if (updates.value) { host.replaceText( expectHostNode(self, "updates happen after creates"), updates.value.c as string, ); } } else if (typeof self.__type === "string") { const hostNode = expectHostNode(self, "updates happen after creates"); for (const k in updates) { if (k === "ref") { } else { const { c, p } = updates[k]!; host.setProperty(hostNode, k, c, p); } } } } let child = self.__firstChild; while (child) { commitInner(child, host, update, collectedEffects); child = child.__nextSibling; }};
var create = <NodeType>(self: Instance<NodeType>, host: Host<NodeType>) => { if (isHostNode(self)) { const selfHost = expectHostNode(self, "children are being created"); let child: Instance<NodeType> | undefined = getLastHostChild(self); let anchor: Instance<NodeType> | undefined; while (child) { if (child.__flags & FLAG_NEEDS_INSERT) { const hostAnchor = anchor ? expectHostNode( anchor, "it is to the right of a node we are inserting", ) : undefined; if (DEV && !child.__hostNode) { /* console.log( "creating " + getTaskName(child.__type, child.__props as any), ); */ } const hostNode = (child.__hostNode ||= child.__type === TEXT_TYPE ? host.createTextNode("") : host.createElement(child.__type as string)); host.insertNode(selfHost, hostNode, hostAnchor); } anchor = child; child = getPrevHostSibling(child); } }};