Commits

Sean Wilkinson  committed edcccb8

Minor edits for the `uuid` method for readability and assorted comments

  • Participants
  • Parent commits 397a486

Comments (0)

Files changed (1)

File src/quanah.js

 //
 //  This file is also available from git.io/q.js and bit.ly/quanahjs :-P
 //
-//  NOTE: What happens in Quanah if a variable which has already failed is used
-//  in a `when` statement? Does the `when` fail immediately, as expected?
-//
 //                                                      ~~ (c) SRW, 14 Nov 2012
-//                                                  ~~ last updated 06 Feb 2013
+//                                                  ~~ last updated 12 Feb 2013
 
 (function () {
     'use strict';
  // Prerequisites
 
     if (Object.prototype.hasOwnProperty('Q') === true) {
+     // Exit early if Quanah's "Method Q" is already present.
         return;
     }
 
  // Declarations
 
-    var AVar, avar, can_run_remotely, def, is_Function, quanah, queue, revive,
+    var AVar, avar, can_run_remotely, def, is_Function, queue, revive,
         run_locally, run_remotely, user_defs, uuid, when;
 
  // Definitions
         return ((typeof f === 'function') && (f instanceof Function));
     };
 
-    quanah = function (f) {
-     // This function acts as the "namespace" for Quanah, but it is far
-     // more useful when assigned to `Object.prototype.Q`, because then
-     // it can be used as a method of any native value except `null` and
-     // `undefined`. It expects its argument to be a function of a single
-     // variable or else an avar whose `val` property is such a function.
-        var x = (this instanceof AVar) ? this : avar({val: this});
-        x.comm({'add_to_queue': f});
-        return x;
-    };
-
     queue = [];
 
     revive = function () {
 
     uuid = function () {
      // This function generates random hexadecimal UUIDs of length 32.
-        var y = Math.random().toString(16);
-        if (y.length === 1) {
+        var y = Math.random().toString(16).slice(2, 32);
+        if (y === '') {
          // This shouldn't ever happen in JavaScript, but Adobe/Mozilla Tamarin
          // has some weird quirks due to its ActionScript roots.
-            y = '';
             while (y.length < 32) {
                 y += (Math.random() * 1e16).toString(16);
             }
             y = y.slice(0, 32);
         } else {
          // Every other JS implementation I have tried will use this instead.
-            y = y.slice(2, 32);
             while (y.length < 32) {
                 y += Math.random().toString(16).slice(2, 34 - y.length);
             }
      // immediate usefulness of this ability may not be obvious, it will turn
      // out to be crucially important for expressing certain concurrency
      // patterns idiomatically :-)
+     //
+     // NOTE: What happens here if an avar which has already failed is used in
+     // a `when` statement? Does the `when` fail immediately, as expected?
+     //
         var args, flag, i, stack, temp, x, y;
         args = Array.prototype.slice.call(arguments);
         stack = args.slice();
              // prevent further execution involving `val` until after we call
              // the input argument `f`.
                 egress.push(evt);
-                count();
-                return;
+                return count();
             };
             count = function () {
              // This function is a simple counting semaphore that closes over
                         return evt.stay(message);
                     }
                 });
-                return;                 //- NOTE: I removed an extra `revive`.
+                return;
             }});
             return y;
         };
     AVar.prototype.revive = function () {
      // This function is syntactic sugar for triggering a `revive` from code
      // external to this giant anonymous closure. Currently, the same effect
-     // can be achieved by invoking `x.comm()` for some avar `x`, but that
-     // technique is deprecated.
+     // can be achieved by invoking the `comm` instance method of an avar,
+     // but that technique is deprecated.
         return revive();
     };
 
 
  // Out-of-scope definitions
 
-    Object.prototype.Q = quanah;
+    Object.prototype.Q = function (f) {
+     // This function acts as a "namespace" for Quanah as well as its most
+     // important syntactic sugar -- "Method Q". This function can be used as
+     // a method of any native value except `null` or `undefined`. It expects
+     // its argument to be a function of a single variable or else an avar with
+     // such a function as its `val`.
+        var x = (this instanceof AVar) ? this : avar({val: this});
+        x.comm({'add_to_queue': f});
+        return x;
+    };
 
     Object.prototype.Q.avar = avar;
 
     Object.prototype.Q.when = when;
 
     (function () {
-     // This function only runs in Node.js.
+     // This function runs in Node.js, PhantomJS, and RingoJS, which means it
+     // may work with other CommonJS-ish package loaders, too. I am not certain
+     // whether this function adds much value, however, because the mere act of
+     // loading Quanah loads "Method Q" anyway ...
         /*jslint node: true */
         if (typeof module === 'object') {
             module.exports = Object.prototype.Q;