/* * Copyright (c) 2014 OpenSilk Productions LLC * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.opensilk.common.core.rx; import java.util.concurrent.TimeUnit; import rx.Scheduler; import rx.Subscription; import rx.functions.Action0; import rx.internal.schedulers.NewThreadWorker; import rx.internal.schedulers.ScheduledAction; import rx.internal.util.RxThreadFactory; import rx.subscriptions.CompositeSubscription; import rx.subscriptions.Subscriptions; /** * Created by drew on 12/24/14. */ public class SingleThreadScheduler extends Scheduler { private static final String THREAD_NAME_PREFIX = "RxSingleThreadWorker-"; private static final RxThreadFactory THREAD_FACTORY = new RxThreadFactory(THREAD_NAME_PREFIX); final NewThreadWorker worker; public SingleThreadScheduler() { worker = new NewThreadWorker(THREAD_FACTORY); } @Override public Worker createWorker() { return new WorkerWrapper(worker); } private static class WorkerWrapper extends Scheduler.Worker { private final CompositeSubscription innerSubscription = new CompositeSubscription(); private final NewThreadWorker worker; WorkerWrapper(NewThreadWorker worker) { this.worker = worker; } @Override public void unsubscribe() { innerSubscription.unsubscribe(); } @Override public boolean isUnsubscribed() { return innerSubscription.isUnsubscribed(); } @Override public Subscription schedule(Action0 action) { return schedule(action, 0, null); } @Override public Subscription schedule(Action0 action, long delayTime, TimeUnit unit) { if (innerSubscription.isUnsubscribed()) { // don't schedule, we are unsubscribed return Subscriptions.empty(); } ScheduledAction s = worker.scheduleActual(action, delayTime, unit); innerSubscription.add(s); s.addParent(innerSubscription); return s; } } }