Skip to content

Commit ba440f7

Browse files
committed
refactor: Optimization for notify onMount listeners
1 parent fb64880 commit ba440f7

2 files changed

Lines changed: 23 additions & 15 deletions

File tree

src/Sutil/Core.fs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -666,23 +666,23 @@ type MountListeners() =
666666
| None ->
667667
MountListeners.WaitUntil( findNode, f )
668668

669-
let private notifySutilEvents (parent : SutilEffect) (node : SutilEffect) =
669+
let private notifySutilEvents (parent : SutilEffect) (node : SutilEffect) (onMountElements : ResizeArray<HTMLElement>) =
670670
if (parent.IsConnected()) then
671-
node.collectDomNodes ()
672-
|> List.iter (fun n ->
671+
let _nodes = onMountElements.ToArray()
672+
onMountElements.Clear()
673+
_nodes |> Array.iter (fun n ->
673674
CustomDispatch<_>.dispatch(n,Event.Connected)
674675
CustomDispatch<_>.dispatch(n,Event.Mount)
675676
MountListenersInternal.notifyOnMountListeners(n, true)
676677

677-
n
678-
|> DomHelpers.descendants
679-
|> Seq.filter DomHelpers.isElementNode
680-
|> Seq.toArray
681-
|> Array.iter (fun n ->
682-
CustomDispatch<_>.dispatch(n,Event.Mount)
683-
MountListenersInternal.notifyOnMountListeners(n,false)
684-
)
685-
678+
// n
679+
// |> DomHelpers.descendants
680+
// |> Seq.filter DomHelpers.isElementNode
681+
// |> Seq.toArray
682+
// |> Array.iter (fun n ->
683+
// CustomDispatch<_>.dispatch(n,Event.Mount)
684+
// MountListenersInternal.notifyOnMountListeners(n,false)
685+
// )
686686
)
687687

688688
/// <exclude/>
@@ -701,6 +701,7 @@ and BuildContext =
701701
Action: DomAction
702702
MakeName: (string -> string)
703703
Class: string option
704+
OnMount: ResizeArray<HTMLElement>
704705
Debug: bool
705706
Pipeline : PipelineFn
706707
}
@@ -716,13 +717,13 @@ and BuildContext =
716717
if logEnabled() then log $"ctx.Append '{node}' to '{ctx.Parent}' after {ctx.Previous}"
717718
ctx.Parent.InsertAfter(node, ctx.Previous)
718719

719-
notifySutilEvents ctx.Parent node
720+
notifySutilEvents ctx.Parent node (ctx.OnMount)
720721

721722
| Replace (existing, insertBefore) ->
722723
if logEnabled() then log $"ctx.Replace '{existing}' with '{node}' before '{nodeStrShort insertBefore}'"
723724
ctx.Parent.ReplaceGroup(node, existing, insertBefore)
724725

725-
notifySutilEvents ctx.Parent node
726+
notifySutilEvents ctx.Parent node (ctx.OnMount)
726727
()
727728

728729

@@ -785,6 +786,7 @@ let private defaultContext (parent : Node) =
785786
Action = Append
786787
// StyleSheet = None
787788
Class = None
789+
OnMount = Unchecked.defaultof<_>
788790
Debug = false
789791
MakeName = fun baseName -> sprintf "%s-%d" baseName (gen ())
790792
Pipeline = id
@@ -800,7 +802,8 @@ let private makeContext (parent: Node) =
800802
classes
801803

802804
{ defaultContext parent with
803-
//Pipeline = pipeline
805+
806+
OnMount = new ResizeArray<_>()
804807

805808
// Ensures that if we create and mount DOM nodes onto a styled element, then
806809
// we inherit the stylesheet class on the mounted nodes
@@ -812,6 +815,7 @@ let private makeContext (parent: Node) =
812815

813816
let private makeShadowContext (customElement: Node) =
814817
{ defaultContext customElement with
818+
OnMount = new ResizeArray<_>()
815819
Action = Nothing
816820
}
817821

src/Sutil/CoreElements.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ let private _on (event : string) (fn : Event -> unit) (options : EventModifier l
369369
// Fable.Core.JS.console.log(" - " + event + ": default prevented", ctx.ParentElement )
370370

371371
Interop.addEventListener(el , event, handler )
372+
if (event = Event.Mount) then
373+
// Fable.Core.JS.console.log("_onMount")
374+
ctx.OnMount.Add(ctx.ParentElement)
375+
Interop.set ctx.ParentElement "_onmount" true
372376

373377
SutilEffect.RegisterUnsubscribe( ctx.Parent, fun _ -> el.removeEventListener(event,handler) )
374378

0 commit comments

Comments
 (0)