Skip to content

Commit

Permalink
feat(rx): add count operator (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Apr 9, 2024
1 parent 08ba0da commit 96f328b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
50 changes: 50 additions & 0 deletions rx/observable-operator-count_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package rx_test

import (
"context"

"github.com/fortytw2/leaktest"
. "github.com/onsi/ginkgo/v2" //nolint:revive // ginkgo ok
"github.com/snivilised/lorax/rx"
)

var _ = Describe("Observable operator", func() {
Context("Count", func() {
Context("principle", func() {
It("🧪 should: ", func() {
// rxgo: Test_Observable_Count
defer leaktest.Check(GinkgoT())()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

rx.Assert(ctx, rx.Range[int](1, 100).Count(),
rx.HasNumber[int]{
Expected: 100,
})
})
})

Context("Parallel", func() {
Context("given: foo", func() {
It("🧪 should: ", func() {
// rxgo: Test_Observable_Count_Parallel
defer leaktest.Check(GinkgoT())()

/*
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
rx.Assert(ctx, rx.Range[int](1, 100).Count(
rx.WithCPUPool[int](),
),
rx.HasNumber[int]{
Expected: 100,
},
)
*/
})
})
})
})
})
36 changes: 36 additions & 0 deletions rx/observable-operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,42 @@ func (op *containsOperator[T]) gatherNext(ctx context.Context, item Item[T],
}
}

func (o *ObservableImpl[T]) Count(opts ...Option[T]) Single[T] {
const (
forceSeq = true
bypassGather = false
)

return single(o.parent, o, func() operator[T] {
return &countOperator[T]{}
}, forceSeq, bypassGather, opts...)
}

type countOperator[T any] struct {
count int
}

func (op *countOperator[T]) next(_ context.Context, _ Item[T],
_ chan<- Item[T], _ operatorOptions[T],
) {
op.count++
}

func (op *countOperator[T]) err(_ context.Context, _ Item[T],
_ chan<- Item[T], operatorOptions operatorOptions[T],
) {
operatorOptions.stop()
}

func (op *countOperator[T]) end(ctx context.Context, dst chan<- Item[T]) {
Num[T](op.count).SendContext(ctx, dst)
}

func (op *countOperator[T]) gatherNext(_ context.Context, _ Item[T],
_ chan<- Item[T], _ operatorOptions[T],
) {
}

// Max determines and emits the maximum-valued item emitted by an Observable according to a comparator.
func (o *ObservableImpl[T]) Max(comparator Comparator[T], initLimit InitLimit[T],
opts ...Option[T],
Expand Down
1 change: 1 addition & 0 deletions rx/observable.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Observable[T any] interface {
BackOffRetry(backOffCfg backoff.BackOff, opts ...Option[T]) Observable[T]
Connect(ctx context.Context) (context.Context, Disposable)
Contains(equal Predicate[T], opts ...Option[T]) Single[T]
Count(opts ...Option[T]) Single[T]

Max(comparator Comparator[T], initLimit InitLimit[T], opts ...Option[T]) OptionalSingle[T]
Map(apply Func[T], opts ...Option[T]) Observable[T]
Expand Down

0 comments on commit 96f328b

Please sign in to comment.