scoop:scoop.window:0.1.0
src/native/watch.scoop
Selected production source · src/native/watch.scoop
1
package scoop.window.native3
import scoop.window.*5
/** A borrowed native notification, retired asynchronously on the owner thread. */6
public class ApplicationWatch internal constructor(7
private val owner: ApplicationOwner,8
internal val identity: Long,9
private val pointer: Ptr<Unit>,10
private val ready: () -> Unit11
) {12
internal var removing: Boolean = false13
private var onRemoved: (() -> Unit)? = None14
/** Cancels once; completion follows every queued or active ready callback. */15
public fun remove(onRemoved: () -> Unit) {16
owner.checkThread()17
if (removing)18
return19
removing = true20
this.onRemoved = Some(onRemoved)21
@Unsafe22
{23
nativeWatchRemove(pointer)24
}25
}26
internal fun notifyReady() {27
if (!removing)28
ready()29
}30
internal fun notifyRemoved() {31
onRemoved!!();32
onRemoved = None33
}34
}36
@Unsafe37
internal fun ApplicationOwner.registerWatch(38
create: (Long) -> NativeWatchResult,39
ready: () -> Unit40
): ApplicationWatch {41
checkThread()42
if (exiting || native == None)43
throw WindowException("install notification", WindowErrorKind.InvalidState)44
if (nextWatch == 9223372036854775807L)45
throw WindowException("notification identity", WindowErrorKind.NativeFailure)46
val identity = nextWatch47
nextWatch += 1L48
val result = create(identity)49
val pointer = result.pointer50
val error = result.error51
val watch =52
ApplicationWatch(53
this,54
identity,55
pointer ?:56
throw WindowException(57
"install notification",58
WindowErrorKind.NativeFailure,59
nativeCode = Some(error)60
),61
ready62
)63
watches.add(watch)64
return watch65
}67
internal fun ApplicationOwner.dispatchWatch(identity: Long, stage: Int) {68
var index = 0L69
while (index < watches.size) {70
val watch = watches[index]71
if (watch.identity == identity) {72
if (stage == 2)73
watch.notifyReady()74
else75
if (stage == 3) {76
watches.removeAt(index)77
watch.notifyRemoved()78
mailbox.wake()79
} else80
throw WindowException(81
"unknown notification stage",82
WindowErrorKind.NativeFailure83
)84
return85
}86
index += 1L87
}88
}90
@CLayout91
internal struct NativeWatchResult(val pointer: Ptr<Unit>?, val error: Long)93
@Extern(name = "scoop_window_watch_remove")94
private fun nativeWatchRemove(watch: Ptr<Unit>)