Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ import kotlin.concurrent.Volatile
*/
@OptIn(UnstableReactNativeAPI::class)
@ReactModule(name = NativeAnimatedModuleSpec.NAME)
public class NativeAnimatedModule(reactContext: ReactApplicationContext?) :
public class NativeAnimatedModule(reactContext: ReactApplicationContext) :
NativeAnimatedModuleSpec(reactContext), LifecycleEventListener, UIManagerListener {

// For `queueAndExecuteBatchedOperations`
Expand Down Expand Up @@ -163,12 +163,13 @@ public class NativeAnimatedModule(reactContext: ReactApplicationContext?) :
// Due to a race condition, we manually "carry-over" a polled item from previous batch
// instead of peeking the queue itself for consistency.
// TODO(T112522554): Clean up the queue access
val peekedOperation = peekedOperation
if (peekedOperation != null) {
if (checkNotNull(peekedOperation).batchNumber > maxBatchNumber) {
if (peekedOperation.batchNumber > maxBatchNumber) {
break
}
operations.add(checkNotNull(peekedOperation))
peekedOperation = null
operations.add(peekedOperation)
this.peekedOperation = null
}

val polledOperation =
Expand All @@ -179,7 +180,7 @@ public class NativeAnimatedModule(reactContext: ReactApplicationContext?) :
if (polledOperation.batchNumber > maxBatchNumber) {
// Because the operation is already retrieved from the queue, there's no way of placing it
// back as the head element, so we remember it manually here
peekedOperation = polledOperation
this.peekedOperation = polledOperation
break
}
operations.add(polledOperation)
Expand All @@ -189,8 +190,7 @@ public class NativeAnimatedModule(reactContext: ReactApplicationContext?) :
}
}

private val animatedFrameCallback: GuardedFrameCallback
private val reactChoreographer: ReactChoreographer? = ReactChoreographer.getInstance()
private val reactChoreographer: ReactChoreographer = ReactChoreographer.getInstance()

private val operations = ConcurrentOperationQueue()
private val preOperations = ConcurrentOperationQueue()
Expand All @@ -205,7 +205,6 @@ public class NativeAnimatedModule(reactContext: ReactApplicationContext?) :

private var initializedForFabric = false
private var initializedForNonFabric = false
private var enqueuedAnimationOnFrame = false

@UIManagerType private var uiManagerType = UIManagerType.LEGACY
private var numFabricAnimations = 0
Expand Down Expand Up @@ -302,8 +301,8 @@ public class NativeAnimatedModule(reactContext: ReactApplicationContext?) :
}
}

checkNotNull(preOperations).executeBatch(batchNumber, nodesManager)
checkNotNull(operations).executeBatch(batchNumber, nodesManager)
preOperations.executeBatch(batchNumber, nodesManager)
operations.executeBatch(batchNumber, nodesManager)
}

// For non-FabricUIManager only
Expand All @@ -322,11 +321,9 @@ public class NativeAnimatedModule(reactContext: ReactApplicationContext?) :
// might be stripped out.
val frameNo = currentBatchNumber++

val preOperationsUIBlock = UIBlock {
checkNotNull(preOperations).executeBatch(frameNo, nodesManager)
}
val preOperationsUIBlock = UIBlock { preOperations.executeBatch(frameNo, nodesManager) }

val operationsUIBlock = UIBlock { checkNotNull(operations).executeBatch(frameNo, nodesManager) }
val operationsUIBlock = UIBlock { operations.executeBatch(frameNo, nodesManager) }

assert(uiManager is UIManagerModule)
val uiManagerModule = uiManager as UIManagerModule
Expand Down Expand Up @@ -366,41 +363,34 @@ public class NativeAnimatedModule(reactContext: ReactApplicationContext?) :
nodesManagerRef.set(nodesManager)
}

init {
animatedFrameCallback =
object : GuardedFrameCallback(checkNotNull(reactContext)) {
override fun doFrameGuarded(frameTimeNanos: Long) {
try {
enqueuedAnimationOnFrame = false
val nodesManager = nodesManager
if (nodesManager?.hasActiveAnimations() == true) {
nodesManager.runUpdates(frameTimeNanos)
}
// This is very unlikely to ever be hit.
if (nodesManager == null || reactChoreographer == null) {
return
}

enqueueFrameCallback()
} catch (ex: Exception) {
throw RuntimeException(ex)
private var enqueuedAnimationOnFrame = false
private val animatedFrameCallback =
object : GuardedFrameCallback(reactContext) {
override fun doFrameGuarded(frameTimeNanos: Long) {
try {
enqueuedAnimationOnFrame = false
val nodesManager = nodesManager ?: return
if (nodesManager.hasActiveAnimations()) {
nodesManager.runUpdates(frameTimeNanos)
}

enqueueFrameCallback()
} catch (ex: Exception) {
throw RuntimeException(ex)
}
}
}
}

private fun clearFrameCallback() {
checkNotNull(reactChoreographer)
.removeFrameCallback(
ReactChoreographer.CallbackType.NATIVE_ANIMATED_MODULE, animatedFrameCallback)
reactChoreographer.removeFrameCallback(
ReactChoreographer.CallbackType.NATIVE_ANIMATED_MODULE, animatedFrameCallback)
enqueuedAnimationOnFrame = false
}

private fun enqueueFrameCallback() {
if (!enqueuedAnimationOnFrame) {
checkNotNull(reactChoreographer)
.postFrameCallback(
ReactChoreographer.CallbackType.NATIVE_ANIMATED_MODULE, animatedFrameCallback)
reactChoreographer.postFrameCallback(
ReactChoreographer.CallbackType.NATIVE_ANIMATED_MODULE, animatedFrameCallback)
enqueuedAnimationOnFrame = true
}
}
Expand Down Expand Up @@ -431,8 +421,9 @@ public class NativeAnimatedModule(reactContext: ReactApplicationContext?) :
}

// Subscribe to UIManager (Fabric or non-Fabric) lifecycle events if we haven't yet
if (if (uiManagerType == UIManagerType.FABRIC) initializedForFabric
else initializedForNonFabric) {
val initialized =
if (uiManagerType == UIManagerType.FABRIC) initializedForFabric else initializedForNonFabric
if (initialized) {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface ReactClippingViewGroup {
*/
public fun updateClippingRect()

public fun updateClippingRect(excludedView: Set<Int>?)
public fun updateClippingRect(excludedViews: Set<Int>?)

/**
* Get rectangular bounds to which view is currently clipped to. Called only on views that has set
Expand Down
Loading
Loading