import type { BooleanAttribute } from "../definitions/boolean-attributes"; import type { CSSClassName, CSSObject, CSSValue } from "./style"; import type { CSSString, EventListenerOrEventListenerObject, EventType, HTMLString } from "./dom"; import { isFuture, ObserverFunction, type Future, type MaybeFuture, type Observable, type Observer, type Subject } from "./futures"; import type { RenderingScheduler } from "./schedulers"; import type { RMLEventName, RMLEventAttributeName } from "./dom"; import type { Sink } from "./sink"; import { ObjectSourceExpression, TargetObject } from "../sources/object-source"; import { isFunction } from "../utils/is-function"; import { isObserverSource } from "../sources/observer-source"; export type ErrorHandler = (e: Error) => void; /** * Data binding configuration for event sources or data sinks * * @remarks Used to describe a binding between a source and a sink when mounting a template * effectively telling Rimmel where to bind what on mount */ export interface BindingConfiguration { type: 'source' | 'sink'; }; /** * An object that specifies how to bind an EventTarget to an event handler. * * Objects of this type are normally created by Source functions or * by the Rimmel parser based on the context */ export interface SourceBindingConfiguration { type: 'source'; listener: EventListenerOrEventListenerObject> | Observer>; options?: AddEventListenerOptions; eventName: RMLEventName; // error?: EventListener; // termination?: EventListener; }; /** * This tells Rimmel how to sink a Promise or an Observable to the DOM. * Objects of this type are normally returned by Sink functions, or chosen * automatically by the Rimmel parser based on the context * ## Examples * * ### Set the value of a textbox from a Promise * * Here when the Promise resolves, the value of the text box will be set * * ```ts * const p = new Promise(resolve => setTimeout(() =>resolve('hello'), 2000)); * const template = rml` * * `; */ export interface SinkBindingConfiguration extends BindingConfiguration { // FIXME: use symbols to avoid potential conflicts with certain objects // TODO: also make use of transferable symbols that can traverse a server|client boundary type: 'sink'; t: string; source: Future; sink: Sink; textNodeOnly?: boolean; scheduler?: RenderingScheduler; params?: any; error?: ErrorHandler; termination?: () => void; }; // export type Empty = MaybeFuture; // export type BindingConfigurationType = T extends SinkBindingConfiguration ? SinkBindingConfiguration : SourceBindingConfiguration; export const isSinkBindingConfiguration = (b: unknown): b is SinkBindingConfiguration => (b as SinkBindingConfiguration)?.type == 'sink'; export const isSourceBindingConfiguration = (b: unknown): b is SourceBindingConfiguration => (b as SourceBindingConfiguration)?.type == 'source'; export const isSourceExpression = (e: unknown): e is RMLTemplateExpressions.SourceExpression => isFunction(e) || isObserverSource(e); /** * An Object's property or an Array's index */ export type property = string | number | symbol; export type QuerySelectorString = string; export type SourceAttributeName = RMLEventAttributeName; export type SinkAttributeName = T extends RMLEventAttributeName ? never : string; export type AttributeName = SourceAttributeName | SinkAttributeName; export type _SinkAttributeValue = boolean | string | number | null | undefined | Function | { [key: string]: _SinkAttributeValue }; export type PresentSinkAttributeValue = _SinkAttributeValue; export const isPresentSinkAttributeValue = (value: any): value is PresentSinkAttributeValue => !isFuture(value) export type FutureSinkAttributeValue = Future<_SinkAttributeValue>; export const isFutureSinkAttributeValue = (value: any): value is FutureSinkAttributeValue => isFuture(value) // TODO: Rename... e.g.: DiachronicAttributeValue? export type SinkAttributeValue = PresentSinkAttributeValue | FutureSinkAttributeValue; export type SourceAttributeValue = Observer | Function; export type AttributeValue = SinkAttributeValue | SourceAttributeValue; export type AttributeObject = { [K in string]: K extends SourceAttributeName ? SourceAttributeValue : SinkAttributeValue; }; export type DOMSubtreeObject = { [K in QuerySelectorString]: AttributeObject; }; export namespace RMLTemplateExpressions { /** * An HTML Subtree */ export type Node = MaybeFuture; /** * The value of an HTML attribute */ export type AttributeValue = MaybeFuture; /** * The value of a boolean HTML attribute */ // export type BooleanAttributeValue = MaybeFuture ? T : never)>; // export type BooleanAttributeValue = MaybeFuture; // export type BooleanAttributeValue = MaybeFuture; // export type BooleanAttributeValue = BooleanAttribute; export type BooleanAttributeValue = MaybeFuture; export type EnumAttributeValue = MaybeFuture; /** * One or more class names */ export type ClassName = MaybeFuture; /** * A Record of class names to set or clear */ export type ClassObject = MaybeFuture>; /** * A simple HTML string or a Future resolving to it */ export type HTMLText = MaybeFuture; export type TextString = MaybeFuture; export type StringLike = MaybeFuture>; export type POJO = ObjectSourceExpression; /** * A CSS declaration "property: value;" string */ export type CSSStyleDeclaration = MaybeFuture; export type CSSStyleValue = MaybeFuture>; export type Mixin = MaybeFuture; export type EventHandler = EventListenerOrEventListenerObject> | Observer>; export type GenericHandler = BindingConfiguration; // Implicit Source export type SourceExpression = Observer | ((t: T) => void) | undefined; export type SinkExpression = Sink; // export type Empty = MaybeFuture; export type TargetEventHandler = | ((data: T) => void) | ObserverFunction | Observer | undefined ; /** * A valid RML template expression which can be: * - a static value, when no data binding is required (e.g.: a string) * - an event handler function * - an event handler object * - an event handler stream (e.g.: an Observer) * - a custom event source * - a data sink **/ export type Any = | Node | AttributeValue | BooleanAttributeValue | ClassName | ClassObject | CSSStyleDeclaration // | CSSStyleValue | Mixin | EventHandler | POJO ; }; export type RMLTemplateExpression = | RMLTemplateExpressions.AttributeValue | RMLTemplateExpressions.BooleanAttributeValue | RMLTemplateExpressions.ClassName | RMLTemplateExpressions.ClassObject | RMLTemplateExpressions.CSSStyleDeclaration // | RMLTemplateExpressions.CSSStyleValue | RMLTemplateExpressions.EventHandler | RMLTemplateExpressions.HTMLText | RMLTemplateExpressions.HTMLText[] | RMLTemplateExpressions.TextString | RMLTemplateExpressions.TextString[] | RMLTemplateExpressions.Mixin | RMLTemplateExpressions.Node | RMLTemplateExpressions.POJO | BindingConfiguration ; /** * A RML template expression * * This includes anything that can be passed in RML templates, such as * - static values (strings, numbers, booleans) * - event handlers (functions, Observers, Subjects, Behaviors) * - CSS class names (strings, objects) * - CSS style declarations (strings, objects) * - HTML text (strings, arrays of strings, Promises, Observables) * - Attribute Mixins (Objects, Promises, Observables) * - Custom sources (e.g. ObjectSourceExpression) * - Custom sinks (e.g. SinkBindingConfiguration) * - and more */ export type Handler = | undefined | number | string | boolean // | ObjectSourceExpression | Record | CSSClassName | Function | Sink | Subject> | Observable | Promise | Observer | BindingConfiguration ; export type Inputs = Record; export type Effects = Record; /** * A Rimmel Component * * A plain function that takes an object of named input parameters and returns a present or future HTML string * @param inputs also referred to as "Props" in other frameworks, a simple object containing all parameters of the Component. Those can be static values, Promises, Observables, Event Handlers, literally anything the Component wants to use **/ export type RimmelComponent = (...inputs: Inputs[]) => HTMLString | Future;