import type { Fn , Fn2 } from "@thi.ng/api" ; import type { HDOMImplementation , HDOMOpts } from "@thi.ng/hdom" ; import { DEFAULT_IMPL } from "@thi.ng/hdom/default" ; import { resolveRoot } from "@thi.ng/hdom/resolve" ; import { derefContext } from "@thi.ng/hiccup/deref" ; import type { Event , Signal } from "./api" ; export const mount = < T > ( model , update , view , subscriptions ? , opts = { } , impl = DEFAULT_IMPL ) => { const _opts = { root : "app" , ... opts } ; let prev = [ ] ; const root = resolveRoot ( _opts . root , impl ) ; const render = ( ) => { _opts . ctx = derefContext ( opts . ctx , _opts . autoDerefKeys ) ; const curr = impl . normalizeTree ( _opts , view ( model , signal ) ) ; if ( curr != null ) { if ( _opts . hydrate ) { impl . hydrateTree ( _opts , root , curr ) ; _opts . hydrate = false ; } else { impl . diffTree ( _opts , root , prev , curr ) ; } prev = curr ; } } ; const signal = ( event ) => { return ( ) => { model = update ( event , model ) ; render ( ) ; } ; } ; render ( ) ; subscriptions && subscriptions ( signal ) ; } ;