|
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 | + |
22 | 25 | /* package */class ScheduledObserver<T> implements Observer<T> {
|
23 | 26 | private final Observer<T> underlying;
|
24 | 27 | private final Scheduler scheduler;
|
25 | 28 |
|
| 29 | + private final ConcurrentLinkedQueue<Notification<T>> queue = new ConcurrentLinkedQueue<Notification<T>>(); |
| 30 | + private final Object lock = new Object(); |
| 31 | + |
26 | 32 | public ScheduledObserver(Observer<T> underlying, Scheduler scheduler) {
|
27 | 33 | this.underlying = underlying;
|
28 | 34 | this.scheduler = scheduler;
|
29 | 35 | }
|
30 | 36 |
|
31 | 37 | @Override
|
32 | 38 | public void onCompleted() {
|
33 |
| - scheduler.schedule(new Action0() { |
34 |
| - @Override |
35 |
| - public void call() { |
36 |
| - underlying.onCompleted(); |
37 |
| - } |
38 |
| - }); |
| 39 | + enqueue(new Notification<T>()); |
39 | 40 | }
|
40 | 41 |
|
41 | 42 | @Override
|
42 | 43 | public void onError(final Exception e) {
|
43 |
| - scheduler.schedule(new Action0() { |
44 |
| - @Override |
45 |
| - public void call() { |
46 |
| - underlying.onError(e); |
47 |
| - } |
48 |
| - }); |
| 44 | + enqueue(new Notification<T>(e)); |
49 | 45 | }
|
50 | 46 |
|
51 | 47 | @Override
|
52 | 48 | public void onNext(final T args) {
|
53 |
| - scheduler.schedule(new Action0() { |
54 |
| - @Override |
55 |
| - public void call() { |
56 |
| - underlying.onNext(args); |
57 |
| - } |
58 |
| - }); |
| 49 | + enqueue(new Notification<T>(args)); |
| 50 | + } |
| 51 | + |
| 52 | + private void enqueue(Notification<T> notification) { |
| 53 | + boolean schedule; |
| 54 | + synchronized (lock) { |
| 55 | + schedule = queue.isEmpty(); |
| 56 | + |
| 57 | + queue.offer(notification); |
| 58 | + } |
| 59 | + |
| 60 | + if (schedule) { |
| 61 | + scheduler.schedule(new Action0() { |
| 62 | + @Override |
| 63 | + public void call() { |
| 64 | + Notification<T> not = dequeue(); |
| 65 | + |
| 66 | + if (not == null) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + switch (not.getKind()) { |
| 71 | + case OnNext: |
| 72 | + underlying.onNext(not.getValue()); |
| 73 | + break; |
| 74 | + case OnError: |
| 75 | + underlying.onError(not.getException()); |
| 76 | + break; |
| 77 | + case OnCompleted: |
| 78 | + underlying.onCompleted(); |
| 79 | + break; |
| 80 | + default: |
| 81 | + throw new IllegalStateException("Unknown kind of notification " + not); |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + scheduler.schedule(this); |
| 86 | + |
| 87 | + } |
| 88 | + }); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private Notification<T> dequeue() { |
| 93 | + synchronized (lock) { |
| 94 | + return queue.poll(); |
| 95 | + } |
59 | 96 | }
|
60 | 97 | }
|
0 commit comments