Skip to content

Kotlinify GuardedFrameCallback #43841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
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
2 changes: 1 addition & 1 deletion packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -4049,7 +4049,7 @@ public final class com/facebook/react/uimanager/FloatUtil {

public abstract class com/facebook/react/uimanager/GuardedFrameCallback : android/view/Choreographer$FrameCallback {
protected fun <init> (Lcom/facebook/react/bridge/ReactContext;)V
public final fun doFrame (J)V
public fun doFrame (J)V
protected abstract fun doFrameGuarded (J)V
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.internal

import com.facebook.react.bridge.UiThreadUtil

/** An implementation of ChoreographerProvider that directly uses android.view.Choreographer. */
public object AndroidChoreographerProvider : ChoreographerProvider {

private class AndroidChoreographer : ChoreographerProvider.Choreographer {
private val instance: android.view.Choreographer = android.view.Choreographer.getInstance()

override public fun postFrameCallback(callback: android.view.Choreographer.FrameCallback) {
instance.postFrameCallback(callback)
}

override public fun removeFrameCallback(callback: android.view.Choreographer.FrameCallback) {
instance.removeFrameCallback(callback)
}
}

@JvmStatic public fun getInstance(): AndroidChoreographerProvider = this

override public fun getChoreographer(): ChoreographerProvider.Choreographer {
UiThreadUtil.assertOnUiThread()
return AndroidChoreographer()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.internal;
package com.facebook.react.internal

import android.view.Choreographer.FrameCallback

public interface ChoreographerProvider {
/**
* The interface to a android.view.Choreographer-like object, that can either use the
* android.view.Choreographer or a mock one for testing purposes, or override built-in
* android.view.Choreographer's behaviors.
*/
interface Choreographer {

public interface Choreographer {
/** Posts a frame callback to run on the next frame. */
void postFrameCallback(android.view.Choreographer.FrameCallback callback);
public fun postFrameCallback(callback: FrameCallback)

/** Removes a previously posted frame callback. */
void removeFrameCallback(android.view.Choreographer.FrameCallback callback);
public fun removeFrameCallback(callback: FrameCallback)
}

/**
* Get an instance of Choreographer.
*
* @return An instance of Choreographer.
*/
Choreographer getChoreographer();
public fun getChoreographer(): Choreographer
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.uimanager

import android.view.Choreographer
import com.facebook.react.bridge.ReactContext

/**
* Abstract base for a Choreographer FrameCallback that should have any RuntimeExceptions it throws
* handled by the [JSExceptionHandler] registered if the app is in dev mode.
*/
public abstract class GuardedFrameCallback
protected constructor(private val reactContext: ReactContext) : Choreographer.FrameCallback {

override public fun doFrame(frameTimeNanos: Long) {
try {
doFrameGuarded(frameTimeNanos)
} catch (e: RuntimeException) {
reactContext.handleException(e)
}
}

/**
* Like the standard doFrame but RuntimeExceptions will be caught and passed to
* [ReactContext#handleException(RuntimeException)].
*/
protected abstract fun doFrameGuarded(frameTimeNanos: Long)
}