Slackbot
12/20/2018, 4:45 PMAllan Wang
12/21/2018, 12:17 AMdoA {
doB {
doC()
}
}
And I want to make them all in the same level, would it be done like the following?
// Original
fun doA(continuation: () -> Unit) {
// some action
continuation()
}
// New
suspend fun doA() = suspendCoroutine<Unit> {
// some action
it.resume(Unit)
}
Then I do something like
launch {
doA()
doB()
doC()
}
Which should occur one after the other by default.
My goal is just to remove the nested brackets. The reason I have continuation to begin with is that the actions I’m running deal with callbacks.
Is there also a benefit to doing some potential safety checks before calling suspendCoroutine vs putting them all inside that wrapper? The delay function seems to do that.Allan Wang
12/21/2018, 12:46 AM// Original
private fun setWebCookie(cookie: String?, callback: (() -> Unit)?) {
with(CookieManager.getInstance()) {
removeAllCookies { _ ->
if (cookie == null) {
callback?.invoke()
return@removeAllCookies
}
L.d { "Setting cookie" }
val cookies = cookie.split(";").map { Pair(it, SingleSubject.create<Boolean>()) }
cookies.forEach { (cookie, callback) -> setCookie(FB_URL_BASE, cookie) { callback.onSuccess(it) } }
Observable.zip<Boolean, Unit>(cookies.map { (_, callback) -> callback.toObservable() }) {}
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
callback?.invoke()
L.d { "Cookies set" }
L._d { cookie }
flush()
}
}
}
}
// With suspension
private suspend fun suspendSetWebCookie(cookie: String?) {
cookie ?: return
val manager = CookieManager.getInstance()
manager.removeAllCookies()
cookie.split(":").forEach {
manager.setSingleWebCookie(it)
}
}
private suspend fun CookieManager.removeAllCookies(): Boolean = suspendCoroutine { cont ->
removeAllCookies {
cont.resume(it)
}
}
private suspend fun CookieManager.setSingleWebCookie(cookie: String): Boolean = suspendCoroutine { cont ->
setCookie(FB_URL_BASE, cookie) {
cont.resume(it)
}
}
The use of RX initially was slight overkill, given there’s no thread switching or anything really asynchronous in my logic. I’m going to ignore the error handling for now.pakoito
12/21/2018, 1:11 AMpakoito
12/21/2018, 1:12 AMgiven there’s no thread switching or anything really asynchronous in my logiclooks overkill here too unless you have something that does IO in removeAllCookies and setCookie
Kulwinder Singh
12/21/2018, 9:22 AMvideosList.forEachIndexed { index, video: BaseVideo ->
val userData = getUserData(video)*//calling suspend function*
listWithData[index].userData = userData
}
obobo
12/21/2018, 1:43 PMdecorateCheckedSupplier(circuitBreaker) {
suspendingFunction() // compiler complains
}
How would I handle this? I've realized that I can wrap the suspendingFunction
call in an async
block, but then the circuitbreaker returns a Deferred
and always succeeds, which is not what I want. What I want, I think, is to have be able to call a suspending function from an arbitrary lambda.Marcelo Velloso
12/21/2018, 2:40 PMVsevolod Tolstopyatov [JB]
12/21/2018, 4:11 PMkotlinx.coroutines
version 1.1.0 is here!
Changelog (compared to 1.1.0-alpha):
* Kotlin updated to 1.3.11.
* Resumes of CancellableContinuation
in the final state now produce IllegalStateException
. This change does not affect previous CancellableContinuation
improvement, races between resume and cancellation do not lead to exceptional situation.
* runBlocking
is integrated with Dispatchers.Unconfined
. Now nested runBlocking
and unconfined calls do not hang, but share the same event loop.itnoles
12/21/2018, 4:14 PMJurriaan Mous
12/21/2018, 9:31 PMdewildte
12/22/2018, 7:18 PMtyped-actors
branch is 394 commits behind master.
Should I make a fork?gmariotti
12/25/2018, 9:24 AM@Test
fun `test failure event method`(testContext: TestContext) {
GlobalScope.launch(vertx.dispatcher()) {
val async = testContext.async()
val cause = RuntimeException()
try {
awaitEvent<Any> { throw cause }
testContext.fail()
} catch (e: Exception) {
testContext.assertEquals(cause, e)
}
async.complete()
}
}
with awaitEvent
implemented in this way:
suspend fun <T> awaitEvent(block: (h: Handler<T>) -> Unit): T {
return suspendCancellableCoroutine { cont: CancellableContinuation<T> ->
try {
block.invoke(Handler { t ->
cont.resume(t)
})
} catch (e: Exception) {
cont.resumeWithException(e)
}
}
}
and now this test pass only if I do testContext.assertEquals(cause, e.cause)
goldin
12/25/2018, 9:43 PMChannel
. Where is my error?bdawg.io
12/26/2018, 4:38 AMfuture
cancel the parent Job
if the block fails exceptionally? (same behavior as async
)Allan Wang
12/26/2018, 4:57 AMhttps://www.youtube.com/watch?v=a3agLJQ6vt8▾
select
? That is one of the differences here compared to the example in the video
• Is the usage of launch
within the main fetch
method acceptable? It is there purely to send an event to a channel while allowing me to use the coroutine continuation
• Is it acceptable that all the maps are global. In the example, they are all within the launch
code. I’m not sure if this affect access from a different thread or not.kenkyee
12/26/2018, 12:31 PMmarcoferrer
12/26/2018, 4:10 PMlouiscad
12/26/2018, 5:38 PMrace
function that would take a list of Deferred
, await for the single fastest (biased or not, doesn't matter), cancel all the "losers", and return the value of the winner. Did someone already solve this problem, or has a clue on how to make this? Thank you!Rohan Maity
12/26/2018, 8:14 PMbreandan
12/27/2018, 5:11 AMAllan Wang
12/27/2018, 6:07 AMlaunch
to launch(<http://Dispatchers.IO|Dispatchers.IO>)
, then the loops each print 3 times. What’s going on here? In production, would I be expected to specify a different context if I want the loops to produce results? Was adding the loops in separate launches not enough?dave08
12/27/2018, 1:04 PMclass Foo { suspend fun execute(): Bar }
and I want to limit that execute
should not run in parallel is the simplest way (without channels) to use newSingleThreadContext
to create one thread context in the class and use it in withContext(executeContext)
in execute
? Is there anything wrong with this, or a better way?Romanow
12/27/2018, 3:03 PMCoroutineScheduler
? I'm trying to use coroutines from a webapp, where kotlin is loaded by the webapp classloader, so I need to cleanup all resources that I allocated. For this I have my own ExecutorCoroutineDispatcher
that is used trough the code based on a internal ScheduledThreadPoolExecutor
. But this seems to still startup some default thread pools...zak.taccardi
12/27/2018, 7:20 PMactor
that I can cancel and restart. Is that possible?simon.vergauwen
12/27/2018, 8:29 PM1.0.0
to 1.1.0
, this seemed to be caused by coroutines changing the exception instance under the hood. However this only occurs test packages. Is this expected behavior?zak.taccardi
12/27/2018, 9:25 PMval actor = scope.actor<StartNewJobCommand> {
var fatherJob: Job? = null
for (startNewJobCommand in channel) {
// cancel old father AND both his children if they are still running
fatherJob?.cancelAndJoin() // wait to start new job until old job is complete
fatherJob = launch {
val son1 = async {
// ..
// completion of this Deferred should cancel fatherJob and son2's job
}
val son2 = async {
// ..
// completion of this Deferred should cancel fatherJob and son1's job
}
// fatherJob should not complete until his two sons have finished their work
// note - only one of the two children need to finish
son1.await()
son2.await()
}
}
}
groostav
12/27/2018, 11:30 PMonCompletionInternal
et-al will either be made public or disappear? I presume there's some work involving JobSupport
that needs to happen. I'm trying to write my own class that extends AbstractCoroutine
, but I'm having a real difficult time.Rohan Maity
12/28/2018, 3:45 AMasad.awadia
12/28/2018, 5:40 PMasad.awadia
12/28/2018, 5:40 PMlouiscad
12/30/2018, 5:52 PMelizarov
01/06/2019, 9:51 PM