import { type ComponentConstructor, type ComponentInstance } from "./Component.js"; import { type Constructor } from "./helpers.js"; /** * Retrieve properties declarations of a Component. */ type PropertiesOf = { [P in keyof T] : Property }; /** * Retrieve properties declarations of a Component. */ type ObserversOf = { [P in keyof T] : PropertyObserver[] }; /** * The observer signature for properties. * * @param oldValue The previous value of the property. * @param newValue The current value of the property. */ export type PropertyObserver = (oldValue: T | undefined, newValue: T, propertyKey: string) => void; export type TypeConstructor = T extends number ? NumberConstructor : T extends string ? StringConstructor : T extends boolean ? BooleanConstructor : T extends any[] ? ArrayConstructor : T extends symbol ? SymbolConstructor : T extends bigint ? BigIntConstructor : T extends Date ? DateConstructor : T extends RegExp ? RegExpConstructor : T extends object ? ObjectConstructor : Constructor; /** * A state property declaration. */ export type PropertyDeclaration = PropertyDescriptor & { /** * The property private symbol. */ symbol?: symbol; /** * Flag state properties. */ state?: boolean; /** * The property is bound to an attribute. Also specifies the attribute name if different from the property. */ attribute?: boolean | string; /** * The event to fire on property change. */ event?: boolean | string; /** * Property change should trigger component update. */ update?: boolean; /** * Convert attribute to property value. * * @param value The attributue value. * @returns The property value. */ fromAttribute?: (value: string | null) => T | undefined; /** * Convert property to attribute value. * @param value The property value. * @returns The attributue value. */ toAttribute?: (value: T) => string | null | undefined; /** * The initial value of the property. */ defaultValue?: T; /** * A list of valid property values prototypes. */ type?: TypeConstructor | TypeConstructor[]; /** * Define a property observable. */ observe?: PropertyObserver; /** * A list of field observables. */ observers?: PropertyObserver[]; /** * A custom validation function for the property. * Property assignement throws when this function returns falsy values. */ validate?: (value: unknown) => boolean; /** * Native custom getter for the property. */ get?: PropertyDescriptor["get"]; /** * Native custom setter for the property. */ set?: PropertyDescriptor["set"]; /** * Define custom getter for the property. * @param value The current property value. */ getter?: (value?: T) => ReturnType>; /** * Define a custom setter for the property. * It runs before property validations. * The returned value will be set to the property. * @param newValue The value to set. */ setter?: (newValue?: Parameters>[0]) => T; /** * The initializer function. */ initializer?: () => T; }; /** * Property configuration for properties accessor. */ export type PropertyConfig = PropertyDeclaration | TypeConstructor | TypeConstructor[]; /** * A property instance. */ export type Property< T extends ComponentInstance, P extends keyof T > = PropertyDescriptor & { /** * The property name of the field. */ readonly name: P; /** * The property has been defined using static getter. */ readonly static: boolean; /** * The property private symbol. */ symbol: symbol; /** * Flag state properties. */ state: boolean; /** * The bound attribute name. */ attribute?: string; /** * The event to fire on property change. */ event?: string; /** * Property change should trigger component update. */ update?: boolean; /** * The initial value of the property. */ defaultValue?: T[P]; /** * A list of valid property values prototypes. */ type: Constructor[]; /** * Convert attribute to property value. * * @param value The attributue value. * @returns The property value. */ fromAttribute?: (value: string | null) => T[P]; /** * Convert property to attribute value. * @param value The property value. * @returns The attributue value. */ toAttribute?: (value: T[P]) => string | null | undefined; /** * A custom validation function for the property. * Property assignement throws when this function returns falsy values. */ validate?: (value: unknown) => boolean; /** * Native custom getter for the property. */ get?: PropertyDescriptor["get"]; /** * Native custom setter for the property. */ set?: PropertyDescriptor["set"]; /** * Define custom getter for the property. * @param value The current property value. */ getter?: (value?: T[P]) => ReturnType>; /** * Define a custom setter for the property. * It runs before property validations. * The returned value will be set to the property. * @param newValue The value to set. */ setter?: (newValue?: Parameters>[0]) => T[P]; /** * The initializer function. */ initializer?: () => T[P]; }; /** * Retrieve all properties descriptors. * @param prototype The component prototype. * @returns A list of property descriptors. * @throws If the component has not been finalized. */ export declare const getProperties: (prototype: T) => PropertiesOf; /** * Retrieve property declaration. * @param prototype The component prototype. * @param propertyKey The name of the property. * @param failIfMissing Should throw an exception if the property is not defined. * @returns The property declaration. * @throws If the property is not defined and `failIfMissing` is `true`. */ export declare const getProperty: < T extends ComponentInstance, P extends keyof T >(prototype: T, propertyKey: P, failIfMissing?: boolean) => Property; /** * Define an observed property. * @param prototype The component prototype. * @param propertyKey The name of the property. * @param declaration The property descriptor. * @param symbolKey The symbol to use to store property value. * @param isStatic The property definition is static. * @returns The final descriptor. */ export declare const defineProperty: < T extends ComponentInstance, P extends keyof T >(prototype: T, propertyKey: P, declaration: PropertyDeclaration, symbolKey?: symbol, isStatic?: boolean) => PropertyDescriptor; /** * Get the property bound to the attribute. * @param prototype The prototype of the Component. * @param attributeName The name of the bound attribute. * @returns The property declaration. */ export declare const getPropertyForAttribute: (prototype: T, attributeName: string) => PropertiesOf[keyof T] | null; /** * Reflect property value to attribute. * * @param element The node to update. * @param propertyName The name of the changed property. * @param newValue The new value for the property (undefined if removed). */ export declare const reflectPropertyToAttribute: < T extends ComponentInstance, P extends keyof T >(element: T, propertyName: P, newValue: T[P]) => void; /** * Iterate over static properties declarations. * @param ctr The component constructor. * @yields Tuples of property key and declaration. */ export declare function staticPropertiesDeclarations< T extends ComponentInstance, C extends ComponentConstructor >(ctr: C): Iterable<[keyof T, PropertyDeclaration]>; /** * Iterate over decorated properties declarations. * @param ctr The component constructor. * @yields Tuples of property key and declaration. */ export declare function decoratedPropertiesDeclarations< T extends ComponentInstance, C extends ComponentConstructor >(ctr: C): Iterable<[keyof T, PropertyDeclaration]>; /** * Iterate over all properties declarations. * @param ctr The component constructor. * @yields Tuples of property key and declaration. */ export declare function decoratedObservers< T extends ComponentInstance, C extends ComponentConstructor >(ctr: C): Iterable<[keyof T, PropertyObserver]>; /** * Get component properties observers. * @param prototype The component prototype. * @returns The map of observers. * @throws If the component has not been finalized. */ export declare const getObservers: (prototype: T) => ObserversOf; /** * Define an observer for a property. * @param element The node context. * @param propertyKey The name of the property to watch. * @param observer The observer function to add. */ export declare const defineObserver: < T extends ComponentInstance, P extends keyof T >(element: T, propertyKey: P, observer: PropertyObserver) => void; /** * Add an observer for a property. * @param element The node context. * @param propertyName The name of the property to watch. * @param observer The observer function to add. */ export declare const addObserver: < T extends ComponentInstance, P extends keyof T >(element: T, propertyName: P, observer: PropertyObserver) => void; /** * Remove an observer for a property. * @param element The node context. * @param propertyName The name of the watched property. * @param observer The observer function to remove. */ export declare const removeObserver: < T extends ComponentInstance, P extends keyof T >(element: T, propertyName: P, observer: PropertyObserver) => void; /** * A decorator for property definition. * @param declaration The property declaration. * @returns The decorator initializer. */ export declare function property(declaration?: PropertyDeclaration): any; /** * A decorator for state property definition. * @param declaration The state property declaration. * @returns The decorator initializer. */ export declare function state(declaration?: PropertyDeclaration): any; /** * A decorator for property observer. * * @param propertyKey The property key to observe. * @returns The decorator initializer. */ export declare function observe(propertyKey: string): any; export {};