Skip to content

Commit 5dbb81f

Browse files
committed
IOSSchedulers for RoboVM
1 parent 022ae66 commit 5dbb81f

File tree

4 files changed

+189
-0
lines changed

4 files changed

+189
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
apply plugin: 'osgi'
2+
3+
dependencies {
4+
compile project(':rxjava-core')
5+
6+
// testing
7+
provided 'junit:junit-dep:4.10'
8+
compile 'org.robovm:robovm-rt:0.0.12'
9+
compile 'org.robovm:robovm-objc:0.0.12'
10+
compile 'org.robovm:robovm-cocoatouch:0.0.12'
11+
}
12+
13+
javadoc {
14+
options {
15+
doclet = "org.benjchristensen.doclet.DocletExclude"
16+
docletpath = [rootProject.file('./gradle/doclet-exclude.jar')]
17+
stylesheetFile = rootProject.file('./gradle/javadocStyleSheet.css')
18+
windowTitle = "RxJava iOS Javadoc ${project.version}"
19+
}
20+
options.addStringOption('top').value = '<h2 class="title" style="padding-top:40px">RxJava iOS</h2>'
21+
}
22+
23+
jar {
24+
manifest {
25+
name = 'rxjava-ios'
26+
instruction 'Bundle-Vendor', 'Netflix'
27+
instruction 'Bundle-DocURL', 'https://github.com/Netflix/RxJava'
28+
instruction 'Import-Package', '!org.junit,!junit.framework,!org.mockito.*,*'
29+
instruction 'Fragment-Host', 'com.netflix.rxjava.core'
30+
}
31+
}
32+
33+
test {
34+
testLogging {
35+
exceptionFormat "full"
36+
events "started"
37+
displayGranularity 2
38+
}
39+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package rx.ios.schedulers;
2+
/**
3+
* Copyright 2013 Netflix, Inc.
4+
* Copyright 2014 Ashley Williams
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
20+
import org.robovm.apple.foundation.NSBlockOperation;
21+
import org.robovm.apple.foundation.NSOperationQueue;
22+
import rx.Scheduler;
23+
import rx.Subscription;
24+
import rx.functions.Action0;
25+
import rx.subscriptions.BooleanSubscription;
26+
import rx.subscriptions.Subscriptions;
27+
28+
import java.util.concurrent.Executors;
29+
import java.util.concurrent.ScheduledExecutorService;
30+
import java.util.concurrent.TimeUnit;
31+
32+
/**
33+
* Schedules actions to run on an iOS Handler thread.
34+
*/
35+
public class HandlerThreadScheduler extends Scheduler {
36+
37+
private final NSOperationQueue operationQueue;
38+
39+
public HandlerThreadScheduler(NSOperationQueue operationQueue) {
40+
this.operationQueue = operationQueue;
41+
}
42+
43+
@Override
44+
public Worker createWorker() {
45+
return new InnerHandlerThreadScheduler(operationQueue);
46+
}
47+
48+
49+
private static class InnerHandlerThreadScheduler extends Worker {
50+
51+
private final NSOperationQueue operationQueue;
52+
private BooleanSubscription innerSubscription = new BooleanSubscription();
53+
54+
55+
public InnerHandlerThreadScheduler(NSOperationQueue operationQueue) {
56+
this.operationQueue = operationQueue;
57+
}
58+
59+
@Override
60+
public void unsubscribe() {
61+
innerSubscription.unsubscribe();
62+
}
63+
64+
@Override
65+
public boolean isUnsubscribed() {
66+
return innerSubscription.isUnsubscribed();
67+
}
68+
69+
@Override
70+
public Subscription schedule(Action0 action0) {
71+
return schedule(action0, 0, TimeUnit.MILLISECONDS);
72+
}
73+
74+
@Override
75+
public Subscription schedule(final Action0 action, long delayTime, TimeUnit unit) {
76+
77+
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
78+
final NSBlockOperation runOperation = new NSBlockOperation();
79+
80+
executor.schedule(new Runnable() {
81+
@Override
82+
public void run() {
83+
if (isUnsubscribed()) {
84+
return;
85+
}
86+
/* Runnable for action */
87+
final Runnable actionRunner = new Runnable() {
88+
@Override
89+
public void run() {
90+
action.call();
91+
}
92+
};
93+
94+
runOperation.addExecutionBlock$(actionRunner);
95+
96+
/* Add operation to operation queue*/
97+
operationQueue.addOperation(runOperation);
98+
}
99+
}, delayTime, unit);
100+
101+
return Subscriptions.create(new Action0() {
102+
@Override
103+
public void call() {
104+
runOperation.cancel();
105+
}
106+
});
107+
}
108+
}
109+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2013 Netflix, Inc.
3+
* Copyright 2014 Ashley Williams
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package rx.ios.schedulers;
18+
19+
import rx.Scheduler;
20+
import org.robovm.apple.foundation.NSOperationQueue;
21+
22+
23+
public class IOSSchedulers {
24+
25+
private static final Scheduler MAIN_THREAD_SCHEDULER =
26+
new HandlerThreadScheduler((NSOperationQueue) NSOperationQueue.getMainQueue());
27+
28+
private IOSSchedulers(){
29+
30+
}
31+
32+
33+
public static Scheduler handlerThread(final NSOperationQueue operationQueue) {
34+
return new HandlerThreadScheduler(operationQueue);
35+
}
36+
37+
public static Scheduler mainThread() {
38+
return MAIN_THREAD_SCHEDULER;
39+
}
40+
}

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ include 'rxjava-core', \
77
'language-adaptors:rxjava-kotlin', \
88
'rxjava-contrib:rxjava-swing', \
99
//'rxjava-contrib:rxjava-javafx', \
10+
'rxjava-contrib:rxjava-ios', \
1011
'rxjava-contrib:rxjava-android', \
1112
'rxjava-contrib:rxjava-android-samples-build-wrapper', \
1213
'rxjava-contrib:rxjava-apache-http', \

0 commit comments

Comments
 (0)