import { hook, OptionsTypes } from './preact-options-helper.ts';
hook(OptionsTypes.RENDER, (old, vnode) => {
let component = vnode.__c;
if (component) {
// do stuff with the component instance
} else {
console.log(vnode.props);
}
old(vnode); // invoke the next options hook in the chain
});
Last active
July 25, 2023 12:44
-
-
Save developit/141814ee816badd659ea7dd6185004b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { options } from "preact"; | |
export interface VNode<P = any> extends preact.VNode<P> { | |
/** The component instance for this VNode */ | |
__c: AugmentedComponent; | |
/** The parent VNode */ | |
__?: VNode; | |
/** The DOM node for this VNode */ | |
__e?: Element | Text; | |
/** Props that had Signal values before diffing (used after diffing to subscribe) */ | |
__np?: Record<string, any> | null; | |
} | |
export const enum OptionsTypes { | |
HOOK = "__h", | |
DIFF = "__b", | |
DIFFED = "diffed", | |
RENDER = "__r", | |
CATCH_ERROR = "__e", | |
UNMOUNT = "unmount", | |
} | |
interface OptionsType { | |
[OptionsTypes.HOOK](component: Component, index: number, type: number): void; | |
[OptionsTypes.DIFF](vnode: VNode): void; | |
[OptionsTypes.DIFFED](vnode: VNode): void; | |
[OptionsTypes.RENDER](vnode: VNode): void; | |
[OptionsTypes.CATCH_ERROR](error: any, vnode: VNode, oldVNode: VNode): void; | |
[OptionsTypes.UNMOUNT](vnode: VNode): void; | |
} | |
/** Install a Preact options hook */ | |
export function hook<T extends OptionsTypes>(hookName: T, hookFn: HookFn<T>) { | |
// @ts-ignore-next-line private options hooks usage | |
options[hookName] = hookFn.bind(null, options[hookName] || (() => {})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment