Skip to content

Simplify getters by always throwing#347

Closed
krocard wants to merge 2 commits into
PRN-92/refactor--remove-rejectpromiseonexceptionblockfrom
PRN-92/refactor--simplify-getter-by-always-throwing
Closed

Simplify getters by always throwing#347
krocard wants to merge 2 commits into
PRN-92/refactor--remove-rejectpromiseonexceptionblockfrom
PRN-92/refactor--simplify-getter-by-always-throwing

Conversation

@krocard
Copy link
Copy Markdown
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
Copy Markdown
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

override fun getName() = MODULE_NAME

private var customMessageHandlerBridgeId: NativeId? = null
private val handler = Handler(Looper.getMainLooper())
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 :)

@rolandkakonyi
Copy link
Copy Markdown
Contributor

Since this was open since long time, with now having v1, I think it is time to close this.

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.

4 participants