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