import { 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 = ( self: Instance, 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 = ( root: Instance, host: Host, update: UpdateState, ) => { 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 = ( self: Instance, host: Host, update: UpdateState, collectedEffects: Array 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 = (self: Instance, host: Host) => { if (isHostNode(self)) { const selfHost = expectHostNode(self, "children are being created"); let child: Instance | undefined = getLastHostChild(self); let anchor: Instance | 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); } } };