https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

Jonathan Walsh

03/29/2019, 11:31 PM
I'm using
CoroutineStart.UNDISPATCHED
to help write
CompletableFuture
alternate methods to my suspending functions for use by Java. But I see its marked as
@ExperimentalCoroutinesApi
. Any recommendations / guidelines/ alternatives to using it? Or just be ready to uptake changes? Will post my specific example this thread
Copy code
class SyncThing(dispatcher: CoroutineDispatcher) : CoroutineScope {

        private val parentJob = Job()
        override val coroutineContext: CoroutineContext = dispatcher + parentJob
        
        private var lastProcessedTxnId: Long = 0

        fun asyncSyncTo(txnId: Long): CompletableFuture<Long> {
            // start UNDISPATCHED so that if the subscription is already up to the ForToRequest we can return immediately
            // Specifically if we have another long running syncTo request and this request is already satisfied we can
            // return immediately rather then waiting for the long running syncTo to finish
            return future(start = CoroutineStart.UNDISPATCHED) {
                suspendSyncTo(txnId)
            }
        }

        suspend fun suspendSyncTo(txnId: Long): Long {
            return when {
                // already processed through requested txnId
                txnId <= lastProcessedTxnId -> lastProcessedTxnId

                // Some other cases

                // haven't processed through the requested txnId, do it now
                else -> executeSync(txnId)
            }
        }

        private suspend fun executeSync(txnId: Long) : Long {
            TODO("other stuff which may suspsend")
        }
    }