Skip to content

Commit e89681a

Browse files
committed
Destroy component on arg change
1 parent 6eef147 commit e89681a

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/Fabulous/MvuComponent.fs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace Fabulous
22

3+
open System
34
open System.Runtime.CompilerServices
45

56
[<Struct; NoEquality; NoComparison>]
@@ -88,6 +89,20 @@ module MvuComponent =
8889

8990
let Data =
9091
Attributes.defineSimpleScalar<MvuComponentData> "MvuComponent_Data" ScalarAttributeComparers.noCompare (fun _ _ _ -> ())
92+
93+
let canReuseMvuComponent (prev: Widget) (curr: Widget) =
94+
let prevData =
95+
match prev.ScalarAttributes with
96+
| ValueSome attrs when attrs.Length > 0 -> attrs[0].Value :?> MvuComponentData
97+
| _ -> failwith "Component widget must have a body"
98+
99+
let currData =
100+
match curr.ScalarAttributes with
101+
| ValueSome attrs when attrs.Length > 0 -> attrs[0].Value :?> MvuComponentData
102+
| _ -> failwith "Component widget must have a body"
103+
104+
// NOTE: Somehow using = here crashes the app and prevents debugging...
105+
Object.Equals(prevData.Arg, currData.Arg)
91106

92107
/// Delegate used by the MvuComponentBuilder to compose a component body
93108
/// It will be aggressively inlined by the compiler leaving no overhead, only a pure function that returns a WidgetBuilder

src/Fabulous/View.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module ViewHelpers =
88
false
99
else if (prevKey = Memo.MemoWidgetKey) then
1010
Memo.canReuseMemoizedWidget prevWidget currWidget
11+
else if (prevKey = MvuComponent.WidgetKey) then
12+
MvuComponent.canReuseMvuComponent prevWidget currWidget
1113
else
1214
true
1315

0 commit comments

Comments
 (0)