This file contains hidden or 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
| fun processPayment( | |
| order: Order, | |
| analytics: AnalyticsContext // the uninvited guest at every layer | |
| ) { | |
| analytics.track("payment_started", order.id) | |
| } |
This file contains hidden or 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
| data class AnalyticsContext( | |
| val sessionId: String, | |
| val experimentVariant: String, | |
| ) { | |
| fun track(event: String, payload: Any) { /* ... */ } | |
| } | |
| context(analytics: AnalyticsContext) | |
| fun processPayment(order: Order) { | |
| analytics.track("payment_started", order.id) |
This file contains hidden or 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
| interface Logger { | |
| fun log(message: String) | |
| } | |
| context(logger: Logger) | |
| fun fetchUserData(userId: String): User { | |
| logger.log("Fetching user: $userId") | |
| // actual fetch logic | |
| return User(userId) | |
| } |
This file contains hidden or 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
| data class MyState(val counter: Int = 0) | |
| class MyViewModel : ViewModel() { | |
| private val _state = MutableStateFlow(MyState()) | |
| val state: StateFlow<MyState> = with(viewModelScope) { | |
| _state | |
| .onStart { /* warm-up logic */ } | |
| .stateInWhileSubscribed(initialValue = MyState()) |
This file contains hidden or 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
| context(scope: CoroutineScope) | |
| fun <T> Flow<T>.stateInWhileSubscribed(initialValue: T): StateFlow<T> = | |
| stateIn( | |
| scope, | |
| SharingStarted.WhileSubscribed(stopTimeoutMillis = 5_000L), | |
| initialValue | |
| ) |
This file contains hidden or 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
| fun <T> ViewModel.stateInWhileSubscribed( | |
| initialValue: T | |
| ): StateFlow<T> = stateIn( | |
| viewModelScope, | |
| SharingStarted.WhileSubscribed(stopTimeoutMillis = 5_000L), | |
| initialValue | |
| ) |