scoop:scoop.window.asyncio:0.1.0
src/application.scoop
Selected production source · src/application.scoop
1
package scoop.window.asyncio3
import scoop.asyncio.*4
import scoop.window.*5
import scoop.window.native.*7
/** Runs window business callbacks in the attached asyncio root scope until both owners close. */8
public fun Application.runWithAsyncio(handler: ApplicationHandler) {9
@Unsafe10
{11
runAttached(handler) {12
host ->13
val attachment = WindowAttachment(host)14
attachment.install()15
attachment16
}17
}18
}20
private class WindowAttachment(private val host: ApplicationHost): ApplicationAttachment {21
private var session: EventLoopSession? = None22
private var cancellation: Disposable? = None24
fun install() {25
val value = eventLoop().attach(WindowLoopHost(host)) { error -> closed(error) }26
session = Some(value)27
value.post {28
val scope = this;29
val attachment = this @WindowAttachment30
attachment.cancellation =31
Some(32
scope.job.invokeOnCancellation {33
attachment.host.postControl { attachment.host.requestExit() }34
}35
)36
}37
}39
public override fun post(block: () -> Unit): Boolean =40
session!!.post {41
val scope = this;42
context(ScopeBinding(scope)) {43
block()44
}45
}47
public override fun requestClose() {48
session!!.requestClose()49
}51
private fun closed(error: Throwable?) {52
var result = error53
try {54
when (cancellation) {55
case Some(val value) -> value.dispose();56
case None -> {}57
}58
} catch (cleanup: Throwable) {59
result =60
Some(61
when (error) {62
case Some(val primary) -> ResourceUseException(primary, cleanup);63
case None -> cleanup64
}65
)66
} finally {67
cancellation = None68
}69
host.complete(result)70
}71
}