generated from JetBrains/compose-multiplatform-template
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from KevinnZou/feature/jsbridge_support Suppor…
…t communication between Native and JS Support communication between Native and JS
- Loading branch information
Showing
42 changed files
with
902 additions
and
100 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
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
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
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
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
60 changes: 60 additions & 0 deletions
60
sample/shared/src/commonMain/kotlin/com/kevinnzou/sample/eventbus/EventBus.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,60 @@ | ||
package com.kevinnzou.sample.eventbus | ||
|
||
import kotlinx.atomicfu.atomic | ||
import kotlinx.atomicfu.getAndUpdate | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* Created By Kevin Zou On 2023/12/15 | ||
*/ | ||
|
||
typealias Observer = (Any) -> Unit | ||
|
||
object EventBus { | ||
private val observers = atomic(mutableMapOf<KClass<out Any>, List<Observer>>()) | ||
|
||
fun <T : Any> observe( | ||
clazz: KClass<T>, | ||
obs: (T) -> Unit, | ||
) { | ||
if (!observers.value.containsKey(clazz)) { | ||
observers.getAndUpdate { cur -> | ||
cur.toMutableMap().also { upd -> | ||
upd[clazz] = listOf(obs as Observer) | ||
} | ||
} | ||
} else { | ||
observers.getAndUpdate { cur -> | ||
cur.toMutableMap().also { upd -> | ||
upd[clazz] = upd[clazz]!! + listOf(obs as Observer) | ||
} | ||
} | ||
} | ||
} | ||
|
||
inline fun <reified T : Any> observe(noinline obs: (T) -> Unit) { | ||
observe(T::class, obs) | ||
} | ||
|
||
fun <T : Any> removeObserver( | ||
clazz: KClass<T>, | ||
obs: (T) -> Unit, | ||
) { | ||
observers.getAndUpdate { cur -> | ||
cur.toMutableMap().also { upd -> | ||
upd.remove(clazz) | ||
} | ||
} | ||
} | ||
|
||
fun <T : Any> post( | ||
clazz: KClass<T>, | ||
event: T, | ||
) { | ||
observers.value[clazz]?.forEach { it.invoke(event) } | ||
} | ||
|
||
inline fun <reified T : Any> post(event: T) { | ||
post(T::class, event) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
sample/shared/src/commonMain/kotlin/com/kevinnzou/sample/eventbus/FlowEventBus.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,16 @@ | ||
package com.kevinnzou.sample.eventbus | ||
|
||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
|
||
/** | ||
* Created By Kevin Zou On 2023/12/16 | ||
*/ | ||
object FlowEventBus { | ||
private val mEvents = MutableSharedFlow<IEvent>() | ||
val events = mEvents.asSharedFlow() | ||
|
||
suspend fun publishEvent(event: IEvent) { | ||
mEvents.emit(event) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
sample/shared/src/commonMain/kotlin/com/kevinnzou/sample/eventbus/IEvent.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,6 @@ | ||
package com.kevinnzou.sample.eventbus | ||
|
||
/** | ||
* Created By Kevin Zou On 2023/12/16 | ||
*/ | ||
interface IEvent |
6 changes: 6 additions & 0 deletions
6
sample/shared/src/commonMain/kotlin/com/kevinnzou/sample/eventbus/NavigationEvent.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,6 @@ | ||
package com.kevinnzou.sample.eventbus | ||
|
||
/** | ||
* Created By Kevin Zou On 2023/12/15 | ||
*/ | ||
class NavigationEvent : IEvent |
Oops, something went wrong.