Skip to content

Commit

Permalink
IOSSchedulers for RoboVM
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleyj committed Jun 6, 2014
1 parent 022ae66 commit 5dbb81f
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 0 deletions.
39 changes: 39 additions & 0 deletions rxjava-contrib/rxjava-ios/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
apply plugin: 'osgi'

dependencies {
compile project(':rxjava-core')

// testing
provided 'junit:junit-dep:4.10'
compile 'org.robovm:robovm-rt:0.0.12'
compile 'org.robovm:robovm-objc:0.0.12'
compile 'org.robovm:robovm-cocoatouch:0.0.12'
}

javadoc {
options {
doclet = "org.benjchristensen.doclet.DocletExclude"
docletpath = [rootProject.file('./gradle/doclet-exclude.jar')]
stylesheetFile = rootProject.file('./gradle/javadocStyleSheet.css')
windowTitle = "RxJava iOS Javadoc ${project.version}"
}
options.addStringOption('top').value = '<h2 class="title" style="padding-top:40px">RxJava iOS</h2>'
}

jar {
manifest {
name = 'rxjava-ios'
instruction 'Bundle-Vendor', 'Netflix'
instruction 'Bundle-DocURL', 'https://github.com/Netflix/RxJava'
instruction 'Import-Package', '!org.junit,!junit.framework,!org.mockito.*,*'
instruction 'Fragment-Host', 'com.netflix.rxjava.core'
}
}

test {
testLogging {
exceptionFormat "full"
events "started"
displayGranularity 2
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package rx.ios.schedulers;
/**
* Copyright 2013 Netflix, Inc.
* Copyright 2014 Ashley Williams
*
* 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.
*/


import org.robovm.apple.foundation.NSBlockOperation;
import org.robovm.apple.foundation.NSOperationQueue;
import rx.Scheduler;
import rx.Subscription;
import rx.functions.Action0;
import rx.subscriptions.BooleanSubscription;
import rx.subscriptions.Subscriptions;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
* Schedules actions to run on an iOS Handler thread.
*/
public class HandlerThreadScheduler extends Scheduler {

private final NSOperationQueue operationQueue;

public HandlerThreadScheduler(NSOperationQueue operationQueue) {
this.operationQueue = operationQueue;
}

@Override
public Worker createWorker() {
return new InnerHandlerThreadScheduler(operationQueue);
}


private static class InnerHandlerThreadScheduler extends Worker {

private final NSOperationQueue operationQueue;
private BooleanSubscription innerSubscription = new BooleanSubscription();


public InnerHandlerThreadScheduler(NSOperationQueue operationQueue) {
this.operationQueue = operationQueue;
}

@Override
public void unsubscribe() {
innerSubscription.unsubscribe();
}

@Override
public boolean isUnsubscribed() {
return innerSubscription.isUnsubscribed();
}

@Override
public Subscription schedule(Action0 action0) {
return schedule(action0, 0, TimeUnit.MILLISECONDS);
}

@Override
public Subscription schedule(final Action0 action, long delayTime, TimeUnit unit) {

ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
final NSBlockOperation runOperation = new NSBlockOperation();

executor.schedule(new Runnable() {
@Override
public void run() {
if (isUnsubscribed()) {
return;
}
/* Runnable for action */
final Runnable actionRunner = new Runnable() {
@Override
public void run() {
action.call();
}
};

runOperation.addExecutionBlock$(actionRunner);

/* Add operation to operation queue*/
operationQueue.addOperation(runOperation);
}
}, delayTime, unit);

return Subscriptions.create(new Action0() {
@Override
public void call() {
runOperation.cancel();
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright 2013 Netflix, Inc.
* Copyright 2014 Ashley Williams
*
* 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 rx.ios.schedulers;

import rx.Scheduler;
import org.robovm.apple.foundation.NSOperationQueue;


public class IOSSchedulers {

private static final Scheduler MAIN_THREAD_SCHEDULER =
new HandlerThreadScheduler((NSOperationQueue) NSOperationQueue.getMainQueue());

private IOSSchedulers(){

}


public static Scheduler handlerThread(final NSOperationQueue operationQueue) {
return new HandlerThreadScheduler(operationQueue);
}

public static Scheduler mainThread() {
return MAIN_THREAD_SCHEDULER;
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ include 'rxjava-core', \
'language-adaptors:rxjava-kotlin', \
'rxjava-contrib:rxjava-swing', \
//'rxjava-contrib:rxjava-javafx', \
'rxjava-contrib:rxjava-ios', \
'rxjava-contrib:rxjava-android', \
'rxjava-contrib:rxjava-android-samples-build-wrapper', \
'rxjava-contrib:rxjava-apache-http', \
Expand Down

0 comments on commit 5dbb81f

Please sign in to comment.