1
1
/**
2
2
* Copyright 2013 Netflix, Inc.
3
- *
3
+ *
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
6
6
* You may obtain a copy of the License at
7
- *
7
+ *
8
8
* http://www.apache.org/licenses/LICENSE-2.0
9
- *
9
+ *
10
10
* Unless required by applicable law or agreed to in writing, software
11
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
18
import rx .Notification ;
19
19
import rx .Observer ;
20
20
import rx .Scheduler ;
21
+ import rx .concurrency .Schedulers ;
21
22
import rx .util .functions .Action0 ;
22
23
23
- import java .util .concurrent .ConcurrentLinkedQueue ;
24
- import java .util .concurrent .atomic .AtomicInteger ;
25
-
26
24
/* package */ class ScheduledObserver <T > implements Observer <T > {
27
25
private final Observer <T > underlying ;
28
26
private final Scheduler scheduler ;
29
27
30
- private final ConcurrentLinkedQueue <Notification <T >> queue = new ConcurrentLinkedQueue <Notification <T >>();
31
- private final AtomicInteger counter = new AtomicInteger (0 );
32
-
33
28
public ScheduledObserver (Observer <T > underlying , Scheduler scheduler ) {
34
29
this .underlying = underlying ;
35
30
this .scheduler = scheduler ;
@@ -46,47 +41,38 @@ public void onError(final Exception e) {
46
41
}
47
42
48
43
@ Override
49
- public void onNext (final T args ) {
50
- enqueue (new Notification <T >(args ));
44
+ public void onNext (final T v ) {
45
+ enqueue (new Notification <T >(v ));
51
46
}
52
47
53
- private void enqueue (Notification <T > notification ) {
54
- int count = counter .getAndIncrement ();
55
-
56
- queue .offer (notification );
48
+ private void enqueue (final Notification <T > notification ) {
57
49
58
- if (count == 0 ) {
59
- processQueue ();
60
- }
61
- }
62
-
63
- private void processQueue () {
64
- scheduler .schedule (new Action0 () {
50
+ Schedulers .currentThread ().schedule (new Action0 () {
65
51
@ Override
66
52
public void call () {
67
- Notification <T > not = queue .poll ();
68
-
69
- switch (not .getKind ()) {
70
- case OnNext :
71
- underlying .onNext (not .getValue ());
72
- break ;
73
- case OnError :
74
- underlying .onError (not .getException ());
75
- break ;
76
- case OnCompleted :
77
- underlying .onCompleted ();
78
- break ;
79
- default :
80
- throw new IllegalStateException ("Unknown kind of notification " + not );
81
-
82
- }
83
-
84
- int count = counter .decrementAndGet ();
85
- if (count > 0 ) {
86
- scheduler .schedule (this );
87
- }
88
53
54
+ scheduler .schedule (new Action0 () {
55
+ @ Override
56
+ public void call () {
57
+ switch (notification .getKind ()) {
58
+ case OnNext :
59
+ underlying .onNext (notification .getValue ());
60
+ break ;
61
+ case OnError :
62
+ underlying .onError (notification .getException ());
63
+ break ;
64
+ case OnCompleted :
65
+ underlying .onCompleted ();
66
+ break ;
67
+ default :
68
+ throw new IllegalStateException ("Unknown kind of notification " + notification );
69
+
70
+ }
71
+ }
72
+ });
89
73
}
74
+
90
75
});
91
- }
76
+ };
77
+
92
78
}
0 commit comments