Mikel Pérez
06/19/2024, 3:37 AMval 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 threadephemient
06/19/2024, 4:19 AMval 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)
Mikel Pérez
06/19/2024, 6:46 AM