Skip to content

Commit

Permalink
ListenFor()
Browse files Browse the repository at this point in the history
  • Loading branch information
sdfsdhgjkbmnmxc committed Mar 15, 2021
1 parent e29ab35 commit 2f7f507
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ func (s *Session) Ws(team string, onfail func(error)) (*WsSession, error) {
}

w := &WsSession{
session: s,
team: team,
conn: conn,
inbox: make(chan serverEvent, defaultSize),
outBytes: make(chan []byte, defaultSize),
fail: make(chan error),
session: s,
team: team,
conn: conn,
inbox: make(chan serverEvent, defaultSize),
outBytes: make(chan []byte, defaultSize),
listeners: make(map[string]chan []byte),
fail: make(chan error),
}

go func() {
Expand All @@ -66,13 +67,14 @@ type serverEvent struct {
}

type WsSession struct {
session *Session
team string
conn *websocket.Conn
closed bool
inbox chan serverEvent
outBytes chan []byte
fail chan error
session *Session
team string
conn *websocket.Conn
closed bool
inbox chan serverEvent
outBytes chan []byte
fail chan error
listeners map[string]chan []byte
}

func (w *WsSession) Ping() string {
Expand Down Expand Up @@ -115,6 +117,12 @@ func (w *WsSession) WaitForConfirm() (string, error) {
return v.Params.ConfirmId, nil
}

func (w *WsSession) ListenFor(v tdproto.Event) chan []byte {
ch := make(chan []byte, defaultSize)
w.listeners[v.GetName()] = ch
return ch
}

func (w *WsSession) WaitFor(v tdproto.Event) error {
name := v.GetName()

Expand Down Expand Up @@ -206,11 +214,23 @@ func (w WsSession) inboxLoop() {
w.SendRaw(tdproto.XClientConfirm(confirmId))
}

select {
case w.inbox <- serverEvent{
ev := serverEvent{
name: string(v.GetStringBytes("event")),
raw: data,
}:
}

ch := w.listeners[ev.name]
if ch != nil {
select {
case ch <- ev.raw:
default:
w.fail <- fmt.Errorf("listener %s chan is full", ev.name)
}
continue
}

select {
case w.inbox <- ev:
default:
w.fail <- errors.Wrapf(err, "full inbox")
}
Expand Down

0 comments on commit 2f7f507

Please sign in to comment.