-
Notifications
You must be signed in to change notification settings - Fork 8
/
op_context.go
42 lines (34 loc) · 858 Bytes
/
op_context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package mono
import (
"context"
"github.com/jjeffcaii/reactor-go"
"github.com/jjeffcaii/reactor-go/internal"
)
type monoContext struct {
source reactor.RawPublisher
kv []Any
}
func (p *monoContext) Parent() reactor.RawPublisher {
return p.source
}
func (p *monoContext) SubscribeWith(ctx context.Context, s reactor.Subscriber) {
for i := 0; i < len(p.kv); i += 2 {
ctx = context.WithValue(ctx, p.kv[i], p.kv[i+1])
}
p.source.SubscribeWith(ctx, s)
}
type monoContextOption func(*monoContext)
func withContextDiscard(fn reactor.FnOnDiscard) monoContextOption {
return func(i *monoContext) {
i.kv = append(i.kv, internal.KeyOnDiscard, fn)
}
}
func newMonoContext(source reactor.RawPublisher, options ...monoContextOption) *monoContext {
mc := &monoContext{
source: source,
}
for _, it := range options {
it(mc)
}
return mc
}