Scoop API

scoop:scoop.http.asyncio:0.1.0

src/websocket/source_operation.scoop

Selected production source · src/websocket/source_operation.scoop

1package scoop.http.asyncio
3private class WsSourceOperation(val state: WsState) {
4 var active: Boolean = true
5}
7internal suspend fun <T> runWsSource(state: WsState, block: suspend () -> T): T {
8 val operation = WsSourceOperation(state)
9 try {
10 return context(operation) {
11 coroutineScope<T> {
12 val scope = this
14 val closing =
15 state.lock.withLock {
16 state.sourceJob = Some(scope.job);
17 state.closing
18 }
19 if (closing)
20 scope.job.cancel()
21 ensureActive()
22 block()
23 }
24 }
25 } finally {
26 state.lock.withLock {
27 state.sourceJob = None;
28 operation.active = false
29 }
30 }
33internal fun checkWsCloseContext(state: WsState) {
34 val operation = contextOrNone<WsSourceOperation>()
35 if (
36 present(operation) &&
37 operation!!.state ===
38 state &&
39 state.lock.withLock { operation!!.active }
40 )
41 throw IllegalStateException(
42 Some("cannot await WebSocket closure from its active send source")
43 )