Skip to content

Commit

Permalink
code refactorings.
Browse files Browse the repository at this point in the history
  • Loading branch information
AmarnathCJD committed Aug 9, 2024
1 parent bbf698e commit e341be9
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 305 deletions.
36 changes: 17 additions & 19 deletions telegram/conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import (

const ConvDefaultTimeOut = int32(60)

type handle interface{}

// Conversation is a struct for conversation with user.
type Conversation struct {
Client *Client
Peer InputPeer
isPrivate bool
timeOut int32
openH []handle
openH []Handle
lastMsg *NewMessage
}

Expand Down Expand Up @@ -99,13 +97,13 @@ func (c *Conversation) GetResponse() (*NewMessage, error) {

h := c.Client.On(OnMessage, waitFunc, filters...)

c.openH = append(c.openH, &h)
c.openH = append(c.openH, h)
select {
case <-time.After(time.Duration(c.timeOut) * time.Second):
go c.removeHandle(&h)
go c.removeHandle(h)
return nil, fmt.Errorf("conversation timeout: %d", c.timeOut)
case m := <-resp:
go c.removeHandle(&h)
go c.removeHandle(h)
return m, nil
}
}
Expand All @@ -131,13 +129,13 @@ func (c *Conversation) GetEdit() (*NewMessage, error) {
}

h := c.Client.On(OnEdit, waitFunc, filters...)
c.openH = append(c.openH, &h)
c.openH = append(c.openH, h)
select {
case <-time.After(time.Duration(c.timeOut) * time.Second):
go c.removeHandle(&h)
go c.removeHandle(h)
return nil, fmt.Errorf("conversation timeout: %d", c.timeOut)
case m := <-resp:
go c.removeHandle(&h)
go c.removeHandle(h)
return m, nil
}
}
Expand Down Expand Up @@ -165,13 +163,13 @@ func (c *Conversation) GetReply() (*NewMessage, error) {
filters = append(filters, FilterReply)

h := c.Client.On(OnMessage, waitFunc, filters...)
c.openH = append(c.openH, &h)
c.openH = append(c.openH, h)
select {
case <-time.After(time.Duration(c.timeOut) * time.Second):
go c.removeHandle(&h)
go c.removeHandle(h)
return nil, fmt.Errorf("conversation timeout: %d", c.timeOut)
case m := <-resp:
go c.removeHandle(&h)
go c.removeHandle(h)
return m, nil
}
}
Expand All @@ -192,13 +190,13 @@ func (c *Conversation) WaitEvent(ev *Update) (Update, error) {
}

h := c.Client.On(*ev, waitFunc)
c.openH = append(c.openH, &h)
c.openH = append(c.openH, h)
select {
case <-time.After(time.Duration(c.timeOut) * time.Second):
go c.removeHandle(&h)
go c.removeHandle(h)
return nil, fmt.Errorf("conversation timeout: %d", c.timeOut)
case u := <-resp:
go c.removeHandle(&h)
go c.removeHandle(h)
return u, nil
}
}
Expand All @@ -215,19 +213,19 @@ func (c *Conversation) WaitRead() (*UpdateReadChannelInbox, error) {
}

h := c.Client.On(&UpdateReadChannelInbox{}, waitFunc)
c.openH = append(c.openH, &h)
c.openH = append(c.openH, h)

select {
case <-time.After(time.Duration(c.timeOut) * time.Second):
go c.removeHandle(&h)
go c.removeHandle(h)
return nil, fmt.Errorf("conversation timeout: %d", c.timeOut)
case u := <-resp:
go c.removeHandle(&h)
go c.removeHandle(h)
return u, nil
}
}

func (c *Conversation) removeHandle(h handle) {
func (c *Conversation) removeHandle(h Handle) {
for i, v := range c.openH {
if v == h {
c.openH = append(c.openH[:i], c.openH[i+1:]...)
Expand Down
Loading

0 comments on commit e341be9

Please sign in to comment.