@@ -59,15 +59,18 @@ namespace ReactNative.Animated
59
59
/// may be caused by concurrent updates of animated graph while UI thread
60
60
/// is "executing" the animation loop.
61
61
/// </remarks>
62
- public class NativeAnimatedModule : ReactContextNativeModuleBase , IOnBatchCompleteListener , ILifecycleEventListener
62
+ public class NativeAnimatedModule : ReactContextNativeModuleBase , ILifecycleEventListener
63
63
{
64
64
private readonly object _operationsGate = new object ( ) ;
65
65
66
66
private EventHandler < FrameEventArgs > _animatedFrameCallback ;
67
67
68
68
private List < Action < NativeAnimatedNodesManager > > _operations =
69
69
new List < Action < NativeAnimatedNodesManager > > ( ) ;
70
- private List < Action < NativeAnimatedNodesManager > > _readyOperations ;
70
+ private List < Action < NativeAnimatedNodesManager > > _preOperations =
71
+ new List < Action < NativeAnimatedNodesManager > > ( ) ;
72
+
73
+ private NativeAnimatedNodesManager _nodesManager ;
71
74
72
75
/// <summary>
73
76
/// Instantiates the <see cref="NativeAnimatedModule"/>.
@@ -89,6 +92,20 @@ public override string Name
89
92
}
90
93
}
91
94
95
+ private NativeAnimatedNodesManager NodesManager
96
+ {
97
+ get
98
+ {
99
+ if ( _nodesManager == null )
100
+ {
101
+ var uiManager = Context . GetNativeModule < UIManagerModule > ( ) ;
102
+ _nodesManager = new NativeAnimatedNodesManager ( uiManager ) ;
103
+ }
104
+
105
+ return _nodesManager ;
106
+ }
107
+ }
108
+
92
109
/// <summary>
93
110
/// Called after the creation of a <see cref="IReactInstance"/>, in
94
111
/// order to initialize native modules that require the React or
@@ -98,26 +115,12 @@ public override void Initialize()
98
115
{
99
116
var ctx = Context ;
100
117
var uiManager = ctx . GetNativeModule < UIManagerModule > ( ) ;
101
- var nodesManager = new NativeAnimatedNodesManager ( uiManager ) ;
118
+ uiManager . DispatchingViewUpdates += OnDispatchingViewUpdates ;
102
119
_animatedFrameCallback = ( sender , args ) =>
103
120
{
104
121
try
105
122
{
106
- var operations = default ( List < Action < NativeAnimatedNodesManager > > ) ;
107
- lock ( _operationsGate )
108
- {
109
- operations = _readyOperations ;
110
- _readyOperations = null ;
111
- }
112
-
113
- if ( operations != null )
114
- {
115
- foreach ( var operation in operations )
116
- {
117
- operation ( nodesManager ) ;
118
- }
119
- }
120
-
123
+ var nodesManager = NodesManager ;
121
124
if ( nodesManager . HasActiveAnimations )
122
125
{
123
126
nodesManager . RunUpdates ( args . RenderingTime ) ;
@@ -136,29 +139,41 @@ public override void Initialize()
136
139
ctx . AddLifecycleEventListener ( this ) ;
137
140
}
138
141
139
- /// <summary>
140
- /// Invoked when a batch of JavaScript to native calls has finished.
141
- /// </summary>
142
- public void OnBatchComplete ( )
142
+ private void OnDispatchingViewUpdates ( object sender , EventArgs e )
143
143
{
144
- var operations = _operations . Count == 0 ? null : _operations ;
145
- _operations = new List < Action < NativeAnimatedNodesManager > > ( ) ;
146
- if ( operations != null )
144
+ if ( _operations . Count == 0 && _preOperations . Count == 0 )
145
+ {
146
+ return ;
147
+ }
148
+
149
+ var uiManager = ( UIManagerModule ) sender ;
150
+ List < Action < NativeAnimatedNodesManager > > preOperations ;
151
+ List < Action < NativeAnimatedNodesManager > > operations ;
152
+ lock ( _operationsGate )
153
+ {
154
+ preOperations = _preOperations ;
155
+ operations = _operations ;
156
+ _preOperations = new List < Action < NativeAnimatedNodesManager > > ( ) ;
157
+ _operations = new List < Action < NativeAnimatedNodesManager > > ( ) ;
158
+ }
159
+
160
+ uiManager . PrependUIBlock ( new UIBlock ( ( ) =>
147
161
{
148
- lock ( _operationsGate )
162
+ var nodesManager = NodesManager ;
163
+ foreach ( var operation in preOperations )
149
164
{
150
- if ( _readyOperations == null )
151
- {
152
- _readyOperations = operations ;
153
- }
154
- else
155
- {
156
- _readyOperations . AddRange ( operations ) ;
157
- }
165
+ operation ( nodesManager ) ;
158
166
}
167
+ } ) ) ;
159
168
160
- ReactChoreographer . Instance . ActivateCallback ( nameof ( NativeAnimatedModule ) ) ;
161
- }
169
+ uiManager . AddUIBlock ( new UIBlock ( ( ) =>
170
+ {
171
+ var nodesManager = NodesManager ;
172
+ foreach ( var operation in operations )
173
+ {
174
+ operation ( nodesManager ) ;
175
+ }
176
+ } ) ) ;
162
177
}
163
178
164
179
/// <summary>
@@ -193,7 +208,7 @@ public void OnSuspend()
193
208
[ ReactMethod ]
194
209
public void createAnimatedNode ( int tag , JObject config )
195
210
{
196
- _operations . Add ( manager =>
211
+ AddOperation ( manager =>
197
212
manager . CreateAnimatedNode ( tag , config ) ) ;
198
213
}
199
214
@@ -204,7 +219,7 @@ public void createAnimatedNode(int tag, JObject config)
204
219
[ ReactMethod ]
205
220
public void startListeningToAnimatedNodeValue ( int tag )
206
221
{
207
- _operations . Add ( manager =>
222
+ AddOperation ( manager =>
208
223
manager . StartListeningToAnimatedNodeValue ( tag , value =>
209
224
Context . GetJavaScriptModule < RCTDeviceEventEmitter > ( )
210
225
. emit ( "onAnimatedValueUpdate" , new JObject
@@ -221,7 +236,7 @@ public void startListeningToAnimatedNodeValue(int tag)
221
236
[ ReactMethod ]
222
237
public void stopListeningToAnimatedNodeValue ( int tag )
223
238
{
224
- _operations . Add ( manager =>
239
+ AddOperation ( manager =>
225
240
manager . StopListeningToAnimatedNodeValue ( tag ) ) ;
226
241
}
227
242
@@ -232,7 +247,7 @@ public void stopListeningToAnimatedNodeValue(int tag)
232
247
[ ReactMethod ]
233
248
public void dropAnimatedNode ( int tag )
234
249
{
235
- _operations . Add ( manager =>
250
+ AddOperation ( manager =>
236
251
manager . DropAnimatedNode ( tag ) ) ;
237
252
}
238
253
@@ -244,7 +259,7 @@ public void dropAnimatedNode(int tag)
244
259
[ ReactMethod ]
245
260
public void setAnimatedNodeValue ( int tag , double value )
246
261
{
247
- _operations . Add ( manager =>
262
+ AddOperation ( manager =>
248
263
manager . SetAnimatedNodeValue ( tag , value ) ) ;
249
264
}
250
265
@@ -256,7 +271,7 @@ public void setAnimatedNodeValue(int tag, double value)
256
271
[ ReactMethod ]
257
272
public void setAnimatedNodeOffset ( int tag , double value )
258
273
{
259
- _operations . Add ( manager =>
274
+ AddOperation ( manager =>
260
275
manager . SetAnimatedNodeOffset ( tag , value ) ) ;
261
276
}
262
277
@@ -267,7 +282,7 @@ public void setAnimatedNodeOffset(int tag, double value)
267
282
[ ReactMethod ]
268
283
public void flattenAnimatedNodeOffset ( int tag )
269
284
{
270
- _operations . Add ( manager =>
285
+ AddOperation ( manager =>
271
286
manager . FlattenAnimatedNodeOffset ( tag ) ) ;
272
287
}
273
288
@@ -278,7 +293,7 @@ public void flattenAnimatedNodeOffset(int tag)
278
293
[ ReactMethod ]
279
294
public void extractAnimatedNodeOffset ( int tag )
280
295
{
281
- _operations . Add ( manager =>
296
+ AddOperation ( manager =>
282
297
manager . ExtractAnimatedNodeOffset ( tag ) ) ;
283
298
}
284
299
@@ -296,7 +311,7 @@ public void startAnimatingNode(
296
311
JObject animationConfig ,
297
312
ICallback endCallback )
298
313
{
299
- _operations . Add ( manager =>
314
+ AddOperation ( manager =>
300
315
manager . StartAnimatingNode (
301
316
animationId ,
302
317
animatedNodeTag ,
@@ -311,7 +326,7 @@ public void startAnimatingNode(
311
326
[ ReactMethod ]
312
327
public void stopAnimation ( int animationId )
313
328
{
314
- _operations . Add ( manager =>
329
+ AddOperation ( manager =>
315
330
manager . StopAnimation ( animationId ) ) ;
316
331
}
317
332
@@ -323,7 +338,7 @@ public void stopAnimation(int animationId)
323
338
[ ReactMethod ]
324
339
public void connectAnimatedNodes ( int parentNodeTag , int childNodeTag )
325
340
{
326
- _operations . Add ( manager =>
341
+ AddOperation ( manager =>
327
342
manager . ConnectAnimatedNodes ( parentNodeTag , childNodeTag ) ) ;
328
343
}
329
344
@@ -335,7 +350,7 @@ public void connectAnimatedNodes(int parentNodeTag, int childNodeTag)
335
350
[ ReactMethod ]
336
351
public void disconnectAnimatedNodes ( int parentNodeTag , int childNodeTag )
337
352
{
338
- _operations . Add ( manager =>
353
+ AddOperation ( manager =>
339
354
manager . DisconnectAnimatedNodes ( parentNodeTag , childNodeTag ) ) ;
340
355
}
341
356
@@ -347,20 +362,25 @@ public void disconnectAnimatedNodes(int parentNodeTag, int childNodeTag)
347
362
[ ReactMethod ]
348
363
public void connectAnimatedNodeToView ( int animatedNodeTag , int viewTag )
349
364
{
350
- _operations . Add ( manager =>
365
+ AddOperation ( manager =>
351
366
manager . ConnectAnimatedNodeToView ( animatedNodeTag , viewTag ) ) ;
352
367
}
353
368
354
369
/// <summary>
355
370
/// Disconnects animated node from view.
356
371
/// </summary>
357
372
/// <param name="animatedNodeTag">Animated node tag.</param>
358
- /// <param name="viewTag">React view tag.s </param>
373
+ /// <param name="viewTag">React view tag.</param>
359
374
[ ReactMethod ]
360
375
public void disconnectAnimatedNodeFromView ( int animatedNodeTag , int viewTag )
361
376
{
362
- _operations . Add ( manager =>
363
- manager . DisconnectAnimatedNodeFromView ( animatedNodeTag , viewTag ) ) ;
377
+ lock ( _operationsGate )
378
+ {
379
+ _preOperations . Add ( manager =>
380
+ manager . RestoreDefaultValues ( animatedNodeTag , viewTag ) ) ;
381
+ _operations . Add ( manager =>
382
+ manager . DisconnectAnimatedNodeFromView ( animatedNodeTag , viewTag ) ) ;
383
+ }
364
384
}
365
385
366
386
/// <summary>
@@ -372,7 +392,7 @@ public void disconnectAnimatedNodeFromView(int animatedNodeTag, int viewTag)
372
392
[ ReactMethod ]
373
393
public void addAnimatedEventToView ( int viewTag , string eventName , JObject eventMapping )
374
394
{
375
- _operations . Add ( manager =>
395
+ AddOperation ( manager =>
376
396
manager . AddAnimatedEventToView ( viewTag , eventName , eventMapping ) ) ;
377
397
}
378
398
@@ -385,8 +405,39 @@ public void addAnimatedEventToView(int viewTag, string eventName, JObject eventM
385
405
[ ReactMethod ]
386
406
public void removeAnimatedEventFromView ( int viewTag , string eventName , int animatedValueTag )
387
407
{
388
- _operations . Add ( manager =>
408
+ AddOperation ( manager =>
389
409
manager . RemoveAnimatedEventFromView ( viewTag , eventName , animatedValueTag ) ) ;
390
410
}
411
+
412
+ private void AddOperation ( Action < NativeAnimatedNodesManager > action )
413
+ {
414
+ lock ( _operationsGate )
415
+ {
416
+ _operations . Add ( action ) ;
417
+ }
418
+ }
419
+
420
+ private void AddPreOperation ( Action < NativeAnimatedNodesManager > action )
421
+ {
422
+ lock ( _operationsGate )
423
+ {
424
+ _preOperations . Add ( action ) ;
425
+ }
426
+ }
427
+
428
+ class UIBlock : IUIBlock
429
+ {
430
+ private readonly Action _action ;
431
+
432
+ public UIBlock ( Action action )
433
+ {
434
+ _action = action ;
435
+ }
436
+
437
+ public void Execute ( NativeViewHierarchyManager nativeViewHierarchyManager )
438
+ {
439
+ _action ( ) ;
440
+ }
441
+ }
391
442
}
392
443
}
0 commit comments