-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Move
ExtendableHybridClass
to it's own file with explanation (#…
…493) * fix: Move `ExtendableHybridClass` to it's own file with explanation * feat: Make `initHybrid()` static * Revert "feat: Make `initHybrid()` static" This reverts commit 9b10dcf.
- Loading branch information
Showing
2 changed files
with
29 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...tive-nitro-modules/android/src/main/java/com/margelo/nitro/utils/ExtendableHybridClass.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.margelo.nitro.core | ||
|
||
import com.facebook.jni.HybridData | ||
|
||
/** | ||
* Represents an fbjni Hybrid Class (aka a Java class with an accompanying C++ class) | ||
* that supports inheritance. | ||
* | ||
* Each fbjni Hybrid Class initializes it's `mHybridData` using an `initHybrid()` method, | ||
* which initializes the respective C++ class this was bound to. | ||
* Assuming we have Java class `B` which extends Java class `A`, and a C++ | ||
* inheritance chain that reflects the Java inheritance chain (C++ class `B` which extends C++ class `A`), | ||
* we would have two `initHybrid()` methods - one for `A` and one for `B`. | ||
* - When you initialize an instance of `A`, it will initialize `A`'s C++ part. | ||
* - When you initialize an instance of `B`, it will initialize `B`'s C++ part, as well as `A`'s C++ part, since | ||
* the native `A.initHybrid()` method cannot be overridden in `B` - both have their own `initHybrid()` methods! | ||
* | ||
* To fix this issue, we have `ExtendableHybridClass`. In an extendable HybridClass, we initialize `mHybridData`, | ||
* but we pass it upwards the inheritance chain after initializing self using `updateNative(mHybridData)`. | ||
* This way the actual `mHybridData` instance is the lowest class in the chain - `B`. | ||
*/ | ||
interface ExtendableHybridClass { | ||
/** | ||
* Update the `hybridData` (C++ part) this Hybrid Class is referencing. | ||
* Call this with `hybridData` from a subclass to properly fill in the inheritance chain. | ||
*/ | ||
fun updateNative(hybridData: HybridData) | ||
} |