Skip to content
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

Simplify getters by always throwing #347

Open
wants to merge 2 commits into
base: PRN-92/refactor--remove-rejectpromiseonexceptionblock
Choose a base branch
from

Conversation

krocard
Copy link
Contributor

@krocard krocard commented Dec 7, 2023

Problem (PRN-92)

Getters were duplicated between returning null or throwing. Now that Exception are consistently caught, those returning a nullable type can be removed.

Changes

Remove and inline many getters.
Accessing another module is now consistently and more transparently: context.MODULE_NAME

📚 Other PRs for this issue

@krocard krocard self-assigned this Dec 7, 2023
}
}

private fun RNPlayerView.getPlayerViewOrThrow() = playerView
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I quite like the require naming convention that androidx libraries use: https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment#requireActivity()

Suggested change
private fun RNPlayerView.getPlayerViewOrThrow() = playerView
private fun RNPlayerView.requirePlayerView() = playerView

@@ -46,6 +45,7 @@ class RNPlayerViewManager(private val context: ReactApplicationContext) : Simple
override fun getName() = MODULE_NAME

private var customMessageHandlerBridgeId: NativeId? = null
private val handler = Handler(Looper.getMainLooper())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private val handler = Handler(Looper.getMainLooper())
private val mainHandler = Handler(Looper.getMainLooper())

Handler(Looper.getMainLooper()).post {
view.playerView?.setFullscreenHandler(
context.getModule<FullscreenHandlerModule>()?.getInstance(fullscreenBridgeId),
handler.postAndLogException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is already in the develop branch, could you rebase this branch please?

inline fun <reified T : ReactContextBaseJavaModule> ReactContext.getModule(): T? {
return getNativeModule(T::class.java)
}
private inline fun <reified T : ReactContextBaseJavaModule> ReactContext.getModuleOrThrow(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I quite like the require naming convention that androidx libraries use: https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment#requireActivity()

Suggested change
private inline fun <reified T : ReactContextBaseJavaModule> ReactContext.getModuleOrThrow(
private inline fun <reified T : ReactContextBaseJavaModule> ReactContext.requireModule(

}
private inline fun <reified T : ReactContextBaseJavaModule> ReactContext.getModuleOrThrow(
name: String,
): T = getNativeModule(T::class.java).throwIfNull(name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this extension function:

Suggested change
): T = getNativeModule(T::class.java).throwIfNull(name)
): T = getNativeModule(T::class.java) ?: error("${name}Module not found")

Copy link
Contributor

@matamegger matamegger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly agree with the comments added by @zigavehovec.

Will have another look once the branch is in sync with the development branch :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants