1 // ==========================================================================
  2 // Project:   SC.Statechart - A Statechart Framework for SproutCore
  3 // Copyright: ©2010, 2011 Michael Cohen, and contributors.
  4 //            Portions @2011 Apple Inc. All rights reserved.
  5 // License:   Licensed under MIT license (see license.js)
  6 // ==========================================================================
  7 
  8 /*globals SC */
  9 
 10 /**
 11   @class
 12 
 13   Represents a call that is intended to be asynchronous. This is
 14   used during a state transition process when either entering or
 15   exiting a state.
 16 
 17   @extends SC.Object
 18   @author Michael Cohen
 19 */
 20 SC.Async = SC.Object.extend(
 21   /** @scope SC.Async.prototype */{
 22 
 23   func: null,
 24 
 25   arg1: null,
 26 
 27   arg2: null,
 28 
 29   /** @private
 30     Called by the statechart
 31   */
 32   tryToPerform: function(state) {
 33     var func = this.get('func'),
 34         arg1 = this.get('arg1'),
 35         arg2 = this.get('arg2'),
 36         funcType = SC.typeOf(func);
 37 
 38     if (funcType === SC.T_STRING) {
 39       state.tryToPerform(func, arg1, arg2);
 40     }
 41     else if (funcType === SC.T_FUNCTION) {
 42       func.apply(state, [arg1, arg2]);
 43     }
 44   }
 45 
 46 });
 47 
 48 /**
 49   Singleton
 50 */
 51 SC.Async.mixin(/** @scope SC.Async */{
 52 
 53   /**
 54     Call in either a state's enterState or exitState method when you
 55     want a state to perform an asynchronous action, such as an animation.
 56 
 57     Examples:
 58 
 59       SC.State.extend({
 60 
 61         enterState: function() {
 62           return SC.Async.perform('foo');
 63         },
 64 
 65         exitState: function() {
 66           return SC.Async.perform('bar', 100);
 67         }
 68 
 69         foo: function() { ... },
 70 
 71         bar: function(arg) { ... }
 72 
 73       });
 74 
 75     @param func {String|Function} the function to be invoked on a state
 76     @param arg1 Optional. An argument to pass to the given function
 77     @param arg2 Optional. An argument to pass to the given function
 78     @return {SC.Async} a new instance of a SC.Async
 79   */
 80   perform: function(func, arg1, arg2) {
 81     return SC.Async.create({ func: func, arg1: arg1, arg2: arg2 });
 82   }
 83 
 84 });
 85