API Docs for: 3.8.0
Show:

File: event-custom/js/subscriber.js

  1. /**
  2.  * Stores the subscriber information to be used when the event fires.
  3.  * @param {Function} fn       The wrapped function to execute.
  4.  * @param {Object}   context  The value of the keyword 'this' in the listener.
  5.  * @param {Array} args*       0..n additional arguments to supply the listener.
  6.  *
  7.  * @class Subscriber
  8.  * @constructor
  9.  */
  10. Y.Subscriber = function(fn, context, args, when) {

  11.     /**
  12.      * The callback that will be execute when the event fires
  13.      * This is wrapped by Y.rbind if obj was supplied.
  14.      * @property fn
  15.      * @type Function
  16.      */
  17.     this.fn = fn;

  18.     /**
  19.      * Optional 'this' keyword for the listener
  20.      * @property context
  21.      * @type Object
  22.      */
  23.     this.context = context;

  24.     /**
  25.      * Unique subscriber id
  26.      * @property id
  27.      * @type String
  28.      */
  29.     this.id = Y.stamp(this);

  30.     /**
  31.      * Additional arguments to propagate to the subscriber
  32.      * @property args
  33.      * @type Array
  34.      */
  35.     this.args = args;

  36.     this._when = when;

  37.     /**
  38.      * Custom events for a given fire transaction.
  39.      * @property events
  40.      * @type {EventTarget}
  41.      */
  42.     // this.events = null;

  43.     /**
  44.      * This listener only reacts to the event once
  45.      * @property once
  46.      */
  47.     // this.once = false;

  48. };

  49. Y.Subscriber.prototype = {
  50.     constructor: Y.Subscriber,

  51.     _notify: function(c, args, ce) {
  52.         if (this.deleted && !this.postponed) {
  53.             if (this.postponed) {
  54.                 delete this.fn;
  55.                 delete this.context;
  56.             } else {
  57.                 delete this.postponed;
  58.                 return null;
  59.             }
  60.         }
  61.         var a = this.args, ret;
  62.         switch (ce.signature) {
  63.             case 0:
  64.                 ret = this.fn.call(c, ce.type, args, c);
  65.                 break;
  66.             case 1:
  67.                 ret = this.fn.call(c, args[0] || null, c);
  68.                 break;
  69.             default:
  70.                 if (a || args) {
  71.                     args = args || [];
  72.                     a = (a) ? args.concat(a) : args;
  73.                     ret = this.fn.apply(c, a);
  74.                 } else {
  75.                     ret = this.fn.call(c);
  76.                 }
  77.         }

  78.         if (this.once) {
  79.             ce._delete(this);
  80.         }

  81.         return ret;
  82.     },

  83.     /**
  84.      * Executes the subscriber.
  85.      * @method notify
  86.      * @param args {Array} Arguments array for the subscriber.
  87.      * @param ce {CustomEvent} The custom event that sent the notification.
  88.      */
  89.     notify: function(args, ce) {
  90.         var c = this.context,
  91.             ret = true;

  92.         if (!c) {
  93.             c = (ce.contextFn) ? ce.contextFn() : ce.context;
  94.         }

  95.         // only catch errors if we will not re-throw them.
  96.         if (Y.config && Y.config.throwFail) {
  97.             ret = this._notify(c, args, ce);
  98.         } else {
  99.             try {
  100.                 ret = this._notify(c, args, ce);
  101.             } catch (e) {
  102.                 Y.error(this + ' failed: ' + e.message, e);
  103.             }
  104.         }

  105.         return ret;
  106.     },

  107.     /**
  108.      * Returns true if the fn and obj match this objects properties.
  109.      * Used by the unsubscribe method to match the right subscriber.
  110.      *
  111.      * @method contains
  112.      * @param {Function} fn the function to execute.
  113.      * @param {Object} context optional 'this' keyword for the listener.
  114.      * @return {boolean} true if the supplied arguments match this
  115.      *                   subscriber's signature.
  116.      */
  117.     contains: function(fn, context) {
  118.         if (context) {
  119.             return ((this.fn == fn) && this.context == context);
  120.         } else {
  121.             return (this.fn == fn);
  122.         }
  123.     },
  124.    
  125.     valueOf : function() {
  126.         return this.id;
  127.     }

  128. };

  129.