@@ -2,6 +2,7 @@ package shell
2
2
3
3
import (
4
4
"encoding/json"
5
+ "io"
5
6
6
7
"github.com/libp2p/go-libp2p-peer"
7
8
)
@@ -16,33 +17,27 @@ type Message struct {
16
17
17
18
// PubSubSubscription allow you to receive pubsub records that where published on the network.
18
19
type PubSubSubscription struct {
19
- resp * Response
20
+ resp io.Closer
21
+ dec * json.Decoder
20
22
}
21
23
22
- func newPubSubSubscription (resp * Response ) * PubSubSubscription {
23
- sub := & PubSubSubscription {
24
+ func newPubSubSubscription (resp io. ReadCloser ) * PubSubSubscription {
25
+ return & PubSubSubscription {
24
26
resp : resp ,
27
+ dec : json .NewDecoder (resp ),
25
28
}
26
-
27
- return sub
28
29
}
29
30
30
31
// Next waits for the next record and returns that.
31
32
func (s * PubSubSubscription ) Next () (* Message , error ) {
32
- if s .resp .Error != nil {
33
- return nil , s .resp .Error
34
- }
35
-
36
- d := json .NewDecoder (s .resp .Output )
37
-
38
33
var r struct {
39
34
From []byte `json:"from,omitempty"`
40
35
Data []byte `json:"data,omitempty"`
41
36
Seqno []byte `json:"seqno,omitempty"`
42
37
TopicIDs []string `json:"topicIDs,omitempty"`
43
38
}
44
39
45
- err := d .Decode (& r )
40
+ err := s . dec .Decode (& r )
46
41
if err != nil {
47
42
return nil , err
48
43
}
@@ -61,9 +56,5 @@ func (s *PubSubSubscription) Next() (*Message, error) {
61
56
62
57
// Cancel cancels the given subscription.
63
58
func (s * PubSubSubscription ) Cancel () error {
64
- if s .resp .Output == nil {
65
- return nil
66
- }
67
-
68
- return s .resp .Output .Close ()
59
+ return s .resp .Close ()
69
60
}
0 commit comments