-
Notifications
You must be signed in to change notification settings - Fork 8
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
27 changed files
with
1,363 additions
and
1,252 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,38 @@ | ||
### SwitchOnFirst | ||
Get first element in this `Flux` and transform to another `Flux`. | ||
|
||
``` go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/jjeffcaii/reactor-go/flux" | ||
) | ||
|
||
func main() { | ||
flux.Just("golang", "I love golang.", "I love java.", "Awesome golang.", "I love ruby."). | ||
SwitchOnFirst(func(firstSignal flux.Signal, originFlux flux.Flux) flux.Flux { | ||
// extract first word and filtering word that contains it. | ||
first, ok := firstSignal.Value() | ||
if !ok { | ||
return originFlux | ||
} | ||
return originFlux. | ||
Filter(func(item interface{}) bool { | ||
return strings.Contains(item.(string), first.(string)) | ||
}) | ||
}). | ||
DoOnNext(func(v interface{}) { | ||
fmt.Println("next:", v) | ||
}). | ||
Subscribe(context.Background()) | ||
// Should print: | ||
// next: golang | ||
// next: I love golang. | ||
// next: Awesome golang. | ||
} | ||
|
||
``` |
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,30 @@ | ||
### Take | ||
Take only the first N values from this `Flux`, if available. | ||
|
||
``` go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/jjeffcaii/reactor-go/flux" | ||
) | ||
|
||
func main() { | ||
// Example: taking 3 elements | ||
flux. | ||
Interval(500 * time.Millisecond). | ||
Take(3). | ||
DoOnNext(func(v interface{}) { | ||
fmt.Println(time.Now().Format("2006-01-02 03:04:05"), "next:", v) | ||
}). | ||
BlockLast(context.Background()) | ||
// Should print: | ||
// 2020-01-12 07:26:58 next: 0 | ||
// 2020-01-12 07:26:59 next: 1 | ||
// 2020-01-12 07:26:59 next: 2 | ||
} | ||
|
||
``` |
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,24 @@ | ||
### SwitchIfEmpty | ||
Fallback to an alternative `Mono` if this mono is completed without data. | ||
|
||
``` go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/jjeffcaii/reactor-go/mono" | ||
) | ||
|
||
func main() { | ||
// v1 should be 42 | ||
v1, _ := mono.Empty().SwitchIfEmpty(mono.Just(42)).Block(context.Background()) | ||
fmt.Println("v1:", v1) | ||
|
||
// v2 should be 1024 | ||
v2, _ := mono.Just(1024).SwitchIfEmpty(mono.Just(2048)).Block(context.Background()) | ||
fmt.Println("v2:", v2) | ||
} | ||
|
||
``` |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,97 +1,97 @@ | ||
package flux | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
"sync" | ||
"sync/atomic" | ||
|
||
rs "github.com/jjeffcaii/reactor-go" | ||
"github.com/jjeffcaii/reactor-go/hooks" | ||
rs "github.com/jjeffcaii/reactor-go" | ||
"github.com/jjeffcaii/reactor-go/hooks" | ||
) | ||
|
||
type bufferedSink struct { | ||
s rs.Subscriber | ||
q queue | ||
n int32 | ||
draining int32 | ||
stat int32 | ||
cond *sync.Cond | ||
s rs.Subscriber | ||
q queue | ||
n int32 | ||
draining int32 | ||
stat int32 | ||
cond *sync.Cond | ||
} | ||
|
||
func (p *bufferedSink) Request(n int) { | ||
atomic.AddInt32(&p.n, int32(n)) | ||
p.drain() | ||
atomic.AddInt32(&p.n, int32(n)) | ||
p.drain() | ||
} | ||
|
||
func (p *bufferedSink) Cancel() { | ||
if !atomic.CompareAndSwapInt32(&p.stat, 0, statCancel) { | ||
return | ||
} | ||
// TODO: support cancel | ||
p.dispose() | ||
if !atomic.CompareAndSwapInt32(&p.stat, 0, statCancel) { | ||
return | ||
} | ||
// TODO: support cancel | ||
p.dispose() | ||
} | ||
|
||
func (p *bufferedSink) Complete() { | ||
p.cond.L.Lock() | ||
for atomic.LoadInt32(&(p.draining)) == 1 || p.q.size() > 0 { | ||
p.cond.Wait() | ||
} | ||
p.cond.L.Unlock() | ||
if atomic.CompareAndSwapInt32(&p.stat, 0, statComplete) { | ||
p.s.OnComplete() | ||
p.dispose() | ||
} | ||
p.cond.L.Lock() | ||
for atomic.LoadInt32(&(p.draining)) == 1 || p.q.size() > 0 { | ||
p.cond.Wait() | ||
} | ||
p.cond.L.Unlock() | ||
if atomic.CompareAndSwapInt32(&p.stat, 0, statComplete) { | ||
p.s.OnComplete() | ||
p.dispose() | ||
} | ||
} | ||
|
||
func (p *bufferedSink) Error(err error) { | ||
if atomic.CompareAndSwapInt32(&p.stat, 0, statError) { | ||
hooks.Global().OnErrorDrop(err) | ||
return | ||
} | ||
p.s.OnError(err) | ||
p.dispose() | ||
if atomic.CompareAndSwapInt32(&p.stat, 0, statError) { | ||
hooks.Global().OnErrorDrop(err) | ||
return | ||
} | ||
p.s.OnError(err) | ||
p.dispose() | ||
} | ||
|
||
func (p *bufferedSink) Next(v interface{}) { | ||
if atomic.LoadInt32(&p.stat) != 0 { | ||
hooks.Global().OnNextDrop(v) | ||
return | ||
} | ||
p.q.offer(v) | ||
p.drain() | ||
if atomic.LoadInt32(&p.stat) != 0 { | ||
hooks.Global().OnNextDrop(v) | ||
return | ||
} | ||
p.q.offer(v) | ||
p.drain() | ||
} | ||
|
||
func (p *bufferedSink) drain() { | ||
if !atomic.CompareAndSwapInt32(&p.draining, 0, 1) { | ||
return | ||
} | ||
defer func() { | ||
p.cond.L.Lock() | ||
atomic.CompareAndSwapInt32(&p.draining, 1, 0) | ||
p.cond.Broadcast() | ||
p.cond.L.Unlock() | ||
}() | ||
for atomic.AddInt32(&p.n, -1) > -1 { | ||
if atomic.LoadInt32(&p.stat) != 0 { | ||
return | ||
} | ||
v, ok := p.q.poll() | ||
if !ok { | ||
atomic.AddInt32(&(p.n), 1) | ||
break | ||
} | ||
p.s.OnNext(v) | ||
} | ||
atomic.CompareAndSwapInt32(&(p.n), -1, 0) | ||
if !atomic.CompareAndSwapInt32(&p.draining, 0, 1) { | ||
return | ||
} | ||
defer func() { | ||
p.cond.L.Lock() | ||
atomic.CompareAndSwapInt32(&p.draining, 1, 0) | ||
p.cond.Broadcast() | ||
p.cond.L.Unlock() | ||
}() | ||
for atomic.AddInt32(&p.n, -1) > -1 { | ||
if atomic.LoadInt32(&p.stat) != 0 { | ||
return | ||
} | ||
v, ok := p.q.poll() | ||
if !ok { | ||
atomic.AddInt32(&(p.n), 1) | ||
break | ||
} | ||
p.s.OnNext(v) | ||
} | ||
atomic.CompareAndSwapInt32(&(p.n), -1, 0) | ||
} | ||
|
||
func (p *bufferedSink) dispose() { | ||
_ = p.q.Close() | ||
_ = p.q.Close() | ||
} | ||
|
||
func newBufferedSink(s rs.Subscriber, cap int) *bufferedSink { | ||
return &bufferedSink{ | ||
s: s, | ||
q: newQueue(cap), | ||
cond: sync.NewCond(&sync.Mutex{}), | ||
} | ||
return &bufferedSink{ | ||
s: s, | ||
q: newQueue(cap), | ||
cond: sync.NewCond(&sync.Mutex{}), | ||
} | ||
} |
Oops, something went wrong.