-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
82 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package rxgo | ||
|
||
import "context" | ||
|
||
type ( | ||
// Item is a wrapper having either a value or an error. | ||
Item struct { | ||
V interface{} | ||
E error | ||
} | ||
) | ||
|
||
// Of creates an item from a value. | ||
func Of(i interface{}) Item { | ||
return Item{V: i} | ||
} | ||
|
||
// Error creates an item from an error. | ||
func Error(err error) Item { | ||
return Item{E: err} | ||
} | ||
|
||
// Error checks if an item is an error. | ||
func (i Item) Error() bool { | ||
return i.E != nil | ||
} | ||
|
||
// SendBlocking sends an item and blocks until it is sent. | ||
func (i Item) SendBlocking(ch chan<- Item) { | ||
ch <- i | ||
} | ||
|
||
// SendContext sends an item and blocks until it is sent or a context canceled. | ||
// It returns a boolean to indicate whether the item was sent. | ||
func (i Item) SendContext(ctx context.Context, ch chan<- Item) bool { | ||
select { | ||
case <-ctx.Done(): | ||
return false | ||
case ch <- i: | ||
return true | ||
} | ||
} | ||
|
||
// SendNonBlocking sends an item without blocking. | ||
// It returns a boolean to indicate whether the item was sent. | ||
func (i Item) SendNonBlocking(ch chan<- Item) bool { | ||
select { | ||
default: | ||
return false | ||
case ch <- i: | ||
return true | ||
} | ||
} |
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,29 @@ | ||
package rxgo | ||
|
||
import ( | ||
"context" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func Test_Item_SendBlocking(t *testing.T) { | ||
ch := make(chan Item, 1) | ||
defer close(ch) | ||
Of(5).SendBlocking(ch) | ||
assert.Equal(t, 5, (<-ch).V) | ||
} | ||
|
||
func Test_Item_SendContext_True(t *testing.T) { | ||
ch := make(chan Item, 1) | ||
defer close(ch) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
assert.True(t, Of(5).SendContext(ctx, ch)) | ||
} | ||
|
||
func Test_Item_SendNonBlocking(t *testing.T) { | ||
ch := make(chan Item, 1) | ||
defer close(ch) | ||
assert.True(t, Of(5).SendNonBlocking(ch)) | ||
assert.False(t, Of(5).SendNonBlocking(ch)) | ||
} |
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