Skip to content

Commit

Permalink
scheduled events and future libs are now pure JS
Browse files Browse the repository at this point in the history
  • Loading branch information
m0wfo committed May 17, 2012
1 parent 1b61fa9 commit 3e5862c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 47 deletions.
4 changes: 3 additions & 1 deletion resources/future.js
Expand Up @@ -10,7 +10,9 @@ importClass(Packages.akka.util.Duration);
importClass(java.util.concurrent.Callable); importClass(java.util.concurrent.Callable);


var createFuture = function(work, cb) { 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) { if (callback != null) {
var callback = new JavaAdapter(OnComplete, {onComplete: cb}); var callback = new JavaAdapter(OnComplete, {onComplete: cb});
return future.andThen(callback); return future.andThen(callback);
Expand Down
26 changes: 22 additions & 4 deletions resources/timers.js
@@ -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(com.mowforth.rhinode.dispatch.Dispatch);
importClass(java.lang.Runnable); 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() { var Timers = function() {


this.setTimeout = function(callback, delay) { this.setTimeout = function(callback, delay) {
var work = new Runnable({run: callback}); [d, worker, work] = scheduleWork(callback, delay);
return Dispatch.doOnce(work, delay); return getScheduler().scheduleOnce(d, worker, work);
} }


this.clearTimeout = function(timeoutId) { this.clearTimeout = function(timeoutId) {
timeoutId.cancel(); timeoutId.cancel();
} }


this.setInterval = function(callback, delay) { this.setInterval = function(callback, delay) {
var work = new Runnable({run: callback}); [d, worker, work] = scheduleWork(callback, delay);
return Dispatch.doRegularly(work, delay); return getScheduler().schedule(Duration.Zero(),
d, worker, work);
} }


this.clearInterval = function(intervalId) { this.clearInterval = function(intervalId) {
Expand Down
42 changes: 0 additions & 42 deletions src/com/mowforth/rhinode/dispatch/Dispatch.java
Expand Up @@ -35,50 +35,8 @@ public static ActorSystem getSystem() {
return system; 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() { public static ActorRef newEventHandler() {
return system.actorOf(new Props(EventActor.class)); 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.