cwtch/app/utils/utils.go

27 lines
720 B
Go
Raw Normal View History

package utils
import (
app2 "cwtch.im/cwtch/app"
2021-05-03 23:32:48 +00:00
"cwtch.im/cwtch/model/attr"
"cwtch.im/cwtch/peer"
"time"
)
// WaitGetPeer is a helper function for utility apps not written using the event bus
// 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
func WaitGetPeer(app app2.Application, name string) peer.CwtchPeer {
for true {
2021-05-03 23:32:48 +00:00
for id := range app.ListPeers() {
peer := app.GetPeer(id)
localName, _ := peer.GetAttribute(attr.GetLocalScope("name"))
if localName == name {
return peer
}
}
time.Sleep(100 * time.Millisecond)
}
return nil
}