Permalink
Browse files

scheduled events and future libs are now pure JS

  • Loading branch information...
1 parent 1b61fa9 commit 3e5862cac90691ff3f7bd9d2e2f1bcd653789851 @m0wfo committed May 17, 2012
Showing with 25 additions and 47 deletions.
  1. +3 −1 resources/future.js
  2. +22 −4 resources/timers.js
  3. +0 −42 src/com/mowforth/rhinode/dispatch/Dispatch.java
View
@@ -10,7 +10,9 @@ importClass(Packages.akka.util.Duration);
importClass(java.util.concurrent.Callable);
var createFuture = function(work, cb) {
- var future = Dispatch.future(new JavaAdapter(Callable, {call: work}));
+ var c = new JavaAdapter(Callable, {call: work});
+ var future = Futures.future(c, Dispatch.getSystem().dispatcher());
+
if (callback != null) {
var callback = new JavaAdapter(OnComplete, {onComplete: cb});
return future.andThen(callback);
View
@@ -1,20 +1,38 @@
+importClass(Packages.akka.actor.UntypedActor);
+importClass(Packages.akka.actor.UntypedActorFactory);
+importClass(Packages.akka.util.Duration);
importClass(com.mowforth.rhinode.dispatch.Dispatch);
importClass(java.lang.Runnable);
+importClass(java.util.concurrent.TimeUnit);
+
+function scheduleWork(cb, delay) {
+ var d = Duration.create(delay, TimeUnit.MILLISECONDS);
+ var work = new Runnable({run: cb});
+ var worker = new actor(function(msg) {
+ msg.run();
+ }).actor;
+ return [d, worker, work];
+};
+
+function getScheduler() {
+ return Dispatch.getSystem().scheduler();
+}
var Timers = function() {
this.setTimeout = function(callback, delay) {
- var work = new Runnable({run: callback});
- return Dispatch.doOnce(work, delay);
+ [d, worker, work] = scheduleWork(callback, delay);
+ return getScheduler().scheduleOnce(d, worker, work);
}
this.clearTimeout = function(timeoutId) {
timeoutId.cancel();
}
this.setInterval = function(callback, delay) {
- var work = new Runnable({run: callback});
- return Dispatch.doRegularly(work, delay);
+ [d, worker, work] = scheduleWork(callback, delay);
+ return getScheduler().schedule(Duration.Zero(),
+ d, worker, work);
}
this.clearInterval = function(intervalId) {
@@ -35,50 +35,8 @@ public static ActorSystem getSystem() {
return system;
}
- public static Future<Object> future(Callable<Object> work) {
- Future<Object> f = Futures.future(work, system.dispatcher());
- return f;
- }
-
- public static Agent<Object> agent(Object initial) {
- return new Agent<Object>(initial, system);
- }
-
- public static Cancellable doOnce(Runnable work, int delay) {
- Duration d = Duration.create(delay, TimeUnit.MILLISECONDS);
- return system.scheduler().scheduleOnce(d, newScheduledActor(), work);
- }
-
- public static Cancellable doRegularly(Runnable work, int delay) {
- Duration d = Duration.create(delay, TimeUnit.MILLISECONDS);
- return system.scheduler().schedule(Duration.Zero(), d, newScheduledActor(), work);
- }
-
public static ActorRef newEventHandler() {
return system.actorOf(new Props(EventActor.class));
}
- private static ActorRef newScheduledActor() {
- ActorRef actor = system.actorOf(new Props().withCreator(new UntypedActorFactory() {
- public UntypedActor create() {
- return new UntypedActor() {
- public void onReceive(Object message) {
- if (message instanceof Runnable) {
- Runnable work = (Runnable)message;
- work.run();
- } else {
- unhandled(message);
- }
- }
- };
- }
- }));
-
- return actor;
- }
-
- public static Timeout forever() {
- return new Timeout(5, TimeUnit.SECONDS);
- }
-
}

0 comments on commit 3e5862c

Please sign in to comment.