From d82396a23fd31f5ae63bbd51a1afb60b01a57b85 Mon Sep 17 00:00:00 2001 From: Brian Carroll Date: Tue, 1 Nov 2022 16:58:51 +0000 Subject: [PATCH] virtual-dom: explicit Err for UnusedViews --- .../virtual-dom/platform/Html/Internal.roc | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/examples/virtual-dom/platform/Html/Internal.roc b/examples/virtual-dom/platform/Html/Internal.roc index 14639cdb9b..6cc1d853bd 100644 --- a/examples/virtual-dom/platform/Html/Internal.roc +++ b/examples/virtual-dom/platform/Html/Internal.roc @@ -414,30 +414,36 @@ ClientInit 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 -> initData <- json |> Decode.fromBytes Json.fromUtf8 |> Result.mapErr (\_ -> JsonError) |> Result.try state = app.initDynamic initData dynamicViews = app.renderDynamic state - unindexedViews = Dict.map dynamicViews translateStatic - empty = Dict.withCapacity (Dict.len unindexedViews) - staticViews <- indexViews viewIdList unindexedViews 0 0 empty |> Result.try + staticUnindexed = Dict.map dynamicViews translateStatic + empty = Dict.withCapacity (Dict.len staticUnindexed) + staticViews <- indexViews viewIdList staticUnindexed 0 0 empty |> Result.try Ok { state, dynamicViews, 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 -> when List.get viewIdList viewIndex is - Err OutOfBounds -> Ok indexedViews Ok id -> view <- Dict.get unindexedViews id |> Result.mapErr (\_ -> ViewNotFound id) |> Result.try indexedState = indexNodes { list: List.withCapacity 1, index: nodeIndex } view indexedView <- List.first indexedState.list |> Result.mapErr (\_ -> ViewNotFound id) |> Result.try 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, index }, node ->