scoop:scoop.surface:0.1.0
src/shared/input/contact_claim.scoop
Selected production source · src/shared/input/contact_claim.scoop
1
package scoop.surface3
import scoop.window.ContactId5
private class ContactClaimChange(6
val route: InputContactRoute,7
val previous: Array<InputScope>,8
val next: Array<InputScope>9
)11
/** Atomically claims active contacts whose original paths contain this scope. */12
public fun InputScope.claimContacts(ids: Array<ContactId>): Boolean {13
checkOpen()14
requireInput(ids.size > 0L, "contact claim requires at least one identity")15
for (index in 0L ..< ids.size)16
for (previous in 0L ..< index)17
requireInput(ids[index] != ids[previous], "contact claim identities must be unique")18
return owner.claimContacts(this, ids)19
}21
/** Cancels only this scope's ownership of the specified contact. */22
public fun InputScope.cancelContactClaim(id: ContactId) {23
owner.checkThread()24
if (closed)25
return26
val route = owner.findContact(id) ?: return27
if (sameScope(route.claimant, Some(this)))28
owner.dispatch {29
owner.cancelContact(route, InputCancelReason.ClaimCancelled, inputObservation())30
}31
}33
internal fun InputRouter.claimContacts(scope: InputScope, ids: Array<ContactId>): Boolean {34
val changes = ArrayList<ContactClaimChange>()35
for (id in ids) {36
val route = findContact(id) ?: return rejectContactClaim()37
if (route.ending || !contactUsable(route))38
return rejectContactClaim()39
when (route.claimant) {40
case Some(val current) -> if (current !== scope)41
return rejectContactClaim()42
case None -> {43
val previous = route.scopes44
var offset = 0L45
while (offset < previous.size && previous[offset] !== scope)46
offset += 1L47
if (offset == previous.size)48
return rejectContactClaim()49
val start = offset50
val next =51
Array<InputScope>(previous.size - start) { index -> previous[start + index] }52
changes.add(ContactClaimChange(route, previous, next))53
}54
}55
}56
// Commit every owner before a cancellation callback can inspect another contact.57
for (change in changes) {58
change.route.claimant = Some(scope)59
change.route.scopes = change.next60
}61
val time = inputObservation()62
var failure: Throwable? = None63
for (change in changes)64
for (previous in change.previous) {65
if (previous === scope)66
break67
failure =68
cleanupFailure(failure) {69
notify(70
previous,71
InputEvent.Contact(72
InputContactEvent.Cancelled(73
change.route.latest.id,74
InputCancelReason.Claimed,75
time76
)77
),78
time,79
Some(change.previous[0L]),80
terminal = true81
)82
}83
}84
// Activity observers may invalidate one owner during the normal drain.85
failure = cleanupFailure(failure) { dispatch {} }86
var intact = !closed87
for (id in ids) {88
val route = findContact(id)89
intact =90
intact &&91
when (route) {92
case Some(val current) -> sameScope(current.claimant, Some(scope)) &&93
contactUsable(current)94
case None -> false95
}96
}97
val failed =98
when (failure) {99
case Some(_) -> true;100
case None -> false101
}102
if (!intact || failed) {103
for (change in changes)104
failure =105
cleanupFailure(failure) {106
cancelContact(change.route, InputCancelReason.ClaimCancelled, time)107
}108
failure = cleanupFailure(failure) { dispatch {} }109
throwFailure(failure)110
return false111
}112
return true113
}115
private fun InputRouter.rejectContactClaim(): Boolean {116
dispatch {}117
return false118
}