-
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.
flatmap documentation and example; create operator to ease implementa…
…tion
- Loading branch information
Showing
4 changed files
with
99 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package flatmap | ||
|
||
import ( | ||
"github.com/reactivex/rxgo/observable" | ||
"github.com/reactivex/rxgo/observer" | ||
"testing" | ||
) | ||
|
||
func TestFlatMapExample(t *testing.T) { | ||
// given | ||
observerMock := observer.NewObserverMock() | ||
|
||
// and | ||
primeSequence := observable.Just([]int{2, 3, 5, 7, 11, 13}) | ||
|
||
// when | ||
<-primeSequence. | ||
FlatMap(func(primes interface{}) observable.Observable { | ||
return observable.Create(func(emitter *observer.Observer) { | ||
for _, prime := range primes.([]int) { | ||
emitter.OnNext(prime) | ||
} | ||
emitter.OnDone() | ||
}) | ||
}, 1). | ||
Last(). | ||
Subscribe(observerMock.Capture()) | ||
|
||
// then | ||
observerMock.AssertCalled(t, "OnNext", 13) | ||
} |
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,34 @@ | ||
package observable | ||
|
||
import "github.com/reactivex/rxgo/observer" | ||
|
||
// Creates observable from based on source function. Keep it mind to call emitter.OnDone() | ||
// to signal sequence's end. | ||
// Example: | ||
// - emitting none elements | ||
// observable.Create(emitter *observer.Observer) { emitter.OnDone() }) | ||
// - emitting one element | ||
// observable.Create(func(emitter chan interface{}) { | ||
// emitter.OnNext("one element") | ||
// emitter.OnDone() | ||
// }) | ||
func Create(source func(emitter *observer.Observer)) Observable { | ||
emitted := make(chan interface{}) | ||
emitter := &observer.Observer{ | ||
NextHandler: func(el interface{}) { | ||
emitted <- el | ||
}, | ||
ErrHandler: func(err error) { | ||
// decide how to deal with errors | ||
}, | ||
DoneHandler: func() { | ||
close(emitted) | ||
}, | ||
} | ||
|
||
go func() { | ||
source(emitter) | ||
}() | ||
|
||
return emitted | ||
} |
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