|
1 | 1 | /**
|
2 | 2 | * Copyright (C) 2016 android10.org Open Source Project
|
3 |
| - * |
| 3 | + * <p> |
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 | + * <p> |
8 | 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 |
| - * |
| 9 | + * <p> |
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 com.fernandocejas.android10.rx.sample.activity.backpressure;
|
17 | 17 |
|
18 |
| -import com.fernandocejas.frodo.annotation.RxLogSubscriber; |
19 |
| -import java.lang.ref.WeakReference; |
| 18 | +import rx.Producer; |
20 | 19 | import rx.Subscriber;
|
21 | 20 | import rx.exceptions.MissingBackpressureException;
|
22 | 21 |
|
23 |
| -@RxLogSubscriber |
| 22 | +import java.lang.ref.WeakReference; |
| 23 | + |
| 24 | +//@RxLogSubscriber |
24 | 25 | class BackpressureSubscriber<T> extends Subscriber<T> {
|
25 | 26 |
|
26 |
| - interface BackPressureResultListener { |
27 |
| - void onOperationStart(String name, long itemsRequested); |
28 |
| - void onOperationProgress(long itemsProcessedSoFar); |
29 |
| - void onOperationResult(long itemsEmitted, OperationResult operation); |
30 |
| - } |
31 |
| - |
32 |
| - private final WeakReference<BackPressureResultListener> backPressureListener; |
33 |
| - private final String name; |
34 |
| - private final long itemsRequested; |
35 |
| - private boolean shouldRequestItem; |
36 |
| - |
37 |
| - private long itemsEmitted = 0; |
38 |
| - |
39 |
| - BackpressureSubscriber(BackPressureResultListener listener, String name) { |
40 |
| - this(listener, name, Long.MAX_VALUE); |
41 |
| - } |
42 |
| - |
43 |
| - BackpressureSubscriber(BackPressureResultListener listener, String name, long requestedItems) { |
44 |
| - this.backPressureListener = new WeakReference<>(listener); |
45 |
| - this.name = name; |
46 |
| - this.itemsRequested = requestedItems; |
47 |
| - this.shouldRequestItem = requestedItems != Long.MAX_VALUE; |
48 |
| - } |
49 |
| - |
50 |
| - @Override public void onStart() { |
51 |
| - requestItems(); |
52 |
| - sendStartData(); |
53 |
| - } |
54 |
| - |
55 |
| - @Override public void onNext(T item) { |
56 |
| - itemsEmitted++; |
57 |
| - sendProgressData(itemsEmitted); |
58 |
| - requestItems(); |
59 |
| - } |
60 |
| - |
61 |
| - @Override public void onCompleted() { |
62 |
| - sendResultData(OperationResult.success()); |
63 |
| - } |
64 |
| - |
65 |
| - @Override public void onError(Throwable throwable) { |
66 |
| - if (throwable instanceof MissingBackpressureException) { |
67 |
| - sendResultData(OperationResult.failure(throwable)); |
| 27 | + interface BackPressureResultListener { |
| 28 | + void onOperationStart(String name, long itemsRequested); |
| 29 | + |
| 30 | + void onOperationProgress(long itemsProcessedSoFar); |
| 31 | + |
| 32 | + void onOperationResult(long itemsEmitted, OperationResult operation); |
| 33 | + } |
| 34 | + |
| 35 | + private final WeakReference<BackPressureResultListener> backPressureListener; |
| 36 | + private final String name; |
| 37 | + private final long itemsRequested; |
| 38 | + private boolean shouldRequestItem; |
| 39 | + |
| 40 | + private long itemsEmitted = 0; |
| 41 | + private long itemsRequestedProcessed = 0; |
| 42 | + |
| 43 | + BackpressureSubscriber(BackPressureResultListener listener, String name) { |
| 44 | + this(listener, name, Long.MAX_VALUE); |
68 | 45 | }
|
69 |
| - } |
70 | 46 |
|
71 |
| - private void requestItems() { |
72 |
| - if (shouldRequestItem) { |
73 |
| - request(itemsRequested); |
| 47 | + BackpressureSubscriber(BackPressureResultListener listener, String name, long requestedItems) { |
| 48 | + this.backPressureListener = new WeakReference<>(listener); |
| 49 | + this.name = name; |
| 50 | + this.itemsRequested = requestedItems; |
| 51 | + this.shouldRequestItem = requestedItems != Long.MAX_VALUE; |
74 | 52 | }
|
75 |
| - } |
76 | 53 |
|
77 |
| - private void sendStartData() { |
78 |
| - if (backPressureListener.get() != null) { |
79 |
| - backPressureListener.get().onOperationStart(name, itemsRequested); |
| 54 | + @Override |
| 55 | + public void onStart() { |
| 56 | + if (shouldRequestItem) { |
| 57 | + //setProducer(new QueuedValueProducer<>(this)); |
| 58 | +// setProducer(new BackPressureProducer()); |
| 59 | + request(itemsRequested); |
| 60 | + } |
| 61 | + sendStartData(); |
80 | 62 | }
|
81 |
| - } |
82 | 63 |
|
83 |
| - private void sendProgressData(long itemsProcessedSoFar) { |
84 |
| - if (backPressureListener.get() != null) { |
85 |
| - backPressureListener.get().onOperationProgress(itemsProcessedSoFar); |
| 64 | + @Override |
| 65 | + public void onNext(T item) { |
| 66 | + itemsEmitted++; |
| 67 | + sendProgressData(itemsEmitted); |
| 68 | + requestMoreItems(); |
86 | 69 | }
|
87 |
| - } |
88 | 70 |
|
89 |
| - private void sendResultData(OperationResult operation) { |
90 |
| - if (backPressureListener.get() != null) { |
91 |
| - backPressureListener.get().onOperationResult(itemsEmitted, operation); |
| 71 | + @Override |
| 72 | + public void onCompleted() { |
| 73 | + sendResultData(OperationResult.success()); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onError(Throwable throwable) { |
| 78 | + if (throwable instanceof MissingBackpressureException) { |
| 79 | + sendResultData(OperationResult.failure(throwable)); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private void requestMoreItems() { |
| 84 | + itemsRequestedProcessed++; |
| 85 | + if (shouldRequestItem && itemsRequestedProcessed >= itemsRequested) { |
| 86 | + request(itemsRequested); |
| 87 | + itemsRequestedProcessed = 0; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private void sendStartData() { |
| 92 | + if (backPressureListener.get() != null) { |
| 93 | + backPressureListener.get().onOperationStart(name, itemsRequested); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private void sendProgressData(long itemsProcessedSoFar) { |
| 98 | + if (backPressureListener.get() != null) { |
| 99 | + backPressureListener.get().onOperationProgress(itemsProcessedSoFar); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private void sendResultData(OperationResult operation) { |
| 104 | + if (backPressureListener.get() != null) { |
| 105 | + backPressureListener.get().onOperationResult(itemsEmitted, operation); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private static class BackPressureProducer<T> implements Producer { |
| 110 | + private final Subscriber<T> subscriber; |
| 111 | + |
| 112 | + BackPressureProducer(Subscriber subscriber) { |
| 113 | + this.subscriber = subscriber; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void request(long requested) { |
| 118 | + for (int i = 0; i < requested && !subscriber.isUnsubscribed(); i++) { |
| 119 | +// subscriber.onNext(T); |
| 120 | + } |
| 121 | + } |
92 | 122 | }
|
93 |
| - } |
94 | 123 | }
|
0 commit comments