78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith/**
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * Stores the subscriber information to be used when the event fires.
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @param {Function} fn The wrapped function to execute.
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @param {Object} context The value of the keyword 'this' in the listener.
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @param {Array} args* 0..n additional arguments to supply the listener.
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith *
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @class Subscriber
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @constructor
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith */
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke SmithY.Subscriber = function(fn, context, args) {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith /**
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * The callback that will be execute when the event fires
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * This is wrapped by Y.rbind if obj was supplied.
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @property fn
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @type Function
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith */
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith this.fn = fn;
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith /**
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * Optional 'this' keyword for the listener
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @property context
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @type Object
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith */
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith this.context = context;
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith /**
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * Unique subscriber id
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @property id
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @type String
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith */
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith this.id = Y.stamp(this);
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith /**
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * Additional arguments to propagate to the subscriber
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @property args
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @type Array
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith */
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith this.args = args;
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith /**
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * Custom events for a given fire transaction.
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @property events
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @type {EventTarget}
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith */
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith // this.events = null;
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith /**
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * This listener only reacts to the event once
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @property once
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith */
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith // this.once = false;
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith};
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke SmithY.Subscriber.prototype = {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith constructor: Y.Subscriber,
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith _notify: function(c, args, ce) {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith if (this.deleted && !this.postponed) {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith if (this.postponed) {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith delete this.fn;
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith delete this.context;
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith } else {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith delete this.postponed;
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith return null;
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith }
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith }
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith var a = this.args, ret;
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith switch (ce.signature) {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith case 0:
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith ret = this.fn.call(c, ce.type, args, c);
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith break;
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith case 1:
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith ret = this.fn.call(c, args[0] || null, c);
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith break;
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith default:
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith if (a || args) {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith args = args || [];
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith a = (a) ? args.concat(a) : args;
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith ret = this.fn.apply(c, a);
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith } else {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith ret = this.fn.call(c);
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith }
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith }
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith if (this.once) {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith ce._delete(this);
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith }
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith return ret;
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith },
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith /**
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * Executes the subscriber.
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @method notify
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @param args {Array} Arguments array for the subscriber.
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @param ce {CustomEvent} The custom event that sent the notification.
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith */
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith notify: function(args, ce) {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith var c = this.context,
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith ret = true;
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith if (!c) {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith c = (ce.contextFn) ? ce.contextFn() : ce.context;
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith }
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith // only catch errors if we will not re-throw them.
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith if (Y.config.throwFail) {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith ret = this._notify(c, args, ce);
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith } else {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith try {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith ret = this._notify(c, args, ce);
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith } catch (e) {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith Y.error(this + ' failed: ' + e.message, e);
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith }
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith }
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith return ret;
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith },
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith /**
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * Returns true if the fn and obj match this objects properties.
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * Used by the unsubscribe method to match the right subscriber.
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith *
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @method contains
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @param {Function} fn the function to execute.
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @param {Object} context optional 'this' keyword for the listener.
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * @return {boolean} true if the supplied arguments match this
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith * subscriber's signature.
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith */
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith contains: function(fn, context) {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith if (context) {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith return ((this.fn == fn) && this.context == context);
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith } else {
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith return (this.fn == fn);
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith }
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith }
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith
78d3c37c02e77731a25f370f9fac264ef51f01a9Luke Smith};