From 157c132394571da2e877098bbfcd7b0c608b36c2 Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Mon, 4 Dec 2023 18:58:11 +0000 Subject: [PATCH 1/2] Implement AttachView --- src/Fabulous/Component.fs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Fabulous/Component.fs b/src/Fabulous/Component.fs index e6af90140..7e36917ac 100644 --- a/src/Fabulous/Component.fs +++ b/src/Fabulous/Component.fs @@ -304,7 +304,24 @@ module Component = { Key = key Name = "Component" TargetType = typeof - AttachView = fun _ -> failwith "Component widget cannot be attached" + AttachView = + fun (widget, treeContext, _, root) -> + match widget.ScalarAttributes with + | ValueNone -> failwith "Component widget must have a body and a context" + | ValueSome attrs -> + let body = + match Array.tryFind (fun (attr: ScalarAttribute) -> attr.Key = Component.Body.Key) attrs with + | Some attr -> attr.Value :?> ComponentBody + | None -> failwith "Component widget must have a body" + + let context = + match Array.tryFind (fun (attr: ScalarAttribute) -> attr.Key = Component.Context.Key) attrs with + | Some attr -> attr.Value :?> ComponentContext + | None -> ComponentContext() + + let comp = new Component(treeContext, body, context) + let node = comp.Attach(root) + node CreateView = fun (widget, treeContext, _) -> match widget.ScalarAttributes with From fc73a5d17821d772ec7ce9fea7a5be9321009559 Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Mon, 4 Dec 2023 19:10:09 +0000 Subject: [PATCH 2/2] MvuComponent and Memo --- src/Fabulous/Component.fs | 4 ++-- src/Fabulous/Memo.fs | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Fabulous/Component.fs b/src/Fabulous/Component.fs index 7e36917ac..02c4af215 100644 --- a/src/Fabulous/Component.fs +++ b/src/Fabulous/Component.fs @@ -305,7 +305,7 @@ module Component = Name = "Component" TargetType = typeof AttachView = - fun (widget, treeContext, _, root) -> + fun (widget, treeContext, _parentNode, view) -> match widget.ScalarAttributes with | ValueNone -> failwith "Component widget must have a body and a context" | ValueSome attrs -> @@ -320,7 +320,7 @@ module Component = | None -> ComponentContext() let comp = new Component(treeContext, body, context) - let node = comp.Attach(root) + let node = comp.AttachView(view) node CreateView = fun (widget, treeContext, _) -> diff --git a/src/Fabulous/Memo.fs b/src/Fabulous/Memo.fs index 63709f855..bc11a056a 100644 --- a/src/Fabulous/Memo.fs +++ b/src/Fabulous/Memo.fs @@ -89,6 +89,19 @@ module Memo = // to pass it to reconciler later on node.MemoizedWidget <- Some memoizedWidget struct (node, view) - AttachView = fun (_widget, _context, _parentNode, _view) -> failwith "Memo widget cannot be attached" } + AttachView = + fun (widget, context, parentNode, view) -> + let memoData = getMemoData widget + + let memoizedWidget = memoData.CreateWidget memoData.KeyData + + let memoizedDef = WidgetDefinitionStore.get memoizedWidget.Key + + let node = memoizedDef.AttachView(memoizedWidget, context, parentNode, view) + + // store widget that was used to produce this node + // to pass it to reconciler later on + node.MemoizedWidget <- Some memoizedWidget + node } WidgetDefinitionStore.set MemoWidgetKey widgetDefinition