generated from InsanusMokrassar/KotlinMultiplatformProjectTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewModel.kt
28 lines (25 loc) · 905 Bytes
/
ViewModel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package dev.inmo.navigation.mvvm
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.navigation.core.NavigationNode
import dev.inmo.navigation.core.configs.NavigationNodeDefaultConfig
import dev.inmo.navigation.core.onDestroyFlow
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
/**
* This is an abstract class of ViewModel for MVVM pattern.
*
* It has its own scope bind to [NavigationNode] lifecycle: when [NavigationNode] will be destroyed, its
* [scope] will be cancelled
*/
abstract class ViewModel<T>(
node: NavigationNode<out T, T>
) {
val scope: CoroutineScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
init {
node.onDestroyFlow.subscribeSafelyWithoutExceptions(scope) {
scope.cancel()
}
}
}