changeset 1301:a9cc35c71eb0

go back to Thread.schedule
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 13 Jan 2019 17:14:45 -0700
parents 590ad449ac7f
children 5345466fa61f
files conv.txt src/luan/modules/Thread.luan src/luan/modules/ThreadLuan.java
diffstat 3 files changed, 15 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
diff -r 590ad449ac7f -r a9cc35c71eb0 conv.txt
--- a/conv.txt	Sun Jan 13 15:31:39 2019 -0700
+++ b/conv.txt	Sun Jan 13 17:14:45 2019 -0700
@@ -1,3 +1,6 @@
+"Thread.once"
+"Thread.repeating"
+
 "Io.print()"
 "Luan.values"
 remove "as_table"
diff -r 590ad449ac7f -r a9cc35c71eb0 src/luan/modules/Thread.luan
--- a/src/luan/modules/Thread.luan	Sun Jan 13 15:31:39 2019 -0700
+++ b/src/luan/modules/Thread.luan	Sun Jan 13 17:14:45 2019 -0700
@@ -28,14 +28,9 @@
 	ThreadLuan.fork(fn,...)
 end
 
-function Thread.once(delay,fn,...)
+function Thread.schedule(delay,fn,repeating)
 	fn = safe(fn)
-	ThreadLuan.schedule(delay,false,fn,...)
-end
-
-function Thread.repeating(delay,fn,...)
-	fn = safe(fn)
-	ThreadLuan.schedule(delay,true,fn,...)
+	ThreadLuan.schedule(delay,fn,...)
 end
 
 
diff -r 590ad449ac7f -r a9cc35c71eb0 src/luan/modules/ThreadLuan.java
--- a/src/luan/modules/ThreadLuan.java	Sun Jan 13 15:31:39 2019 -0700
+++ b/src/luan/modules/ThreadLuan.java	Sun Jan 13 17:14:45 2019 -0700
@@ -48,23 +48,28 @@
 		};
 	}
 */
-	public static void schedule(LuanState luan,long delay,boolean repeat,LuanFunction fn,Object... args) {
+	public static void schedule(LuanState luan,long delay,LuanFunction fn,String repeating)
+		throws LuanException
+	{
 		LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
 		final LuanState newLuan = (LuanState)cloner.clone(luan);
 		final LuanFunction newFn = (LuanFunction)cloner.get(fn);
-		final Object[] newArgs = cloner.clone(args);
 		Runnable r = new Runnable(){public void run() {
 			try {
-				newFn.call(newLuan,newArgs);
+				newFn.call(newLuan);
 			} catch(LuanException e) {
 				e.printStackTrace();
 			}
 		}};
 		final ScheduledFuture sf;
-		if( repeat ) {
+		if( repeating==null ) {
+			sf = scheduler.schedule(r,delay,TimeUnit.MILLISECONDS);
+		} else if( repeating.equals("with_fixed_delay") ) {
 			sf = scheduler.scheduleWithFixedDelay(r,delay,delay,TimeUnit.MILLISECONDS);
+		} else if( repeating.equals("at_fixed_rate") ) {
+			sf = scheduler.scheduleAtFixedRate(r,delay,delay,TimeUnit.MILLISECONDS);
 		} else {
-			sf = scheduler.schedule(r,delay,TimeUnit.MILLISECONDS);
+			throw new LuanException("repeat must be nil or 'with_fixed_delay' or 'at_fixed_rate'");
 		}
 		final Closeable c = new Closeable(){public void close(){
 			boolean b = sf.cancel(false);