-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAPublisher.java
62 lines (51 loc) · 1.46 KB
/
APublisher.java
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
package example;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.reactivestreams.Operator;
import org.reactivestreams.Processor;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
public class APublisher<T> implements Publisher<T> {
private final Consumer<Subscriber<T>> f;
protected APublisher(Consumer<Subscriber<T>> f) {
this.f = f;
}
public <R> APublisher<R> process(Supplier<Processor<T, R>> supplier) {
return new APublisher<R>((s) -> {
Processor<T, R> p = supplier.get();
p.subscribe(s);
f.accept(p);
});
}
public <R> APublisher<R> lift(Operator<T, R> lift) {
return new APublisher<R>((s) -> {
f.accept(lift.call(s));
});
}
/**
* This will blow up because it happens in the wrong order
*
* @param p
* @return
*/
public <R> Publisher<R> processSimple(Processor<T, R> p) {
subscribe(p);
return p;
}
/**
* This works for a single subscription, but not when subscribed to multiple times because the `Processor` instance gets reused
*
* @param p
* @return
*/
public <R> Publisher<R> process(Processor<T, R> p) {
return new APublisher<R>((s) -> {
p.subscribe(s);
f.accept(p);
});
}
@Override
public void subscribe(Subscriber<T> s) {
f.accept(s);
}
}