-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
- Loading branch information
Showing
4 changed files
with
56 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package streamingset | ||
|
||
import ( | ||
"context" | ||
|
||
cid "gx/ipfs/QmYjnkEL7i731PirfVH1sis89evN7jt4otSHw5D2xXXwUV/go-cid" | ||
) | ||
|
||
// StreamingSet is an extension of cid.Set which allows to implement back-pressure | ||
// for the Visit function | ||
type StreamingSet struct { | ||
Set *cid.Set | ||
New chan *cid.Cid | ||
} | ||
|
||
// NewStreamingSet initializes and returns new Set. | ||
func NewStreamingSet() *StreamingSet { | ||
return &StreamingSet{ | ||
Set: cid.NewSet(), | ||
New: make(chan *cid.Cid), | ||
} | ||
} | ||
|
||
// Visitor creates new visitor which adds a Cids to the set and emits them to | ||
// the set.New channel | ||
func (s *StreamingSet) Visitor(ctx context.Context) func(c *cid.Cid) bool { | ||
return func(c *cid.Cid) bool { | ||
if s.Set.Visit(c) { | ||
select { | ||
case s.New <- c: | ||
case <-ctx.Done(): | ||
} | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
} |