|
1 | 1 | package io.rsocket.internal;
|
2 | 2 |
|
| 3 | +import java.util.Objects; |
| 4 | +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
| 5 | +import java.util.function.BiFunction; |
3 | 6 | import org.reactivestreams.Publisher;
|
4 | 7 | import org.reactivestreams.Subscription;
|
5 | 8 | import reactor.core.CoreSubscriber;
|
6 |
| -import reactor.core.publisher.DirectProcessor; |
7 | 9 | import reactor.core.publisher.Flux;
|
8 | 10 | import reactor.core.publisher.Operators;
|
9 | 11 |
|
10 |
| -import java.util.Objects; |
11 |
| -import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
12 |
| -import java.util.function.BiFunction; |
13 |
| - |
14 | 12 | public final class SwitchTransform<T, R> extends Flux<R> {
|
15 | 13 |
|
16 |
| - final Publisher<? extends T> source; |
17 |
| - final BiFunction<T, Flux<T>, Publisher<? extends R>> transformer; |
18 |
| - |
19 |
| - public SwitchTransform(Publisher<? extends T> source, BiFunction<T, Flux<T>, Publisher<? extends R>> transformer) { |
20 |
| - this.source = Objects.requireNonNull(source, "source"); |
21 |
| - this.transformer = Objects.requireNonNull(transformer, "transformer"); |
22 |
| - } |
23 |
| - |
24 |
| - @Override |
25 |
| - public void subscribe(CoreSubscriber<? super R> actual) { |
26 |
| - source.subscribe(new SwitchTransformSubscriber<>(actual, transformer)); |
27 |
| - } |
| 14 | + final Publisher<? extends T> source; |
| 15 | + final BiFunction<T, Flux<T>, Publisher<? extends R>> transformer; |
28 | 16 |
|
29 |
| - static final class SwitchTransformSubscriber<T, R> implements CoreSubscriber<T> { |
30 |
| - final CoreSubscriber<? super R> actual; |
31 |
| - final BiFunction<T, Flux<T>, Publisher<? extends R>> transformer; |
32 |
| - final DirectProcessor<T> processor = DirectProcessor.create(); |
| 17 | + public SwitchTransform( |
| 18 | + Publisher<? extends T> source, BiFunction<T, Flux<T>, Publisher<? extends R>> transformer) { |
| 19 | + this.source = Objects.requireNonNull(source, "source"); |
| 20 | + this.transformer = Objects.requireNonNull(transformer, "transformer"); |
| 21 | + } |
33 | 22 |
|
34 |
| - Subscription s; |
| 23 | + @Override |
| 24 | + public void subscribe(CoreSubscriber<? super R> actual) { |
| 25 | + Flux.from(source).subscribe(new SwitchTransformSubscriber<>(actual, transformer)); |
| 26 | + } |
35 | 27 |
|
36 |
| - volatile int once; |
37 |
| - @SuppressWarnings("rawtypes") |
38 |
| - static final AtomicIntegerFieldUpdater<SwitchTransformSubscriber> ONCE = |
39 |
| - AtomicIntegerFieldUpdater.newUpdater(SwitchTransformSubscriber.class, "once"); |
| 28 | + static final class SwitchTransformSubscriber<T, R> implements CoreSubscriber<T> { |
| 29 | + @SuppressWarnings("rawtypes") |
| 30 | + static final AtomicIntegerFieldUpdater<SwitchTransformSubscriber> ONCE = |
| 31 | + AtomicIntegerFieldUpdater.newUpdater(SwitchTransformSubscriber.class, "once"); |
40 | 32 |
|
41 |
| - SwitchTransformSubscriber(CoreSubscriber<? super R> actual, BiFunction<T, Flux<T>, Publisher<? extends R>> transformer) { |
42 |
| - this.actual = actual; |
43 |
| - this.transformer = transformer; |
44 |
| - } |
| 33 | + final CoreSubscriber<? super R> actual; |
| 34 | + final BiFunction<T, Flux<T>, Publisher<? extends R>> transformer; |
| 35 | + final UnboundedProcessor<T> processor = new UnboundedProcessor<>(); |
| 36 | + Subscription s; |
| 37 | + volatile int once; |
45 | 38 |
|
46 |
| - @Override |
47 |
| - public void onSubscribe(Subscription s) { |
48 |
| - if (Operators.validate(this.s, s)) { |
49 |
| - this.s = s; |
| 39 | + SwitchTransformSubscriber( |
| 40 | + CoreSubscriber<? super R> actual, |
| 41 | + BiFunction<T, Flux<T>, Publisher<? extends R>> transformer) { |
| 42 | + this.actual = actual; |
| 43 | + this.transformer = transformer; |
| 44 | + } |
50 | 45 |
|
51 |
| - processor.onSubscribe(s); |
52 |
| - } |
53 |
| - } |
| 46 | + @Override |
| 47 | + public void onSubscribe(Subscription s) { |
| 48 | + if (Operators.validate(this.s, s)) { |
| 49 | + this.s = s; |
| 50 | + processor.onSubscribe(s); |
| 51 | + } |
| 52 | + } |
54 | 53 |
|
55 |
| - @Override |
56 |
| - public void onNext(T t) { |
57 |
| - if (once == 0 && ONCE.compareAndSet(this, 0, 1)) { |
58 |
| - try { |
59 |
| - Publisher<? extends R> result = Objects.requireNonNull(transformer.apply(t, processor), |
60 |
| - "The transformer returned a null value"); |
61 |
| - result.subscribe(actual); |
62 |
| - } |
63 |
| - catch (Throwable e) { |
64 |
| - onError(Operators.onOperatorError(s, e, t, actual.currentContext())); |
65 |
| - return; |
66 |
| - } |
67 |
| - } |
68 |
| - processor.onNext(t); |
69 |
| - } |
| 54 | + @Override |
| 55 | + public void onNext(T t) { |
| 56 | + if (once == 0 && ONCE.compareAndSet(this, 0, 1)) { |
| 57 | + try { |
| 58 | + Publisher<? extends R> result = |
| 59 | + Objects.requireNonNull( |
| 60 | + transformer.apply(t, processor), "The transformer returned a null value"); |
| 61 | + Flux.from(result).subscribe(actual); |
| 62 | + } catch (Throwable e) { |
| 63 | + onError(Operators.onOperatorError(s, e, t, actual.currentContext())); |
| 64 | + return; |
| 65 | + } |
| 66 | + } |
| 67 | + processor.onNext(t); |
| 68 | + } |
70 | 69 |
|
71 |
| - @Override |
72 |
| - public void onError(Throwable t) { |
73 |
| - processor.onError(t); |
74 |
| - } |
| 70 | + @Override |
| 71 | + public void onError(Throwable t) { |
| 72 | + processor.onError(t); |
| 73 | + } |
75 | 74 |
|
76 |
| - @Override |
77 |
| - public void onComplete() { |
78 |
| - processor.onComplete(); |
79 |
| - } |
80 |
| - } |
| 75 | + @Override |
| 76 | + public void onComplete() { |
| 77 | + processor.onComplete(); |
| 78 | + } |
| 79 | + } |
81 | 80 | }
|
0 commit comments