# Flows: StateFlow or distinct+conflated?

**URL:** https://discuss.kotlinlang.org/t/flows-stateflow-or-distinct-conflated/18526
**Category:** Support
**Created:** [July 22, 2020, 6:17pm UTC](https://discuss.kotlinlang.org/t/flows-stateflow-or-distinct-conflated/18526 "2020-07-22T18:17:46Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![benjaminhill](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@benjaminhill](https://discuss.kotlinlang.org/u/benjaminhill)
#### Post date: [July 22, 2020, 6:17pm UTC](https://discuss.kotlinlang.org/t/flows-stateflow-or-distinct-conflated/18526/1 "2020-07-22T18:17:46Z")

</div>

I’ve got a bizarre file that is always the same size, with the same modified date. (some driver showing its state as a file’s contents through a fake filesystem). It contains a short string that changes when the device’s state changes. I don’t think I can [Watch](https://docs.oracle.com/javase/tutorial/essential/io/notification.html) the file because it is a fake filesystem. Cool so far!

I was hoping to modernize it! A changing file that may or may not have anyone listening feels like a flow.

I’m not entirely sure about **[distinctUntilChanged](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/distinct-until-changed.html)** and **[conflate](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/conflate.html)** (do they play nice together?) and if I’m just reinventing a **[StateFlow](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/)**?

```kotlin
fun fileChanges(file: File): Flow<String> = flow {
    while (true) {
        emit(file.readText(charset))
        delay(1)
    }
}
    .flowOn(Dispatchers.IO) // Run in background
    .distinctUntilChanged() // no duplicate status
    .conflate() // only most recent
    .map { it.trim() } // just the one line without newlines

```

This seems to work (with the expected system overhead) and I’m only trimming the deduped values, but… it smells funny. Like I should be able to reuse file handles, or memory mapped stuff, or a mark()/reset(), or better still, wrap it all up in a [StateFlow](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/) object.
