|
| 1 | +/* |
| 2 | + * Copyright 2017 ObjectBox Ltd. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.objectbox.rx3; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import io.objectbox.query.Query; |
| 22 | +import io.objectbox.reactive.DataSubscription; |
| 23 | +import io.reactivex.rxjava3.core.BackpressureStrategy; |
| 24 | +import io.reactivex.rxjava3.core.Flowable; |
| 25 | +import io.reactivex.rxjava3.core.FlowableEmitter; |
| 26 | +import io.reactivex.rxjava3.core.Observable; |
| 27 | +import io.reactivex.rxjava3.core.Single; |
| 28 | + |
| 29 | +/** |
| 30 | + * Static methods to Rx-ify ObjectBox queries. |
| 31 | + */ |
| 32 | +public abstract class RxQuery { |
| 33 | + /** |
| 34 | + * The returned Flowable emits Query results one by one. Once all results have been processed, onComplete is called. |
| 35 | + * Uses BackpressureStrategy.BUFFER. |
| 36 | + */ |
| 37 | + public static <T> Flowable<T> flowableOneByOne(final Query<T> query) { |
| 38 | + return flowableOneByOne(query, BackpressureStrategy.BUFFER); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * The returned Flowable emits Query results one by one. Once all results have been processed, onComplete is called. |
| 43 | + * Uses given BackpressureStrategy. |
| 44 | + */ |
| 45 | + public static <T> Flowable<T> flowableOneByOne(final Query<T> query, BackpressureStrategy strategy) { |
| 46 | + return Flowable.create(emitter -> createListItemEmitter(query, emitter), strategy); |
| 47 | + } |
| 48 | + |
| 49 | + static <T> void createListItemEmitter(final Query<T> query, final FlowableEmitter<T> emitter) { |
| 50 | + final DataSubscription dataSubscription = query.subscribe().observer(data -> { |
| 51 | + for (T datum : data) { |
| 52 | + if (emitter.isCancelled()) { |
| 53 | + return; |
| 54 | + } else { |
| 55 | + emitter.onNext(datum); |
| 56 | + } |
| 57 | + } |
| 58 | + if (!emitter.isCancelled()) { |
| 59 | + emitter.onComplete(); |
| 60 | + } |
| 61 | + }); |
| 62 | + emitter.setCancellable(dataSubscription::cancel); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * The returned Observable emits Query results as Lists. |
| 67 | + * Never completes, so you will get updates when underlying data changes |
| 68 | + * (see {@link Query#subscribe()} for details). |
| 69 | + */ |
| 70 | + public static <T> Observable<List<T>> observable(final Query<T> query) { |
| 71 | + return Observable.create(emitter -> { |
| 72 | + final DataSubscription dataSubscription = query.subscribe().observer(data -> { |
| 73 | + if (!emitter.isDisposed()) { |
| 74 | + emitter.onNext(data); |
| 75 | + } |
| 76 | + }); |
| 77 | + emitter.setCancellable(dataSubscription::cancel); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * The returned Single emits one Query result as a List. |
| 83 | + */ |
| 84 | + public static <T> Single<List<T>> single(final Query<T> query) { |
| 85 | + return Single.create(emitter -> { |
| 86 | + query.subscribe().single().observer(data -> { |
| 87 | + if (!emitter.isDisposed()) { |
| 88 | + emitter.onSuccess(data); |
| 89 | + } |
| 90 | + }); |
| 91 | + // no need to cancel, single never subscribes |
| 92 | + }); |
| 93 | + } |
| 94 | +} |
0 commit comments