1717import co .paralleluniverse .fibers .SuspendExecution ;
1818import co .paralleluniverse .fibers .Suspendable ;
1919import co .paralleluniverse .strands .Strand ;
20- import co .paralleluniverse .strands .Timeout ;
2120import co .paralleluniverse .strands .channels .Channel ;
2221import co .paralleluniverse .strands .channels .Channels ;
2322import co .paralleluniverse .strands .channels .ReceivePort ;
2423import co .paralleluniverse .strands .channels .SendPort ;
25- import java .util .concurrent .TimeUnit ;
2624import rx .Observable ;
2725import rx .Observer ;
2826import rx .Scheduler ;
29- import rx .util .Exceptions ;
30- import rx .util .OnErrorNotImplementedException ;
3127import rx .util .functions .Action2 ;
3228import rx .util .functions .Actions ;
3329import rx .util .functions .Func1 ;
@@ -109,7 +105,7 @@ public void onCompleted() {
109105
110106 @ Override
111107 public void onError (Throwable e ) {
112- throw new OnErrorNotImplementedException (e );
108+ channel . close (e );
113109 }
114110 };
115111 }
@@ -124,7 +120,7 @@ public void onError(Throwable e) {
124120 * @return A new channel with the given buffer size and overflow policy that will receive all events emitted by the observable.
125121 */
126122 public final static <T > ReceivePort <T > subscribe (int bufferSize , Channels .OverflowPolicy policy , Observable <T > o ) {
127- final ChannelWithErrors <T > channel = new ChannelWithErrors < T >( Channels .newChannel (bufferSize , policy ) );
123+ final Channel <T > channel = Channels .newChannel (bufferSize , policy );
128124
129125 System .out .println (Functions .fromFunc (new Func1 <String , String >() {
130126
@@ -146,7 +142,7 @@ public void call(String t1, String t2) {
146142 @ Suspendable
147143 public void onNext (T t ) {
148144 try {
149- channel .sendPort (). send (t );
145+ channel .send (t );
150146 } catch (InterruptedException ex ) {
151147 Strand .interrupted ();
152148 } catch (SuspendExecution ex ) {
@@ -156,85 +152,14 @@ public void onNext(T t) {
156152
157153 @ Override
158154 public void onCompleted () {
159- channel .sendPort (). close ();
155+ channel .close ();
160156 }
161157
162158 @ Override
163159 public void onError (Throwable e ) {
164- channel .error (e );
160+ channel .close (e );
165161 }
166162 });
167- return channel .receivePort ();
168- }
169-
170- private static class ChannelWithErrors <T > {
171- private final Channel <Object > ch ;
172-
173- public ChannelWithErrors (Channel <Object > ch ) {
174- this .ch = ch ;
175- }
176-
177- @ Suspendable
178- public void error (Throwable t ) {
179- try {
180- ch .send (new ThrowableWrapper (t ));
181- ch .close ();
182- } catch (InterruptedException e ) {
183- } catch (SuspendExecution e ) {
184- throw new AssertionError (e );
185- }
186- }
187-
188- public ReceivePort <T > receivePort () {
189- return new ReceivePort <T >() {
190- @ Override
191- public T receive () throws SuspendExecution , InterruptedException {
192- return get (ch .receive ());
193- }
194-
195- @ Override
196- public T receive (long timeout , TimeUnit unit ) throws SuspendExecution , InterruptedException {
197- return get (ch .receive (timeout , unit ));
198- }
199-
200- @ Override
201- public T receive (Timeout timeout ) throws SuspendExecution , InterruptedException {
202- return get (ch .receive (timeout ));
203- }
204-
205- @ Override
206- public T tryReceive () {
207- return get (ch .tryReceive ());
208- }
209-
210- @ Override
211- public void close () {
212- ch .close ();
213- }
214-
215- @ Override
216- public boolean isClosed () {
217- return ch .isClosed ();
218- }
219- };
220- }
221-
222- public SendPort <T > sendPort () {
223- return (SendPort <T >) ch ;
224- }
225-
226- private T get (Object m ) {
227- if (m instanceof ThrowableWrapper )
228- throw Exceptions .propagate (((ThrowableWrapper ) m ).t );
229- return (T ) m ;
230- }
231-
232- private static class ThrowableWrapper {
233- final Throwable t ;
234-
235- public ThrowableWrapper (Throwable t ) {
236- this .t = t ;
237- }
238- }
163+ return channel ;
239164 }
240165}
0 commit comments