Browse Source

Remove Function.prototype.sync/bind and Sync.sync and replace with just Sync as the one-simple-great way to make things sync.

Edward Lee 15 years ago
parent
commit
f9c1c37881
2 changed files with 17 additions and 69 deletions
  1. 6 39
      Sync.js
  2. 11 30
      test/unit/test_Sync.js

+ 6 - 39
Sync.js

@@ -47,16 +47,15 @@ const Cu = Components.utils;
  * Make a synchronous version of the function object that will be called with
  * the provided thisArg.
  *
- * @param this {Function}
+ * @param func {Function}
  *        The asynchronous function to make a synchronous function
  * @param thisArg {Object} [optional]
  *        The object that the function accesses with "this"
- * @usage let syncFunc = syncBind.call(asyncFunc, thisArg);
+ * @usage let ret = Sync(asyncFunc, obj)(arg1, arg2);
+ * @usage let ret = Sync(ignoreThisFunc)(arg1, arg2);
+ * @usage let sync = Sync(async); let ret = sync(arg1, arg2);
  */
-function syncBind(thisArg) {
-  // Save for which function we're creating a sync version
-  let func = this;
-
+function Sync(func, thisArg) {
   // Hold the value passed in from the callback to return
   let retval;
 
@@ -130,38 +129,6 @@ function sleep(callback, milliseconds) {
   setTimeout(callback, milliseconds);
 }
 
-/**
- * Prepare the Function object to make synchronous functions
- *
- * @usage Cu.import(".../Sync.js"); Sync(Function);
- */
-let Sync = function Sync(Function) {
-  // Basic case with undefined/global for thisArg for the synchronous function
-  // @usage let ret = ignoreThisFunc.sync(arg1, arg2);
-  // @usage let func = ignoreThisFunc.sync; let ret = func(arg1, arg2);
-  Function.prototype.__defineGetter__("sync", syncBind);
-
-  // Allow binding of an arbitrary thisArg for the synchronous function
-  // @usage let ret = obj.asyncFunc.syncBind(obj)(arg1, arg2);
-  // @usage let func = obj.asyncFunc.syncBind(obj); let ret = func(arg1, arg2);
-  Function.prototype.syncBind = syncBind;
-};
-
-/**
- * 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);
-
-// 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
  * without halting the current thread.
@@ -176,4 +143,4 @@ Sync(Function);
  * @param milliseconds {Number}
  *        The number of milliseconds to sleep
  */
-Sync.sleep = sleep.sync;
+Sync.sleep = Sync(sleep);

+ 11 - 30
test/unit/test_Sync.js

@@ -1,5 +1,4 @@
 Components.utils.import("resource://jsmodules/Sync.js");
-Sync(Function);
 
 // Helper function to check how long a function takes to run
 function time(func) {
@@ -27,45 +26,27 @@ function test_Sync_sleep() {
   do_check_true(duration >= 100);
 }
 
-// Check that the Function.prototype version of sync works
-function test_Function_prototype_sync() {
+// Check that we can create a sync. function
+function test_Sync() {
   let duration = time(function() {
-    let sum = slowAdd.sync(100, 1, 10);
+    let sum = Sync(slowAdd)(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() {
+// Check that we can create a sync. function that gets "this" set
+function test_Sync_this() {
   let duration = time(function() {
-    let sum = Sync.sync(slowAdd)(100, -123, 123);
-    do_check_eq(sum, 0);
-  });
-  do_check_true(duration >= 100);
-}
-
-// Check that the Function.prototype version of syncBind works
-function test_Function_prototype_syncBind() {
-  let duration = time(function() {
-    let val = slowThisGet.syncBind({ five: 5 })(100, "five");
+    let val = Sync(slowThisGet, { five: 5 })(100, "five");
     do_check_eq(val, 5);
   });
   do_check_true(duration >= 100);
 }
 
-// Check that the non-Function.prototype version of syncBind works
-function test_Sync_sync_bind() {
-  let duration = time(function() {
-    let val = Sync.sync(slowThisGet, { foo: "bar" })(100, "foo");
-    do_check_eq(val, "bar");
-  });
-  do_check_true(duration >= 100);
-}
-
 // Check that sync. function callbacks can be extracted
-function test_Function_prototype_sync_onComplete() {
-  let add = slowAdd.sync;
+function test_Sync_onComplete() {
+  let add = Sync(slowAdd);
   let duration = time(function() {
     let sum = add(add.onComplete, 100, 1000, 234);
     do_check_eq(sum, 1234);
@@ -74,10 +55,10 @@ function test_Function_prototype_sync_onComplete() {
 }
 
 // Test sync of async function that indirectly takes the callback
-function test_Function_prototype_sync_onComplete_indirect() {
-  let square = (function(obj) {
+function test_Sync_onComplete_indirect() {
+  let square = Sync(function(obj) {
     setTimeout(function() obj.done(obj.num * obj.num), obj.wait);
-  }).sync;
+  });
 
   let thing = {
     done: square.onComplete,