virtual-dom: explicit Err for UnusedViews

This commit is contained in:
Brian Carroll 2022-11-01 16:58:51 +00:00
parent df4e5de5ee
commit d82396a23f
No known key found for this signature in database
GPG key ID: 5C7B2EC4101703C0

View file

@ -414,30 +414,36 @@ ClientInit state : {
staticViews: Dict HtmlId (Html state), staticViews: Dict HtmlId (Html state),
} }
initClientApp : List U8, List Str, App state initData -> Result (ClientInit state) [ViewNotFound HtmlId, JsonError] | initData has Decoding initClientApp : List U8, List Str, App state initData -> Result (ClientInit state) [JsonError, ViewNotFound HtmlId, UnusedViews (List HtmlId)] | initData has Decoding
initClientApp = \json, viewIdList, app -> initClientApp = \json, viewIdList, app ->
initData <- json |> Decode.fromBytes Json.fromUtf8 |> Result.mapErr (\_ -> JsonError) |> Result.try initData <- json |> Decode.fromBytes Json.fromUtf8 |> Result.mapErr (\_ -> JsonError) |> Result.try
state = app.initDynamic initData state = app.initDynamic initData
dynamicViews = app.renderDynamic state dynamicViews = app.renderDynamic state
unindexedViews = Dict.map dynamicViews translateStatic staticUnindexed = Dict.map dynamicViews translateStatic
empty = Dict.withCapacity (Dict.len unindexedViews) empty = Dict.withCapacity (Dict.len staticUnindexed)
staticViews <- indexViews viewIdList unindexedViews 0 0 empty |> Result.try staticViews <- indexViews viewIdList staticUnindexed 0 0 empty |> Result.try
Ok { Ok {
state, state,
dynamicViews, dynamicViews,
staticViews, staticViews,
} }
indexViews : List HtmlId, Dict HtmlId (Html state), Nat, Nat, Dict HtmlId (Html state) -> Result (Dict HtmlId (Html state)) [ViewNotFound HtmlId] indexViews : List HtmlId, Dict HtmlId (Html state), Nat, Nat, Dict HtmlId (Html state) -> Result (Dict HtmlId (Html state)) [ViewNotFound HtmlId, UnusedViews (List HtmlId)]
indexViews = \viewIdList, unindexedViews, viewIndex, nodeIndex, indexedViews -> indexViews = \viewIdList, unindexedViews, viewIndex, nodeIndex, indexedViews ->
when List.get viewIdList viewIndex is when List.get viewIdList viewIndex is
Err OutOfBounds -> Ok indexedViews
Ok id -> Ok id ->
view <- Dict.get unindexedViews id |> Result.mapErr (\_ -> ViewNotFound id) |> Result.try view <- Dict.get unindexedViews id |> Result.mapErr (\_ -> ViewNotFound id) |> Result.try
indexedState = indexNodes { list: List.withCapacity 1, index: nodeIndex } view indexedState = indexNodes { list: List.withCapacity 1, index: nodeIndex } view
indexedView <- List.first indexedState.list |> Result.mapErr (\_ -> ViewNotFound id) |> Result.try indexedView <- List.first indexedState.list |> Result.mapErr (\_ -> ViewNotFound id) |> Result.try
newIndexedViews = Dict.insert indexedViews id indexedView newIndexedViews = Dict.insert indexedViews id indexedView
indexViews viewIdList unindexedViews (viewIndex + 1) indexedState.index newIndexedViews newUnindexedViews = Dict.remove unindexedViews id
indexViews viewIdList newUnindexedViews (viewIndex + 1) indexedState.index newIndexedViews
Err OutOfBounds ->
if Dict.len unindexedViews == 0 then
Ok indexedViews
else
Err (UnusedViews (Dict.keys unindexedViews))
indexNodes : { list : List (Html state), index : Nat }, Html state -> { list : List (Html state), index : Nat } indexNodes : { list : List (Html state), index : Nat }, Html state -> { list : List (Html state), index : Nat }
indexNodes = \{ list, index }, node -> indexNodes = \{ list, index }, node ->