|
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.
|
|
15 | 15 | */
|
16 | 16 | package rx.operators;
|
17 | 17 |
|
| 18 | +import rx.Notification; |
18 | 19 | import rx.Observer;
|
19 | 20 | import rx.Scheduler;
|
20 | 21 | import rx.util.functions.Action0;
|
21 | 22 |
|
| 23 | +import java.util.concurrent.ConcurrentLinkedQueue; |
| 24 | +import java.util.concurrent.atomic.AtomicInteger; |
| 25 | + |
22 | 26 | /* package */class ScheduledObserver<T> implements Observer<T> {
|
23 | 27 | private final Observer<T> underlying;
|
24 | 28 | private final Scheduler scheduler;
|
25 | 29 |
|
| 30 | + private final ConcurrentLinkedQueue<Notification<T>> queue = new ConcurrentLinkedQueue<Notification<T>>(); |
| 31 | + private final AtomicInteger counter = new AtomicInteger(0); |
| 32 | + |
26 | 33 | public ScheduledObserver(Observer<T> underlying, Scheduler scheduler) {
|
27 | 34 | this.underlying = underlying;
|
28 | 35 | this.scheduler = scheduler;
|
29 | 36 | }
|
30 | 37 |
|
31 | 38 | @Override
|
32 | 39 | public void onCompleted() {
|
33 |
| - scheduler.schedule(new Action0() { |
34 |
| - @Override |
35 |
| - public void call() { |
36 |
| - underlying.onCompleted(); |
37 |
| - } |
38 |
| - }); |
| 40 | + enqueue(new Notification<T>()); |
39 | 41 | }
|
40 | 42 |
|
41 | 43 | @Override
|
42 | 44 | public void onError(final Exception e) {
|
43 |
| - scheduler.schedule(new Action0() { |
44 |
| - @Override |
45 |
| - public void call() { |
46 |
| - underlying.onError(e); |
47 |
| - } |
48 |
| - }); |
| 45 | + enqueue(new Notification<T>(e)); |
49 | 46 | }
|
50 | 47 |
|
51 | 48 | @Override
|
52 | 49 | public void onNext(final T args) {
|
| 50 | + enqueue(new Notification<T>(args)); |
| 51 | + } |
| 52 | + |
| 53 | + private void enqueue(Notification<T> notification) { |
| 54 | + int count = counter.getAndIncrement(); |
| 55 | + |
| 56 | + queue.offer(notification); |
| 57 | + |
| 58 | + if (count == 0) { |
| 59 | + processQueue(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private void processQueue() { |
53 | 64 | scheduler.schedule(new Action0() {
|
54 | 65 | @Override
|
55 | 66 | public void call() {
|
56 |
| - underlying.onNext(args); |
| 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 | + |
57 | 89 | }
|
58 | 90 | });
|
59 | 91 | }
|
|
0 commit comments