[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/yuilib/3.17.2/parallel/ -> parallel-debug.js (source)

   1  /*
   2  YUI 3.17.2 (build 9c3c78e)
   3  Copyright 2014 Yahoo! Inc. All rights reserved.
   4  Licensed under the BSD License.
   5  http://yuilibrary.com/license/
   6  */
   7  
   8  YUI.add('parallel', function (Y, NAME) {
   9  
  10  
  11  /**
  12  * A concurrent parallel processor to help in running several async functions.
  13  * @module parallel
  14  * @main parallel
  15  */
  16  
  17  /**
  18  A concurrent parallel processor to help in running several async functions.
  19  
  20      var stack = new Y.Parallel();
  21  
  22      for (var i = 0; i < 15; i++) {
  23          Y.io('./api/json/' + i, {
  24              on: {
  25                  success: stack.add(function() {
  26                      Y.log('Done!');
  27                  })
  28              }
  29          });
  30      }
  31  
  32      stack.done(function() {
  33          Y.log('All IO requests complete!');
  34      });
  35  
  36  @class Parallel
  37  @param {Object} o A config object
  38  @param {Object} [o.context=Y] The execution context of the callback to done
  39  
  40  
  41  */
  42  
  43  Y.Parallel = function(o) {
  44      this.config = o || {};
  45      this.results = [];
  46      this.context = this.config.context || Y;
  47      this.total = 0;
  48      this.finished = 0;
  49  };
  50  
  51  Y.Parallel.prototype = {
  52      /**
  53      * An Array of results from all the callbacks in the stack
  54      * @property results
  55      * @type Array
  56      */
  57  
  58      results: null,
  59      /**
  60      * The total items in the stack
  61      * @property total
  62      * @type Number
  63      */
  64      total: null,
  65      /**
  66      * The number of stacked callbacks executed
  67      * @property finished
  68      * @type Number
  69      */
  70      finished: null,
  71      /**
  72      * Add a callback to the stack
  73      * @method add
  74      * @param {Function} fn The function callback we are waiting for
  75      */
  76      add: function (fn) {
  77          var self = this,
  78              index = self.total;
  79  
  80          self.total += 1;
  81  
  82          return function () {
  83              self.finished++;
  84              self.results[index] = (fn && fn.apply(self.context, arguments)) ||
  85                  (arguments.length === 1 ? arguments[0] : Y.Array(arguments));
  86  
  87              self.test();
  88          };
  89      },
  90      /**
  91      * Test to see if all registered items in the stack have completed, if so call the callback to `done`
  92      * @method test
  93      */
  94      test: function () {
  95          var self = this;
  96          if (self.finished >= self.total && self.callback) {
  97              self.callback.call(self.context, self.results, self.data);
  98          }
  99      },
 100      /**
 101      * The method to call when all the items in the stack are complete.
 102      * @method done
 103      * @param {Function} callback The callback to execute on complete
 104      * @param {Mixed} callback.results The results of all the callbacks in the stack
 105      * @param {Mixed} [callback.data] The data given to the `done` method
 106      * @param {Mixed} data Mixed data to pass to the success callback
 107      */
 108      done: function (callback, data) {
 109          this.callback = callback;
 110          this.data = data;
 111          this.test();
 112      }
 113  };
 114  
 115  
 116  }, '3.17.2', {"requires": ["yui-base"]});


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1