|
1 | 1 | package rx.lang.kotlin |
2 | 2 |
|
3 | 3 | import rx.Subscriber |
4 | | -import rx.observers.SerializedSubscriber |
5 | 4 | import rx.exceptions.OnErrorNotImplementedException |
| 5 | +import rx.observers.SerializedSubscriber |
| 6 | +import java.util.ArrayList |
| 7 | + |
| 8 | +public class FunctionSubscriber<T>() : Subscriber<T>() { |
| 9 | + private val onCompletedFunctions = ArrayList<() -> Unit>() |
| 10 | + private val onErrorFunctions = ArrayList<(e: Throwable) -> Unit>() |
| 11 | + private val onNextFunctions = ArrayList<(value: T) -> Unit>() |
| 12 | + private val onStartFunctions = ArrayList<() -> Unit>() |
| 13 | + |
| 14 | + override fun onCompleted() = onCompletedFunctions.forEach { it() } |
| 15 | + |
| 16 | + override fun onError(e: Throwable?) = (e ?: RuntimeException("exception is unknown")).let { ex -> |
| 17 | + if (onErrorFunctions.isEmpty()) { |
| 18 | + throw OnErrorNotImplementedException(ex) |
| 19 | + } else { |
| 20 | + onErrorFunctions.forEach { it(ex) } |
| 21 | + } |
| 22 | + } |
6 | 23 |
|
7 | | -public class FunctionSubscriber<T>(onCompletedFunction: () -> Unit, onErrorFunction: (e : Throwable) -> Unit, onNextFunction: (value : T) -> Unit, onStartFunction : () -> Unit) : Subscriber<T>() { |
8 | | - private val onCompletedFunction: () -> Unit = onCompletedFunction |
9 | | - private val onErrorFunction: (e : Throwable) -> Unit = onErrorFunction |
10 | | - private val onNextFunction: (value : T) -> Unit = onNextFunction |
11 | | - private val onStartFunction : () -> Unit = onStartFunction |
| 24 | + override fun onNext(t: T) = onNextFunctions.forEach { it(t) } |
12 | 25 |
|
13 | | - override fun onCompleted() = onCompletedFunction() |
| 26 | + override fun onStart() = onStartFunctions.forEach { it() } |
14 | 27 |
|
15 | | - override fun onError(e: Throwable?) = onErrorFunction(e ?: RuntimeException("exception is unknown")) |
| 28 | + fun onCompleted(onCompletedFunction: () -> Unit): FunctionSubscriber<T> = copy { onCompletedFunctions.add(onCompletedFunction) } |
| 29 | + fun onError(onErrorFunction: (t: Throwable) -> Unit): FunctionSubscriber<T> = copy { onErrorFunctions.add(onErrorFunction) } |
| 30 | + fun onNext(onNextFunction: (t: T) -> Unit): FunctionSubscriber<T> = copy { onNextFunctions.add(onNextFunction) } |
| 31 | + fun onStart(onStartFunction : () -> Unit) : FunctionSubscriber<T> = copy { onStartFunctions.add(onStartFunction) } |
16 | 32 |
|
17 | | - override fun onNext(t: T) = onNextFunction(t) |
| 33 | + private fun copy(block: FunctionSubscriber<T>.() -> Unit): FunctionSubscriber<T> { |
| 34 | + val newSubscriber = FunctionSubscriber<T>() |
| 35 | + newSubscriber.onCompletedFunctions.addAll(onCompletedFunctions) |
| 36 | + newSubscriber.onErrorFunctions.addAll(onErrorFunctions) |
| 37 | + newSubscriber.onNextFunctions.addAll(onNextFunctions) |
| 38 | + newSubscriber.onStartFunctions.addAll(onStartFunctions) |
18 | 39 |
|
19 | | - override fun onStart() = onStartFunction() |
| 40 | + newSubscriber.block() |
20 | 41 |
|
21 | | - fun onCompleted(onCompletedFunction: () -> Unit) : FunctionSubscriber<T> = FunctionSubscriber(onCompletedFunction, this.onErrorFunction, this.onNextFunction, this.onStartFunction) |
22 | | - fun onError(onErrorFunction: (t : Throwable) -> Unit) : FunctionSubscriber<T> = FunctionSubscriber(this.onCompletedFunction, onErrorFunction, this.onNextFunction, this.onStartFunction) |
23 | | - fun onNext(onNextFunction: (t : T) -> Unit) : FunctionSubscriber<T> = FunctionSubscriber(this.onCompletedFunction, this.onErrorFunction, onNextFunction, this.onStartFunction) |
24 | | - fun onStart(onStartFunction : () -> Unit) : FunctionSubscriber<T> = FunctionSubscriber(this.onCompletedFunction, this.onErrorFunction, this.onNextFunction, onStartFunction) |
| 42 | + return newSubscriber |
| 43 | + } |
25 | 44 | } |
26 | 45 |
|
27 | | -public fun <T> subscriber(): FunctionSubscriber<T> = FunctionSubscriber({}, {throw OnErrorNotImplementedException(it)}, {}, {}) |
28 | | -public fun <T> Subscriber<T>.synchronized() : Subscriber<T> = SerializedSubscriber(this) |
| 46 | +public fun <T> subscriber(): FunctionSubscriber<T> = FunctionSubscriber() |
| 47 | +public fun <T> Subscriber<T>.synchronized(): Subscriber<T> = SerializedSubscriber(this) |
0 commit comments