-
Notifications
You must be signed in to change notification settings - Fork 8
/
initiate.go
113 lines (93 loc) · 2.68 KB
/
initiate.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package mono
import (
"context"
"fmt"
"time"
"github.com/jjeffcaii/reactor-go"
"github.com/jjeffcaii/reactor-go/scheduler"
)
var empty = wrap(monoEmpty{})
var _errJustNilValue = "require non nil value"
func Error(e error) Mono {
return wrap(newMonoError(e))
}
func ErrorOneshot(e error) Mono {
return borrowOneshotWrapper(newMonoError(e))
}
func Empty() Mono {
return empty
}
func JustOrEmpty(v Any) Mono {
if v == nil {
return empty
}
return Just(v)
}
func Just(v Any) Mono {
if v == nil {
panic(_errJustNilValue)
}
return wrap(newMonoJust(v))
}
func JustOneshot(v Any) Mono {
if v == nil {
panic(_errJustNilValue)
}
return borrowOneshotWrapper(newMonoJust(v))
}
func Create(gen func(ctx context.Context, s Sink)) Mono {
return wrap(newMonoCreate(gen))
}
func CreateOneshot(gen func(ctx context.Context, s Sink)) Mono {
return borrowOneshotWrapper(newMonoCreate(gen))
}
func Delay(delay time.Duration) Mono {
return wrap(newMonoDelay(delay))
}
func NewProcessor(sc scheduler.Scheduler, hook ProcessorFinallyHook) (Mono, Sink, Disposable) {
p := globalProcessorPool.get()
p.mu.Lock()
p.sc = sc
p.hookOnFinally = hook
p.mu.Unlock()
return wrap(p), p, p
}
func Zip(first Mono, second Mono, rest ...Mono) Mono {
return wrap(zip(first, second, rest, nil))
}
func ZipCombine(cmb Combinator, itemHandler func(item *reactor.Item), sources ...Mono) Mono {
return wrap(zipCombine(sources, cmb, itemHandler))
}
func ZipOneshot(first Mono, second Mono, rest ...Mono) Mono {
return borrowOneshotWrapper(zip(first, second, rest, nil))
}
func ZipCombineOneshot(cmb Combinator, itemHandler func(*reactor.Item), sources ...Mono) Mono {
return borrowOneshotWrapper(zipCombine(sources, cmb, itemHandler))
}
func innerZipCombine(sources []reactor.RawPublisher, cmb Combinator, itemHandler func(*reactor.Item)) *monoZip {
for i := 0; i < len(sources); i++ {
if sources[i] == nil {
panic(fmt.Sprintf("the #%d Mono to be zipped is nil!", i))
}
}
return newMonoZip(sources, cmb, itemHandler)
}
func zip(first Mono, second Mono, rest []Mono, itemHandler func(*reactor.Item)) *monoZip {
sources := make([]reactor.RawPublisher, len(rest)+2)
sources[0] = unpackRawPublisher(first)
sources[1] = unpackRawPublisher(second)
for i := 0; i < len(rest); i++ {
sources[i+2] = unpackRawPublisher(rest[i])
}
return innerZipCombine(sources, nil, itemHandler)
}
func zipCombine(sources []Mono, cmb Combinator, itemHandler func(*reactor.Item)) *monoZip {
if len(sources) < 2 {
panic("ZipCombine need at least two Mono!")
}
pubs := make([]reactor.RawPublisher, len(sources))
for i := 0; i < len(sources); i++ {
pubs[i] = unpackRawPublisher(sources[i])
}
return innerZipCombine(pubs, cmb, itemHandler)
}