Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recreate component on arg change #1069

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

_No unreleased changes_

## [2.5.0-pre11] - 2024-02-19

### Fixed
- Recreate component on arg change by @TimLariviere (https://github.com/fabulous-dev/Fabulous/pull/1069)

## [2.5.0-pre10] - 2024-02-19

### Fixed
Expand Down Expand Up @@ -105,7 +110,8 @@ _No unreleased changes_
### Changed
- Fabulous.XamarinForms & Fabulous.MauiControls have been moved been out of the Fabulous repository. Find them in their own repositories: [https://github.com/fabulous-dev/Fabulous.XamarinForms](https://github.com/fabulous-dev/Fabulous.XamarinForms) / [https://github.com/fabulous-dev/Fabulous.MauiControls](https://github.com/fabulous-dev/Fabulous.MauiControls)

[unreleased]: https://github.com/fabulous-dev/Fabulous/compare/2.5.0-pre10...HEAD
[unreleased]: https://github.com/fabulous-dev/Fabulous/compare/2.5.0-pre11...HEAD
[2.5.0-pre11]: https://github.com/fabulous-dev/Fabulous/releases/tag/2.5.0-pre11
[2.5.0-pre10]: https://github.com/fabulous-dev/Fabulous/releases/tag/2.5.0-pre10
[2.5.0-pre9]: https://github.com/fabulous-dev/Fabulous/releases/tag/2.5.0-pre9
[2.5.0-pre8]: https://github.com/fabulous-dev/Fabulous/releases/tag/2.5.0-pre8
Expand Down
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 @@ -89,6 +90,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
type MvuComponentBodyBuilder<'msg, 'marker> =
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
Loading