API Docs for: undefined
Show:

File: ../gallery-async/js/async.js

  1. /**
  2.  * @module gallery-async
  3.  */
  4. (function (Y, moduleName) {
  5.     'use strict';
  6.    
  7.     var _string_complete = 'complete',
  8.         _string_initOnly = 'initOnly',
  9.        
  10.         _run = {
  11.             all: '_runAll',
  12.             queue: '_runQueue'
  13.         },
  14.        
  15.         _Array = Y.Array,
  16.         _AsyncCommand = Y.AsyncCommand,
  17.         _Lang = Y.Lang,
  18.        
  19.         _createAndRun,
  20.         _each = _Array.each,
  21.         _instanceOf = Y.instanceOf,
  22.         _isArray = _Lang.isArray,
  23.         _isFunction = _Lang.isFunction,
  24.         _isString = _Lang.isString,
  25.         _map = _Array.map,
  26.         _merge = Y.merge,
  27.         _unnest = _Array.unnest,
  28.    
  29.         /**
  30.         * Asynchronous command runner class.
  31.         * @class Async
  32.         * @extends AsyncCommand
  33.         * @param {Object} config Configuration Object.
  34.         */
  35.         _class = Y.Base.create(moduleName, _AsyncCommand, [], {
  36.             initializer: function () {
  37.                 var me = this,
  38.                     run = _run[me.get('mode')];
  39.                
  40.                 if (run) {
  41.                     me._set('fn', function (success) {
  42.                         me[run].call(me, success, me.get('run'));
  43.                     });
  44.                 }
  45.             },
  46.             /**
  47.             * Command function for all mode.
  48.             * @method _runAll
  49.             * @param {Function} success
  50.             * @param {[AsyncCommand]} run
  51.             * @protected
  52.             */
  53.             _runAll: function (success, run) {
  54.                 var commandCount = run.length,
  55.                     completeCount = 0,
  56.                     value = [];

  57.                 _each(run, function (asyncCommand, index) {
  58.                     asyncCommand.run().after(_string_complete, function (eventFacade) {
  59.                         if (eventFacade.failed) {
  60.                             success.fail(eventFacade.error);
  61.                             return;
  62.                         }

  63.                         completeCount += 1;
  64.                         value[index] = eventFacade.value;

  65.                         if (completeCount === commandCount) {
  66.                             success(value);
  67.                         }
  68.                     });
  69.                 });

  70.                 if (!commandCount) {
  71.                     success(value);
  72.                 }
  73.             },
  74.             /**
  75.             * Command function for queue mode.
  76.             * @method _runAll
  77.             * @param {Function} success
  78.             * @param {[AsyncCommand]} run
  79.             * @param {Number} index
  80.             * @param {Array} value
  81.             * @protected
  82.             */
  83.             _runQueue: function (success, run, index, value) {
  84.                 index = index || 0;
  85.                 value = value || [];

  86.                 if (index >= run.length) {
  87.                     success(value);
  88.                     return;
  89.                 }

  90.                 run[index].run().after(_string_complete, function (eventFacade) {
  91.                     if (eventFacade.failed) {
  92.                         success.fail(eventFacade.error);
  93.                         return;
  94.                     }

  95.                     value[index] = eventFacade.value;

  96.                     this._runQueue(success, run, index + 1, value);
  97.                 }, this);
  98.             }
  99.         }, {
  100.             ATTRS: {
  101.                 /**
  102.                 * The inherited args attribute is protected.
  103.                 * @attribute args
  104.                 * @default []
  105.                 * @initonly
  106.                 * @protected
  107.                 * @type Array
  108.                 */
  109.                /**
  110.                 * A config object passed to the AsyncCommand constructor when
  111.                 * instantiating dynamically.
  112.                 * @attribute config
  113.                 * @default {}
  114.                 * @initonly
  115.                 * @type Object
  116.                 */
  117.                 config: {
  118.                     value: {},
  119.                     writeOnce: _string_initOnly
  120.                 },
  121.                 /**
  122.                 * The inherited ctx attribute is protected.
  123.                 * @attribute ctx
  124.                 * @initonly
  125.                 * @protected
  126.                 */
  127.                 /**
  128.                 * The inherited fn attribute is protected.
  129.                 * @attribute fn
  130.                 * @initonly
  131.                 * @protected
  132.                 * @type Function
  133.                 */
  134.                 /**
  135.                 * Value indicating the run mode.  Possible modes are:
  136.                 * <dl>
  137.                 *     <dt>
  138.                 *         all
  139.                 *     </dt>
  140.                 *     <dd>
  141.                 *         This mode runs all commands.  The commands might be
  142.                 *         completed out of order.  The run completes once all
  143.                 *         commands have completed.  The run fails if any command
  144.                 *         fails.
  145.                 *     </dd>
  146.                 *     <dt>
  147.                 *         queue
  148.                 *     </dt>
  149.                 *     <dd>
  150.                 *         This mode runs one command at a time.  It waits for
  151.                 *         the first command to complete before moving on to the
  152.                 *         next one.  The run completes when the last command has
  153.                 *         completed.  The run fails if a command fails and the
  154.                 *         remaining commands are not run.
  155.                 *     </dd>
  156.                 * </dl>
  157.                 * @attribute mode
  158.                 * @default 'queue'
  159.                 * @initonly
  160.                 * @type String
  161.                 */
  162.                 mode: {
  163.                     value: 'queue',
  164.                     writeOnce: _string_initOnly
  165.                 },
  166.                 /**
  167.                 * An array of AsyncCommands to run.  Command functions,
  168.                 * AsyncCommand config objects, and named command strings will
  169.                 * get converted to instances of AsyncCommand.
  170.                 * @attribute run
  171.                 * @default []
  172.                 * @initonly
  173.                 * @type [AsyncCommand]
  174.                 */
  175.                 run: {
  176.                     setter: function (run) {
  177.                         var config = this.get('config');
  178.                        
  179.                         return _map(_isArray(run) ? run : [
  180.                             run
  181.                         ], function (item) {
  182.                             if (_instanceOf(item, _AsyncCommand)) {
  183.                                 return item;
  184.                             }
  185.                            
  186.                             if (_isString(item)) {
  187.                                 item = _class.commands[item] || {};
  188.                             }
  189.                            
  190.                             if (_isFunction(item)) {
  191.                                 return new _AsyncCommand(_merge(config, {
  192.                                     fn: item
  193.                                 }));
  194.                             }
  195.                            
  196.                             return new _AsyncCommand(_merge(config, item));
  197.                         });
  198.                     },
  199.                     value: [],
  200.                     writeOnce: _string_initOnly
  201.                 }
  202.             },
  203.             /**
  204.              * This is a static object that stores named command definitions for
  205.              * repeat use.  This object's keys are the names of commands.  The
  206.              * values can either command functions or AsyncCommand config
  207.              * objects.
  208.              * @property commands
  209.              * @static
  210.              * @type Object
  211.              */
  212.             commands: {},
  213.             /**
  214.             * Creates and runs an instance of Async in 'all' mode.  This method
  215.             * accepts an unlimited number of arguments.  Arguments can be
  216.             * command functions, AsyncCommand config objects, instances of
  217.             * AsyncCommand, instances of Async, or arrays containing any of the
  218.             * above.
  219.             * @method runAll
  220.             * @return {Async}
  221.             * @static
  222.             */
  223.             runAll: function () {
  224.                 return _createAndRun({}, 'all', _unnest(arguments));
  225.             },
  226.             /**
  227.             * Creates and runs an instance of Async in 'all' mode.  This method
  228.             * accepts an unlimited number of arguments.  The first argument is a
  229.             * config object passed to the AsyncCommand constructor when
  230.             * instantiating dynamically.  The rest of the arguments can be
  231.             * command functions, AsyncCommand config objects, instances of
  232.             * AsyncCommand, instances of Async, or arrays containing any of the
  233.             * above.
  234.             * @method runAllWithConfig
  235.             * @return {Async}
  236.             * @static
  237.             */
  238.             runAllWithConfig: function () {
  239.                 var args = _Array(arguments);
  240.                
  241.                 return _createAndRun(args.shift(), 'all', _unnest(args));
  242.             },
  243.             /**
  244.             * Creates and runs an instance of Async in 'queue' mode.  This
  245.             * method accepts an unlimited number of parameters.  Parameters can
  246.             * be command functions, AsyncCommand config objects, instances of
  247.             * AsyncCommand, instances of Async, or arrays containing any of the
  248.             * above.
  249.             * @method runQueue
  250.             * @return {Async}
  251.             * @static
  252.             */
  253.             runQueue: function () {
  254.                 return _createAndRun({}, 'queue', _unnest(arguments));
  255.             },
  256.             /**
  257.             * Creates and runs an instance of Async in 'queue' mode.  This
  258.             * method accepts an unlimited number of parameters.  The first
  259.             * argument is a config object passed to the AsyncCommand constructor
  260.             * when instantiating dynamically.  The rest of the arguments can
  261.             * be command functions, AsyncCommand config objects, instances of
  262.             * AsyncCommand, instances of Async, or arrays containing any of the
  263.             * above.
  264.             * @method runQueue
  265.             * @return {Async}
  266.             * @static
  267.             */
  268.             runQueueWithConfig: function () {
  269.                 var args = _Array(arguments);
  270.                
  271.                 return _createAndRun(args.shift(), 'queue', _unnest(args));
  272.             }
  273.         });
  274.    
  275.     _createAndRun = function (config, mode, run) {
  276.         return new _class({
  277.             config: config,
  278.             mode: mode,
  279.             run: run
  280.         }).run();
  281.     };
  282.    
  283.     Y.Async = _class;
  284. }(Y, arguments[1]));
  285.