gscript r10 - trunk



Author: alexl
Date: Tue Sep 16 18:24:27 2008
New Revision: 10
URL: http://svn.gnome.org/viewvc/gscript?rev=10&view=rev

Log:
2008-09-16  Alexander Larsson  <alexl redhat com>

        * gtk.js:
	Convert to AsyncRunner class




Modified:
   trunk/ChangeLog
   trunk/gtk.js

Modified: trunk/gtk.js
==============================================================================
--- trunk/gtk.js	(original)
+++ trunk/gtk.js	Tue Sep 16 18:24:27 2008
@@ -1,4 +1,4 @@
-/* Simulate GIO-style async operation using idles */
+/* Simulate a GIO-style async operation using idles */
 
 function open_async(filename, flags, cb) {
     function on_idle() {
@@ -19,10 +19,19 @@
     return res.data;
 }
 
-/* Wrap the GIO-style async op for the yield format */
+/* Define AsyncRunner class */
 
-function async_curry (fn, fn_finish) {
-    /* We return a curried version of open_async with everything but the
+function AsyncRunner(_fn) {
+    this.fn = _fn;
+}
+
+AsyncRunner.prototype.toString = function(cb) {
+    return "[AsyncRunner]";
+}
+
+/* static function to help wrapping async functions */
+AsyncRunner.curry_function = function (fn, fn_finish) {
+    /* We return a curried version of foo_async with everything but the
      * callback argument bound. Plus we pass the (curried) paired finish method as a
      * property of the return value function.
      */
@@ -41,16 +50,12 @@
     return curried;
 }
 
-function $open (filename, flags) {
-    return async_curry (open_async, open_finish, filename, flags);
-}
-
 /* This function lets us run a generator function as an async operation,
    handling all the callbacks from the yields. You can also pass in
    a callback to run when the operation is done and a list of arguments
    for the generator */
 
-function do_async (fun, cb /* , args ... */) {
+AsyncRunner.prototype.start = function (cb /* , args ... */) {
     function async_cb (obj, res) {
 	let ret;
 	try {
@@ -67,25 +72,27 @@
 	    cb ();
     }
     let extra_args = [];
-    for (let i = 2; i < arguments.length; i++)
-	extra_args[i-2] = arguments[i];
-    let gen = fun.apply (this, extra_args);
+    for (let i = 1; i < arguments.length; i++)
+	extra_args[i-1] = arguments[i];
+    let gen = this.fn.apply (this, extra_args);
     let f = gen.next();
     f (async_cb);
 }
 
 /* Lets you call other async generators from an async generator */
 
-function call_async (fun) {
-    var args = [fun, null];
-    for (let i = 1; i < arguments.length; i++)
-	args[i+1] = arguments[i];
+    AsyncRunner.prototype.call = function (/* args... */) {
+    var args = [null];
+    var runner = this;
+    for (let i = 0; i < arguments.length; i++)
+	args.push(arguments[i]);
+    
     let f = function (cb) {
 	function wrap_cb () {
 	    cb (null, null);
 	}
-	args[1] = wrap_cb;
-	do_async.apply (this, args);
+	args[0] = wrap_cb;
+	runner.start.apply (runner, args);
     }
     f.finish = function (res) {
 	return undefined;
@@ -93,25 +100,30 @@
     return f;
 }
 
-/* Test this stuff */
+/* Wrap the GIO-style async op for the AsyncRunner yield based format */
 
-function do_stuff2 () {
-    print ("do_stuff2");
-    var data3 = yield $open ("other", 0);
-    print ("data3 = " + data3);
-    print ("end of do_stuff2");
-}
-
-function do_stuff (filename1, filename2) {
-    print ("do_stuff");
-    var data = yield $open (filename1, 0);
-    print ("data = " + data);
-    var data2 = yield $open (filename2, 0);
-    print ("data2 = " + data2);
-    var ret = yield call_async (do_stuff2);
-    print ("ret: " + ret);
-    print ("end of do_stuff");
+function $open (filename, flags) {
+    return AsyncRunner.curry_function (open_async, open_finish, filename, flags);
 }
+    
+/* Test this stuff */
 
-do_async (do_stuff, null, "test.txt", "test2.txt");
+do_stuff2 = new AsyncRunner (function (filename) {
+	print ("do_stuff2");
+	var data3 = yield $open (filename, 0);
+	print ("data3 = " + data3);
+	print ("end of do_stuff2");
+    });
+
+do_stuff = new AsyncRunner (function  (filename1, filename2) {
+	print ("do_stuff");
+	var data = yield $open (filename1, 0);
+	print ("data = " + data);
+	var data2 = yield $open (filename2, 0);
+	print ("data2 = " + data2);
+	var ret = yield do_stuff2.call("other.txt");
+	print ("ret: " + ret);
+	print ("end of do_stuff");
+    });
 
+do_stuff.start(null, "test.txt", "test2.txt");



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]