hey there :^) I used to have this observable whic...
# android
m
hey there :^) I used to have this observable which I was very proud of that was synced to the display's refresh rate:
Copy code
val vsync: Observable<Long> by lazy {
    var cb: Choreographer.FrameCallback? = null
    Observable.create<Long> {emi ->
        cb = Choreographer.FrameCallback {
            emi.onNext(it)
            Choreographer.getInstance().postFrameCallback(cb)
        }
        Choreographer.getInstance().postFrameCallback(cb)
    }
        .subscribeOn(AndroidSchedulers.mainThread())
        .share()
}
what would be the proper way to turn it into a flow? considering flows are suspended but I need to call the choreographer from the main thread
e
Copy code
val vsync = callbackFlow {
    val choreographer = Choreographer.getInstance()
    val cb = object : Choreographer.FrameCallback {
        override fun doFrame(frameTimeMillis: Long) {
            val result = trySend(FrameTimeMillis)
            if (!result.isClosed) choreographer.postFrameCallback(this)
        }
    }
    choreographer.postFrameCallback(cb)
    awaitClose {
        choreographer.removeFrameCallback(cb)
    }
}
    .buffer(onBufferOverflow = BufferOverflow.DROP_OLDEST)
    .flowOn(Dispatchers.Main)
❤️ 1
m
I am in ecstasy just by reading all of this. makes me so excited coming back into android development! thank you so much!!