Revamp listing decoding, include after/before anchors in response

Now, instead of returning an object containing a list of results + the
anchors, we return just the list. The anchors are available in the
response object. Much cleaner this way in my opinion

go-github and godo do it this way too. They include some meta
information in the returned response objects

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian
2020-08-29 14:20:30 -04:00
parent 37e712b334
commit 2a1806ec33
15 changed files with 520 additions and 579 deletions
+12 -12
View File
@@ -29,15 +29,15 @@ func (s *StreamService) Posts(subreddit string, opts ...StreamOpt) (<-chan *Post
}
ticker := time.NewTicker(streamConfig.Interval)
posts := make(chan *Post)
errs := make(chan error)
postsCh := make(chan *Post)
errsCh := make(chan error)
var once sync.Once
stop := func() {
once.Do(func() {
ticker.Stop()
close(posts)
close(errs)
close(postsCh)
close(errsCh)
})
}
@@ -54,16 +54,16 @@ func (s *StreamService) Posts(subreddit string, opts ...StreamOpt) (<-chan *Post
for ; ; <-ticker.C {
n++
result, err := s.getPosts(subreddit)
posts, err := s.getPosts(subreddit)
if err != nil {
errs <- err
errsCh <- err
if !infinite && n >= streamConfig.MaxRequests {
break
}
continue
}
for _, post := range result.Posts {
for _, post := range posts {
id := post.FullID
// if this post id is already part of the set, it means that it and the ones
@@ -78,7 +78,7 @@ func (s *StreamService) Posts(subreddit string, opts ...StreamOpt) (<-chan *Post
break
}
posts <- post
postsCh <- post
}
if !infinite && n >= streamConfig.MaxRequests {
@@ -87,12 +87,12 @@ func (s *StreamService) Posts(subreddit string, opts ...StreamOpt) (<-chan *Post
}
}()
return posts, errs, stop
return postsCh, errsCh, stop
}
func (s *StreamService) getPosts(subreddit string) (*Posts, error) {
result, _, err := s.client.Subreddit.NewPosts(context.Background(), subreddit, &ListOptions{Limit: 100})
return result, err
func (s *StreamService) getPosts(subreddit string) ([]*Post, error) {
posts, _, err := s.client.Subreddit.NewPosts(context.Background(), subreddit, &ListOptions{Limit: 100})
return posts, err
}
type set map[string]struct{}