Skip to content

Commit

Permalink
Destroy component on arg change
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothé Larivière committed Feb 19, 2024
1 parent 6eef147 commit e89681a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Fabulous/MvuComponent.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Fabulous

open System
open System.Runtime.CompilerServices

[<Struct; NoEquality; NoComparison>]
Expand Down Expand Up @@ -88,6 +89,20 @@ module MvuComponent =

let Data =
Attributes.defineSimpleScalar<MvuComponentData> "MvuComponent_Data" ScalarAttributeComparers.noCompare (fun _ _ _ -> ())

let canReuseMvuComponent (prev: Widget) (curr: Widget) =
let prevData =
match prev.ScalarAttributes with
| ValueSome attrs when attrs.Length > 0 -> attrs[0].Value :?> MvuComponentData
| _ -> failwith "Component widget must have a body"

let currData =
match curr.ScalarAttributes with
| ValueSome attrs when attrs.Length > 0 -> attrs[0].Value :?> MvuComponentData
| _ -> failwith "Component widget must have a body"

// NOTE: Somehow using = here crashes the app and prevents debugging...
Object.Equals(prevData.Arg, currData.Arg)

/// Delegate used by the MvuComponentBuilder to compose a component body
/// It will be aggressively inlined by the compiler leaving no overhead, only a pure function that returns a WidgetBuilder
Expand Down
2 changes: 2 additions & 0 deletions src/Fabulous/View.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module ViewHelpers =
false
else if (prevKey = Memo.MemoWidgetKey) then
Memo.canReuseMemoizedWidget prevWidget currWidget
else if (prevKey = MvuComponent.WidgetKey) then
MvuComponent.canReuseMvuComponent prevWidget currWidget
else
true

Expand Down

0 comments on commit e89681a

Please sign in to comment.