/* * iOS-specific ViewModel that lives in iosMain of common module */ class KSearchViewModel( exitDb: KExitDatabase ) : CoroutineScope { private val job = Job() override val coroutineContext: CoroutineContext = dispatcher() + job @InternalCoroutinesApi private val searchStateMachine = SearchStateMachine(this, exitDb) val cStateFlow = searchStateMachine.viewState.wrap() fun dispatchAction(action: Search.Action) { searchStateMachine.dispatchAction.invoke(action) } @UseExperimental(ExperimentalCoroutinesApi::class) fun clear() { job.cancel() } } fun ConflatedBroadcastChannel.wrap(): CFlow = CFlow(asFlow()) fun Flow.wrap(): CFlow = CFlow(this) class CFlow(private val origin: Flow) : Flow by origin { fun watch(block: (T) -> Unit): Closeable { val job = Job() onEach { block(it) }.launchIn(CoroutineScope(Dispatchers.Main + job)) return object : Closeable { override fun close() { job.cancel() } } } }