Skip to content

Commit

Permalink
Perform the Update from OnInitialized synchronously (#352)
Browse files Browse the repository at this point in the history
Fixes #349
  • Loading branch information
marklam authored Oct 22, 2023
1 parent 01ceb9d commit 446845d
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions src/Avalonia.FuncUI/Components/Component.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,34 @@ type Component (render: IComponentContext -> IView) as this =
member internal this.Context with get () = context
member internal this.ComponentId with get () = componentId

member private this.Update () : unit =
Dispatcher.UIThread.Post (fun _ ->

let nextViewElement = Some (render context)

// reset internal context counter
context.AfterRender ()

// update view
VirtualDom.updateBorderRoot (this, lastViewElement, nextViewElement)
lastViewElement <- nextViewElement

let nextViewAttrs = context.ComponentAttrs
member private this.UIThreadUpdate() : unit =
let nextViewElement = Some (render context)

// reset internal context counter
context.AfterRender ()

// update view
VirtualDom.updateBorderRoot (this, lastViewElement, nextViewElement)
lastViewElement <- nextViewElement

let nextViewAttrs = context.ComponentAttrs

// update attrs
Patcher.patch (
this,
{ Delta.ViewDelta.ViewType = typeof<Border>
Delta.ViewDelta.ConstructorArgs = null
Delta.ViewDelta.KeyDidChange = false
Delta.ViewDelta.Outlet = ValueNone
Delta.ViewDelta.Attrs = Differ.diffAttributes (lastViewAttrs, nextViewAttrs) }
)

// update attrs
Patcher.patch (
this,
{ Delta.ViewDelta.ViewType = typeof<Border>
Delta.ViewDelta.ConstructorArgs = null
Delta.ViewDelta.KeyDidChange = false
Delta.ViewDelta.Outlet = ValueNone
Delta.ViewDelta.Attrs = Differ.diffAttributes (lastViewAttrs, nextViewAttrs) }
)
lastViewAttrs <- nextViewAttrs

lastViewAttrs <- nextViewAttrs
context.EffectQueue.ProcessAfterRender ()

context.EffectQueue.ProcessAfterRender ()
)
member private this.Update () : unit =
Dispatcher.UIThread.Post (fun _ -> this.UIThreadUpdate ())

override this.OnInitialized () =
base.OnInitialized ()
Expand All @@ -57,7 +57,7 @@ type Component (render: IComponentContext -> IView) as this =
)
)

this.Update ()
this.UIThreadUpdate ()

override this.OnDetachedFromLogicalTree (eventArgs: Avalonia.LogicalTree.LogicalTreeAttachmentEventArgs) =
base.OnDetachedFromLogicalTree eventArgs
Expand Down

0 comments on commit 446845d

Please sign in to comment.