Browse Source

Add Sync.sync as another way to make sync. functions (in addition to Function.prototype).

Edward Lee 15 years ago
parent
commit
6ec3904ebb
2 changed files with 30 additions and 11 deletions
  1. 14 4
      Sync.js
  2. 16 7
      test/unit/test_Sync.js

+ 14 - 4
Sync.js

@@ -49,7 +49,7 @@ const Cu = Components.utils;
  *
  *
  * @param this {Function}
  * @param this {Function}
  *        The asynchronous function to make a synchronous function
  *        The asynchronous function to make a synchronous function
- * @param thisArg {Object}
+ * @param thisArg {Object} [optional]
  *        The object that the function accesses with "this"
  *        The object that the function accesses with "this"
  * @usage let syncFunc = syncBind.call(asyncFunc, thisArg);
  * @usage let syncFunc = syncBind.call(asyncFunc, thisArg);
  */
  */
@@ -135,10 +135,20 @@ let Sync = function Sync(Function) {
   Function.prototype.__defineGetter__("sync", syncBind);
   Function.prototype.__defineGetter__("sync", syncBind);
 };
 };
 
 
-// Make functions in this module be sync-able
-Sync(Function);
+/**
+ * Make a synchronous version of the provided function, optionally binding a
+ * "this" for the sync function.
+ *
+ * @param func {Function}
+ *        Async function that takes an onComplete callback as its first arg
+ * @param thisArg {Object} [optional]
+ *        The object that the function accesses with "this"
+ * @usage let ret = Sync.sync(ignoreThisFunc)(arg1, arg2);
+ */
+Sync.sync = function Sync_sync(func, thisArg) syncBind.call(func, thisArg);
 
 
-// Add additional properties to export with the Sync function/object
+// Make functions in this module be sync-able (Sync.sync does something else)
+Sync(Function);
 
 
 /**
 /**
  * Sleep the specified number of milliseconds, pausing execution of the caller
  * Sleep the specified number of milliseconds, pausing execution of the caller

+ 16 - 7
test/unit/test_Sync.js

@@ -8,6 +8,11 @@ function time(func) {
   return new Date() - startTime;
   return new Date() - startTime;
 }
 }
 
 
+// Helper function to test sync functionality
+function slowAdd(onComplete, wait, num1, num2) {
+  setTimeout(function() onComplete(num1 + num2), wait);
+}
+
 // Test the built-in Sync.sleep method.
 // Test the built-in Sync.sleep method.
 function test_Sync_sleep() {
 function test_Sync_sleep() {
   let duration = time(function() {
   let duration = time(function() {
@@ -16,16 +21,20 @@ function test_Sync_sleep() {
   do_check_true(duration >= 100);
   do_check_true(duration >= 100);
 }
 }
 
 
+// Check that the Function.prototype version of sync works
 function test_Function_prototype_sync() {
 function test_Function_prototype_sync() {
-  // Test using the Sync.sync method that was added to the Function prototype
-  // to define our own sleep method.
-
-  function sleep(callback, milliseconds) {
-    setTimeout(callback, milliseconds);
-  }
+  let duration = time(function() {
+    let sum = slowAdd.sync(100, 1, 10);
+    do_check_eq(sum, 11);
+  });
+  do_check_true(duration >= 100);
+}
 
 
+// Check that the non-Function.prototype version of sync works
+function test_Sync_sync() {
   let duration = time(function() {
   let duration = time(function() {
-    sleep.sync(100);
+    let sum = Sync.sync(slowAdd)(100, -123, 123);
+    do_check_eq(sum, 0);
   });
   });
   do_check_true(duration >= 100);
   do_check_true(duration >= 100);
 }
 }