Scoop API

scoop:scoop.surface:0.1.0

src/shared/input/contact_claim.scoop

Selected production source · src/shared/input/contact_claim.scoop

1package scoop.surface
3import scoop.window.ContactId
5private 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. */
12public 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)
21/** Cancels only this scope's ownership of the specified contact. */
22public fun InputScope.cancelContactClaim(id: ContactId) {
23 owner.checkThread()
24 if (closed)
25 return
26 val route = owner.findContact(id) ?: return
27 if (sameScope(route.claimant, Some(this)))
28 owner.dispatch {
29 owner.cancelContact(route, InputCancelReason.ClaimCancelled, inputObservation())
30 }
33internal 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.scopes
44 var offset = 0L
45 while (offset < previous.size && previous[offset] !== scope)
46 offset += 1L
47 if (offset == previous.size)
48 return rejectContactClaim()
49 val start = offset
50 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.next
60 }
61 val time = inputObservation()
62 var failure: Throwable? = None
63 for (change in changes)
64 for (previous in change.previous) {
65 if (previous === scope)
66 break
67 failure =
68 cleanupFailure(failure) {
69 notify(
70 previous,
71 InputEvent.Contact(
72 InputContactEvent.Cancelled(
73 change.route.latest.id,
74 InputCancelReason.Claimed,
75 time
76 )
77 ),
78 time,
79 Some(change.previous[0L]),
80 terminal = true
81 )
82 }
83 }
84 // Activity observers may invalidate one owner during the normal drain.
85 failure = cleanupFailure(failure) { dispatch {} }
86 var intact = !closed
87 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 -> false
95 }
96 }
97 val failed =
98 when (failure) {
99 case Some(_) -> true;
100 case None -> false
101 }
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 false
111 }
112 return true
115private fun InputRouter.rejectContactClaim(): Boolean {
116 dispatch {}
117 return false