virtual-dom: translateStatic

This commit is contained in:
Brian Carroll 2022-10-30 20:25:14 +00:00
parent 9beaedc536
commit acf4e39980
No known key found for this signature in database
GPG Key ID: 5C7B2EC4101703C0

View File

@ -7,6 +7,7 @@ interface Html.Internal
Handler,
element,
translate,
translateStatic,
insertHandler,
replaceHandler,
dispatchEvent,
@ -183,6 +184,34 @@ translateHandler = \childHandler, parentToChild, childToParent ->
Custom parentFn
translateStatic : Node * -> Node []
translateStatic = \node ->
when node is
Text content ->
Text content
Element name size attrs children ->
newAttrs = List.keepOks attrs keepStaticAttr
newChildren = List.map children translateStatic
Element name size newAttrs newChildren
# TODO: Triggers a stack overflow in the compiler. I think in type checking.
# If someone made this node Lazy, then it's probably expensive, and worth server-side rendering.
# Lazy callback ->
# { node: dynamicNode } = callback (Err NotCached)
# translateStatic dynamicNode
Lazy _ -> None
None -> None
keepStaticAttr : Attribute _ -> Result (Attribute []) {}
keepStaticAttr = \attr ->
when attr is
EventListener _ _ _ -> Err {}
HtmlAttr k v -> Ok (HtmlAttr k v)
DomProp _ _ -> Err {}
Style k v -> Ok (Style k v)
insertHandler : List (Result (Handler state) [NoHandler]), Handler state -> { index : Nat, lookup : List (Result (Handler state) [NoHandler]) }
insertHandler = \lookup, newHandler ->
when List.findFirstIndex lookup Result.isErr is