Compare commits

...

14 Commits

Author SHA1 Message Date
Denis Karmyshakov 6d83eb59bd remove targetSdkVersion 2017-06-09 16:01:26 +03:00
Elena Bobkova 635d053a60 build Tools Update 2017-06-02 19:21:15 +03:00
Ilia Kurtov d8a720567d changed to one thread back 2017-05-29 18:45:43 +03:00
Ilia Kurtov 6eaa7fe987 static 2017-05-26 17:05:26 +03:00
Ilia Kurtov 5af24c4d30 change threads 2017-05-26 16:53:34 +03:00
Gavriil 4d6d9ea64e Merge pull request #29 from TouchInstinct/feature/thread-priority
change executor to single thread and add priority for execution
2017-05-25 16:24:08 +03:00
Ilia Kurtov 2f39f2a3fb change executor to single thread and add priority for execution 2017-05-25 16:15:25 +03:00
Ilia Kurtov cf7087417f fixed schedulers 2017-05-23 19:39:09 +03:00
Gavriil Sitnikov 42ac5d6130 fixed task unsubscribing 2017-04-07 19:33:40 +03:00
Gavriil Sitnikov 3819f51962 static fixes 2017-04-04 21:29:57 +03:00
Gavriil Sitnikov 95a8ceef0b no-class of 1.2.9 rxjava crash fixed 2017-04-04 20:55:38 +03:00
Gavriil Sitnikov ab3a16136c add task subclass to sequence executor 2017-04-04 18:22:29 +03:00
Gavriil Sitnikov e67daf7805 onerror/complete callbacks added to Sequence executor 2017-04-04 17:05:49 +03:00
Gavriil Sitnikov 3196ee4271 for boom 2017-04-04 16:31:01 +03:00
4 changed files with 138 additions and 5 deletions

View File

@ -3,7 +3,7 @@ apply plugin: 'me.tatarka.retrolambda'
android { android {
compileSdkVersion 25 compileSdkVersion 25
buildToolsVersion '25.0.2' buildToolsVersion '25.0.3'
compileOptions { compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8 sourceCompatibility JavaVersion.VERSION_1_8
@ -11,8 +11,7 @@ android {
} }
defaultConfig { defaultConfig {
minSdkVersion 9 minSdkVersion 16
targetSdkVersion 25
} }
} }

View File

@ -0,0 +1,78 @@
package ru.touchin.roboswag.core.observables;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import ru.touchin.roboswag.core.utils.ProcessPriorityThreadFactory;
import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Actions;
import rx.schedulers.Schedulers;
//TODO: errors/next/completion/single/observable/block next after first fail
public class SequenceObservableExecutor {
private static void safeUnsubscribe(@Nullable final Subscription subscription) {
if (subscription != null && !subscription.isUnsubscribed()) {
subscription.unsubscribe();
}
}
@NonNull
private final Scheduler sendingScheduler = Schedulers.from(Executors.newSingleThreadExecutor());
@NonNull
private final Scheduler executeScheduler = Schedulers.from(Executors.newSingleThreadExecutor(
new ProcessPriorityThreadFactory(Thread.MIN_PRIORITY)));
@NonNull
public Observable<?> execute(@NonNull final Observable<?> completable) {
final Task task = new Task(completable);
return Observable
.create(task)
.doOnUnsubscribe(task::cancel);
}
private class Task implements Observable.OnSubscribe<Subscriber> {
@NonNull
private final Observable<?> completable;
@Nullable
private Subscription scheduleSubscription;
@Nullable
private Subscription executeSubscription;
public Task(@NonNull final Observable<?> completable) {
this.completable = completable;
}
@Override
public void call(@NonNull final Subscriber subscriber) {
scheduleSubscription = sendingScheduler.createWorker().schedule(() -> {
final CountDownLatch blocker = new CountDownLatch(1);
executeSubscription = completable
.subscribeOn(executeScheduler)
.doOnUnsubscribe(blocker::countDown)
.subscribe(Actions.empty(), subscriber::onError, subscriber::onCompleted);
try {
blocker.await();
} catch (final InterruptedException exception) {
safeUnsubscribe(executeSubscription);
subscriber.onError(exception);
}
});
}
public void cancel() {
safeUnsubscribe(scheduleSubscription);
safeUnsubscribe(executeSubscription);
}
}
}

View File

@ -26,6 +26,7 @@ import java.lang.reflect.Type;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import ru.touchin.roboswag.core.log.Lc;
import ru.touchin.roboswag.core.log.LcGroup; import ru.touchin.roboswag.core.log.LcGroup;
import ru.touchin.roboswag.core.observables.OnSubscribeRefCountWithCacheTime; import ru.touchin.roboswag.core.observables.OnSubscribeRefCountWithCacheTime;
import ru.touchin.roboswag.core.observables.storable.builders.NonNullStorableBuilder; import ru.touchin.roboswag.core.observables.storable.builders.NonNullStorableBuilder;
@ -35,6 +36,7 @@ import rx.Observable;
import rx.Scheduler; import rx.Scheduler;
import rx.Single; import rx.Single;
import rx.exceptions.OnErrorThrowable; import rx.exceptions.OnErrorThrowable;
import rx.functions.Actions;
import rx.schedulers.Schedulers; import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject; import rx.subjects.PublishSubject;
@ -293,12 +295,15 @@ public class Storable<TKey, TObject, TStoreObject> {
return internalSet(newValue, true).toObservable(); return internalSet(newValue, true).toObservable();
} }
public void setCalm(@Nullable final TObject value) {
set(value).subscribe(Actions.empty(), Lc::assertion);
}
/** /**
* Sets value synchronously. You should NOT use this method normally. Use {@link #set(Object)} asynchronously instead. * Sets value synchronously. You should NOT use this method normally. Use {@link #set(Object)} asynchronously instead.
* *
* @param newValue Value to set; * @param newValue Value to set;
*/ */
@Deprecated
//deprecation: it should be used for debug only and in very rare cases. //deprecation: it should be used for debug only and in very rare cases.
public void setSync(@Nullable final TObject newValue) { public void setSync(@Nullable final TObject newValue) {
set(newValue).toBlocking().subscribe(); set(newValue).toBlocking().subscribe();
@ -331,7 +336,6 @@ public class Storable<TKey, TObject, TStoreObject> {
* *
* @return Returns value; * @return Returns value;
*/ */
@Deprecated
//deprecation: it should be used for debug only and in very rare cases. //deprecation: it should be used for debug only and in very rare cases.
@Nullable @Nullable
public TObject getSync() { public TObject getSync() {

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2015 RoboSwag (Gavriil Sitnikov, Vsevolod Ivanov)
*
* This file is part of RoboSwag library.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package ru.touchin.roboswag.core.utils;
import android.support.annotation.NonNull;
import java.util.concurrent.ThreadFactory;
/**
* Created by Ilia Kurtov on 25/05/2017 with a help from https://stackoverflow.com/a/21187003/4312184
* ThreadFactory that can change default thread priority. Suitable for creating Schedulers fo RxJava like this:
* final Scheduler scheduler = Schedulers.from(Executors.newSingleThreadExecutor(new ProcessPriorityThreadFactory(Thread.MIN_PRIORITY)));
*/
public final class ProcessPriorityThreadFactory implements ThreadFactory {
private final int threadPriority;
/**
* threadPriority can be in a range from {@link Thread#MIN_PRIORITY} to {@link Thread#MAX_PRIORITY}
*
* @param threadPriority priority for the Thread.
*/
public ProcessPriorityThreadFactory(final int threadPriority) {
this.threadPriority = threadPriority;
}
@Override
@NonNull
public Thread newThread(@NonNull final Runnable runnable) {
final Thread thread = new Thread(runnable);
thread.setPriority(threadPriority);
return thread;
}
}