@@ -8,6 +8,8 @@ public abstract class BaseContainerNode : BaseNode
88 {
99 private readonly List < BaseNode > nodes = new List < BaseNode > ( ) ;
1010
11+ private int updateCount ;
12+
1113 /// <summary>The child nodes of the container.</summary>
1214 public IReadOnlyList < BaseNode > Nodes => nodes ;
1315
@@ -122,6 +124,35 @@ private bool TryGetNeighbour(BaseNode node, int offset, out BaseNode neighbour)
122124 return true ;
123125 }
124126
127+ /// <summary>
128+ /// Disables internal events to speed up batch processing.
129+ /// <see cref="EndUpdate"/> must be called to restore the functionality.
130+ /// </summary>
131+ public void BeginUpdate ( )
132+ {
133+ updateCount ++ ;
134+ }
135+
136+ /// <summary>
137+ /// Enables internal events disabled by <see cref="BeginUpdate"/>.
138+ /// </summary>
139+ public void EndUpdate ( )
140+ {
141+ updateCount = Math . Max ( 0 , updateCount - 1 ) ;
142+
143+ OnNodesUpdated ( ) ;
144+ }
145+
146+ private void OnNodesUpdated ( )
147+ {
148+ if ( updateCount == 0 )
149+ {
150+ UpdateOffsets ( ) ;
151+
152+ GetParentContainer ( ) ? . ChildHasChanged ( this ) ;
153+ }
154+ }
155+
125156 /// <summary>Replaces the old node with the new node.</summary>
126157 /// <param name="oldNode">The old node to replacce.</param>
127158 /// <param name="newNode">The new node.</param>
@@ -172,9 +203,7 @@ public void ReplaceChildNode(BaseNode oldNode, BaseNode newNode, ref List<BaseNo
172203 }*/
173204 }
174205
175- UpdateOffsets ( ) ;
176-
177- GetParentContainer ( ) ? . ChildHasChanged ( this ) ;
206+ OnNodesUpdated ( ) ;
178207 }
179208
180209 /// <summary>
@@ -253,9 +282,7 @@ protected void InsertBytes(int index, int size, ref List<BaseNode> createdNodes)
253282 index ++ ;
254283 }
255284
256- UpdateOffsets ( ) ;
257-
258- GetParentContainer ( ) ? . ChildHasChanged ( this ) ;
285+ OnNodesUpdated ( ) ;
259286 }
260287
261288 /// <summary>
@@ -286,9 +313,7 @@ public void AddNode(BaseNode node)
286313
287314 nodes . Add ( node ) ;
288315
289- UpdateOffsets ( ) ;
290-
291- GetParentContainer ( ) ? . ChildHasChanged ( this ) ;
316+ OnNodesUpdated ( ) ;
292317 }
293318
294319 /// <summary>
@@ -312,9 +337,7 @@ public void InsertNode(BaseNode position, BaseNode node)
312337
313338 nodes . Insert ( index , node ) ;
314339
315- UpdateOffsets ( ) ;
316-
317- GetParentContainer ( ) ? . ChildHasChanged ( this ) ;
340+ OnNodesUpdated ( ) ;
318341 }
319342
320343 /// <summary>Removes the specified node.</summary>
@@ -327,9 +350,7 @@ public bool RemoveNode(BaseNode node)
327350 var result = nodes . Remove ( node ) ;
328351 if ( result )
329352 {
330- UpdateOffsets ( ) ;
331-
332- GetParentContainer ( ) ? . ChildHasChanged ( this ) ;
353+ OnNodesUpdated ( ) ;
333354 }
334355 return result ;
335356 }
0 commit comments