17
17
import co .paralleluniverse .fibers .SuspendExecution ;
18
18
import co .paralleluniverse .fibers .Suspendable ;
19
19
import co .paralleluniverse .strands .Strand ;
20
- import co .paralleluniverse .strands .Timeout ;
21
20
import co .paralleluniverse .strands .channels .Channel ;
22
21
import co .paralleluniverse .strands .channels .Channels ;
23
22
import co .paralleluniverse .strands .channels .ReceivePort ;
24
23
import co .paralleluniverse .strands .channels .SendPort ;
25
- import java .util .concurrent .TimeUnit ;
26
24
import rx .Observable ;
27
25
import rx .Observer ;
28
26
import rx .Scheduler ;
29
- import rx .util .Exceptions ;
30
- import rx .util .OnErrorNotImplementedException ;
31
27
import rx .util .functions .Action2 ;
32
28
import rx .util .functions .Actions ;
33
29
import rx .util .functions .Func1 ;
@@ -109,7 +105,7 @@ public void onCompleted() {
109
105
110
106
@ Override
111
107
public void onError (Throwable e ) {
112
- throw new OnErrorNotImplementedException (e );
108
+ channel . close (e );
113
109
}
114
110
};
115
111
}
@@ -124,7 +120,7 @@ public void onError(Throwable e) {
124
120
* @return A new channel with the given buffer size and overflow policy that will receive all events emitted by the observable.
125
121
*/
126
122
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 );
128
124
129
125
System .out .println (Functions .fromFunc (new Func1 <String , String >() {
130
126
@@ -146,7 +142,7 @@ public void call(String t1, String t2) {
146
142
@ Suspendable
147
143
public void onNext (T t ) {
148
144
try {
149
- channel .sendPort (). send (t );
145
+ channel .send (t );
150
146
} catch (InterruptedException ex ) {
151
147
Strand .interrupted ();
152
148
} catch (SuspendExecution ex ) {
@@ -156,85 +152,14 @@ public void onNext(T t) {
156
152
157
153
@ Override
158
154
public void onCompleted () {
159
- channel .sendPort (). close ();
155
+ channel .close ();
160
156
}
161
157
162
158
@ Override
163
159
public void onError (Throwable e ) {
164
- channel .error (e );
160
+ channel .close (e );
165
161
}
166
162
});
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 ;
239
164
}
240
165
}
0 commit comments