2022-07-05 22:31:44 +00:00
|
|
|
package app
|
2019-06-21 21:50:43 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-03 23:32:48 +00:00
|
|
|
"cwtch.im/cwtch/model/attr"
|
2021-10-15 19:38:22 +00:00
|
|
|
"cwtch.im/cwtch/model/constants"
|
2019-06-21 21:50:43 +00:00
|
|
|
"cwtch.im/cwtch/peer"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-07-22 20:49:23 +00:00
|
|
|
// WaitGetPeer is a helper function for utility apps not written using the event bus
|
2019-06-21 21:50:43 +00:00
|
|
|
// Proper use of an App is to call CreatePeer and then process the NewPeer event
|
|
|
|
// however for small utility use, this function which polls the app until the peer is created
|
|
|
|
// may fill that usecase better
|
2022-07-05 22:31:44 +00:00
|
|
|
func WaitGetPeer(app Application, name string) peer.CwtchPeer {
|
2021-06-02 18:34:57 +00:00
|
|
|
for {
|
2021-10-15 19:38:22 +00:00
|
|
|
for _, handle := range app.ListProfiles() {
|
|
|
|
peer := app.GetPeer(handle)
|
2021-10-26 21:50:05 +00:00
|
|
|
localName, _ := peer.GetScopedZonedAttribute(attr.PublicScope, attr.ProfileZone, constants.Name)
|
2021-05-03 23:32:48 +00:00
|
|
|
if localName == name {
|
|
|
|
return peer
|
2019-06-21 21:50:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|