-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create StateFlowWrapper for multiplatform use (#254)
- Loading branch information
1 parent
06ab763
commit ebf4db0
Showing
66 changed files
with
654 additions
and
911 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
plugins { | ||
kotlin("multiplatform") | ||
} | ||
|
||
configureJvmTargets() | ||
|
||
kotlin { | ||
sourceSets { | ||
commonMain.dependencies { | ||
api(libs.kotlin.coroutines.core) | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
core/flow/src/commonMain/kotlin/br/alexandregpereira/flow/FlowWrapper.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,41 @@ | ||
package br.alexandregpereira.flow | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
|
||
class StateFlowWrapper<T : Any>( | ||
private val origin: StateFlow<T>, | ||
private val coroutineScope: CoroutineScope, | ||
) : StateFlow<T> by origin { | ||
|
||
fun subscribe(block: (T) -> Unit) { | ||
origin.onEach(block).launchIn(coroutineScope) | ||
} | ||
} | ||
|
||
class SharedFlowWrapper<T : Any>( | ||
private val origin: SharedFlow<T>, | ||
private val coroutineScope: CoroutineScope, | ||
) : SharedFlow<T> by origin { | ||
|
||
fun subscribe(block: (T) -> Unit) { | ||
origin.onEach(block).launchIn(coroutineScope) | ||
} | ||
} | ||
|
||
fun <T : Any> StateFlow<T>.wrap( | ||
coroutineScope: CoroutineScope | ||
): StateFlowWrapper<T> = StateFlowWrapper( | ||
origin = this, | ||
coroutineScope = coroutineScope | ||
) | ||
|
||
fun <T : Any> SharedFlow<T>.wrap( | ||
coroutineScope: CoroutineScope | ||
): SharedFlowWrapper<T> = SharedFlowWrapper( | ||
origin = this, | ||
coroutineScope = coroutineScope | ||
) |
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
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
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
Oops, something went wrong.