scoop:scoop.http.asyncio:0.1.0
src/websocket/source_operation.scoop
Selected production source · src/websocket/source_operation.scoop
1
package scoop.http.asyncio3
private class WsSourceOperation(val state: WsState) {4
var active: Boolean = true5
}7
internal 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 = this14
val closing =15
state.lock.withLock {16
state.sourceJob = Some(scope.job);17
state.closing18
}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 = false29
}30
}31
}33
internal 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
)44
}