Scoop API

scoop:scoop.window.asyncio:0.1.0

src/application.scoop

Selected production source · src/application.scoop

1package scoop.window.asyncio
3import scoop.asyncio.*
4import scoop.window.*
5import scoop.window.native.*
7/** Runs window business callbacks in the attached asyncio root scope until both owners close. */
8public fun Application.runWithAsyncio(handler: ApplicationHandler) {
9 @Unsafe
10 {
11 runAttached(handler) {
12 host ->
13 val attachment = WindowAttachment(host)
14 attachment.install()
15 attachment
16 }
17 }
20private class WindowAttachment(private val host: ApplicationHost): ApplicationAttachment {
21 private var session: EventLoopSession? = None
22 private var cancellation: Disposable? = None
24 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 @WindowAttachment
30 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 = error
53 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 -> cleanup
64 }
65 )
66 } finally {
67 cancellation = None
68 }
69 host.complete(result)
70 }