Skip to content

Commit

Permalink
update readme, move channels
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Kaperys committed Jul 25, 2018
1 parent 21483a5 commit fa5f379
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

A small Go library for interaction with webOS enabled TVs. Tested on LG webOS TV UH668V (webOS version 05.30.20).

[![Go Report Card](https://goreportcard.com/badge/github.com/kaperys/go-webos)](https://goreportcard.com/report/github.com/kaperys/go-webos)

```go
dialer := websocket.Dialer{
HandshakeTimeout: 10 * time.Second,
Expand Down
7 changes: 4 additions & 3 deletions tv.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ func (tv *TV) Close() error {
// to the channel in the MessageHandler method, and read in this method. Responses
// are vaildates before they are returned.
func (tv *TV) request(msg *Message) (Message, error) {
ch := make(chan Message, 1)
tv.setupResponseChannel(msg.ID, ch)
ch := tv.setupResponseChannel(msg.ID)
defer tv.teardownResponseChannel(msg.ID)

b, err := json.Marshal(msg)
Expand Down Expand Up @@ -177,15 +176,17 @@ func (tv *TV) request(msg *Message) (Message, error) {
}

// setupResponseChannel ensures a channel is available for the given Message ID responses.
func (tv *TV) setupResponseChannel(id string, ch chan<- Message) {
func (tv *TV) setupResponseChannel(id string) chan Message {
tv.resMutex.Lock()
defer tv.resMutex.Unlock()

if tv.res == nil {
tv.res = make(map[string]chan<- Message)
}

ch := make(chan Message, 1)
tv.res[id] = ch
return ch
}

// teardownResponseChannel removes the channels used by the given Message ID.
Expand Down

0 comments on commit fa5f379

Please sign in to comment.