|
| 1 | +export interface State<T> { |
| 2 | + val: T |
| 3 | + readonly oldVal: T |
| 4 | + readonly rawVal: T |
| 5 | +} |
| 6 | + |
| 7 | +// Defining readonly view of State<T> for covariance. |
| 8 | +// Basically we want StateView<string> to implement StateView<string | number> |
| 9 | +export type StateView<T> = Readonly<State<T>> |
| 10 | + |
| 11 | +export type Primitive = string | number | boolean | bigint |
| 12 | + |
| 13 | +export type PropValue = Primitive | ((e: any) => void) | null |
| 14 | + |
| 15 | +export type Props = Record<string, PropValue | StateView<PropValue> | (() => PropValue)> |
| 16 | + |
| 17 | +interface HasFirstChild {firstChild?: unknown} |
| 18 | + |
| 19 | +type NodeType<ElementType extends HasFirstChild> = |
| 20 | + Omit<ElementType["firstChild"], "after" | "before" | "remove" | "replaceWith"> |
| 21 | + |
| 22 | +export type ValidChildDomValue<ElementType extends HasFirstChild, TextNodeType> = |
| 23 | + Primitive | ElementType | NodeType<ElementType> | TextNodeType | null | undefined |
| 24 | + |
| 25 | +export type BindingFunc<ElementType extends HasFirstChild, TextNodeType> = |
| 26 | + | ((dom?: ElementType | TextNodeType) => ValidChildDomValue<ElementType, TextNodeType>) |
| 27 | + | ((dom?: ElementType) => ElementType) |
| 28 | + |
| 29 | +export type ChildDom<ElementType extends HasFirstChild, TextNodeType> = |
| 30 | + | ValidChildDomValue<ElementType, TextNodeType> |
| 31 | + | StateView<Primitive | null | undefined> |
| 32 | + | BindingFunc<ElementType, TextNodeType> |
| 33 | + | readonly ChildDom<ElementType, TextNodeType>[] |
| 34 | + |
| 35 | +type AddFunc<ElementType extends HasFirstChild, TextNodeType> = |
| 36 | + (dom: ElementType, ...children: readonly ChildDom<ElementType, TextNodeType>[]) => ElementType |
| 37 | + |
| 38 | +export type TagFunc<ElementType extends HasFirstChild, TextNodeType, ResultType = ElementType> = |
| 39 | + (first?: Props | ChildDom<ElementType, TextNodeType>, |
| 40 | + ...rest: readonly ChildDom<ElementType, TextNodeType>[]) => ResultType |
| 41 | + |
| 42 | +type Tags<ElementType extends HasFirstChild, TextNodeType> = |
| 43 | + Readonly<Record<string, TagFunc<ElementType, TextNodeType>>> |
| 44 | + |
| 45 | +// Tags type in browser context, which contains the signatures to tag functions that return |
| 46 | +// specialized DOM elements. |
| 47 | +type BrowserTags = Tags<Element, Text> & { |
| 48 | + [K in keyof HTMLElementTagNameMap]: TagFunc<Element, Text, HTMLElementTagNameMap[K]> |
| 49 | +} |
| 50 | + |
| 51 | +declare function state<T>(): State<T> |
| 52 | +declare function state<T>(initVal: T): State<T> |
| 53 | + |
| 54 | +export interface VanObj<ElementType extends HasFirstChild, TextNodeType> { |
| 55 | + readonly state: typeof state |
| 56 | + readonly derive: <T>(f: () => T) => State<T> |
| 57 | + readonly add: AddFunc<ElementType, TextNodeType> |
| 58 | + readonly tags: Tags<ElementType, TextNodeType> & ((namespaceURI: string) => Tags<ElementType, TextNodeType>) |
| 59 | + |
| 60 | + // Mini-Van specific API |
| 61 | + html: (first?: Props | ChildDom<ElementType, TextNodeType>, |
| 62 | + ...rest: readonly ChildDom<ElementType, TextNodeType>[]) => string |
| 63 | +} |
| 64 | + |
| 65 | +export interface Van extends VanObj<Element, Text> { |
| 66 | + readonly vanWithDoc: <ElementType extends HasFirstChild, TextNodeType>(doc: { |
| 67 | + createElement(s: any): ElementType, |
| 68 | + createTextNode(s: any): TextNodeType, |
| 69 | + }) => VanObj<ElementType, TextNodeType> |
| 70 | + readonly tags: BrowserTags & ((namespaceURI: string) => Tags<Element, Text>) |
| 71 | +} |
| 72 | + |
| 73 | +declare const van: Van |
| 74 | + |
| 75 | +export default van |
0 commit comments