2023-09-25 19:52:57 +03:00
|
|
|
package tg
|
|
|
|
|
|
|
|
// The type represents general update channel.
|
|
|
|
type UpdateChan struct {
|
|
|
|
chn chan *Update
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return new update channel.
|
|
|
|
func NewUpdateChan() *UpdateChan {
|
|
|
|
ret := &UpdateChan{}
|
|
|
|
ret.chn = make(chan *Update)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (updates *UpdateChan) Chan() chan *Update {
|
|
|
|
return updates.chn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send an update to the channel.
|
|
|
|
// Returns true if the update was sent.
|
|
|
|
func (updates *UpdateChan) Send(u *Update) bool {
|
2023-09-28 08:15:57 +03:00
|
|
|
defer recover()
|
2023-09-25 19:52:57 +03:00
|
|
|
if updates == nil || updates.chn == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
updates.chn <- u
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read an update from the channel.
|
|
|
|
func (updates *UpdateChan) Read() *Update {
|
|
|
|
if updates == nil || updates.chn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return <-updates.chn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the channel is closed.
|
|
|
|
func (updates *UpdateChan) Closed() bool {
|
|
|
|
return updates==nil || updates.chn == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the channel. Used in defers.
|
|
|
|
func (updates *UpdateChan) Close() {
|
|
|
|
if updates == nil || updates.chn == nil {
|
|
|
|
return
|
|
|
|
}
|
2023-09-28 08:15:57 +03:00
|
|
|
chn := updates.chn
|
2023-09-25 19:52:57 +03:00
|
|
|
updates.chn = nil
|
2023-09-28 08:15:57 +03:00
|
|
|
close(chn)
|
2023-09-25 19:52:57 +03:00
|
|
|
}
|
|
|
|
|