/*
Add listener for an api's errors
eventEmitter: events.EventEmitter
eventEmitter.id api name

Can be invokd multiple times for any value
489252
function addErrorListener(eventEmitter) {
	if (eventEmitter instanceof events.EventEmitter &&
		!~eventEmitter.listeners('error').indexOf(apiError))
		eventEmitter.on('error', apiError)
	return eventEmitter
}
636767
@Override
	public void addEventHandler(EventHandler eventHandler) {
		Preconditions.checkNotNull(eventHandler.getName(), "Missing Name");
		if(getEventHandler(eventHandler.getName()) != null) {
			throw new ApplicationException(Code.CONFLICT, "EventHandler with name " + eventHandler.getName() + " already exists!");
		}
		index(eventHandler);
		dynoClient.hset(nsKey(EVENT_HANDLERS), eventHandler.getName(), toJson(eventHandler));
		recordRedisDaoRequests("addEventHandler");
	}
809195
public void addTangoPipeListener(ITangoPipeListener listener, boolean stateless)
                throws DevFailed
    {
		event_listeners.add(ITangoPipeListener.class, listener);
        event_identifier = subscribe_pipe_event(attr_name, filters, stateless);
    }
265702
function eventEmitter(guid) {
        var self = this;
        self.listeners = [];
        self.addListener = function (options) {
            var id = guid();
            self.listeners.push({
                id: id,
                actionType: options.actionType,
                callback: options.callback
            });
            return id;
        };
        self.removeListener = function (options) {
            for (var i = 0; i < self.listeners.length; i++) {
                if (self.listeners[i].id === options.id) {
                    self.listeners.slice(i, 1);
                }
            }
        };
        self.emit = function (options) {
            for (var i = 0; i < self.listeners.length; i++) {
                if (self.listeners[i].actionType === options.actionType) {
                    self.listeners[i].callback(options.options);
                }
            }
        };
        return self;
    }
1233678
function addNodeOrPathListenerApi( eventId, jsonPathOrListenerMap, callback ){

    if( isString(jsonPathOrListenerMap) ) {
      addSingleNodeOrPathListener(eventId, jsonPathOrListenerMap, callback);

    } else {
      addMultipleNodeOrPathListeners(eventId, jsonPathOrListenerMap);
    }

    return oboeApi; // chaining
  }
599337
function listener (event, done) {
  return function onevent (arg1) {
    var args = new Array(arguments.length)
    var ee = this
    var err = event === 'error'
      ? arg1
      : null

    // copy args to prevent arguments escaping scope
    for (var i = 0; i < args.length; i++) {
      args[i] = arguments[i]
    }

    done(err, ee, event, args)
  }
}
727370
function (eventName,handler) {
			var event;
			if (event = this.getEvent(eventName))
			{
				if (!event.once(handler,true))
					this.e.log('Invalid handler sent to event `'+eventName+'` on `'+this.getConfig('id')+'`','warning');
			} else
				this.e.log('Not existent event `'+eventName+'` on `'+this.getConfig('id')+'`','warning');
			return this;
		}
1358332
function EventEmitter(name, node) {
        if ('string' !== typeof name) {
            throw new TypeError('EventEmitter constructor: name must be ' +
                                'string. Found: ' + name);
        }

        this.node = node;

        // ## Public properties

        this.name = name;

        /**
         * ### EventEmitter.listeners
         *
         * Event listeners collection
         */
        this.events = {};

        /**
         * ## EventEmitter.recordChanges
         *
         * If TRUE, keeps tracks of addition and deletion of listeners
         *
         * @see EventEmitter.changes
         */
        this.recordChanges = false;

        /**
         * ## EventEmitter.changes
         *
         * If TRUE, keeps tracks of addition and deletion of listeners
         *
         * @see EventEmitter.recordChanges
         */
        this.changes = {
            added: [],
            removed: []
        };

        /**
         * ## EventEmitter.labels
         *
         * List of labels associated to an event listener
         *
         * @see EventEmitter.on
         */
        this.labels = {};

        /**
         * ### EventEmitter.history
         *
         * Database of emitted events
         *
         * @experimental
         *
         * @see NDDB
         * @see EventEmitter.EventHistory
         * @see EventEmitter.store
         */
        this.history = new EventHistory(this.node);
    }
1089633
function addListener(element, listener) {
        var id = idHandler.get(element);

        if(!eventListeners[id]) {
            eventListeners[id] = [];
        }

        eventListeners[id].push(listener);
    }
707435
public void addTangoUserListener(ITangoUserListener listener, boolean stateless)
                throws DevFailed
    {
		event_listeners.add(ITangoUserListener.class, listener);
        event_identifier = subscribe_user_event(attr_name, filters, stateless);
    }
955588
function modAddDefinedListener( id, listener, lowPriority ) {
        if ( modIsDefined( id ) ) {
            listener( id );
            return;
        }

        var listeners = modDefinedListener[ id ];
        if ( !listeners ) {
            listeners = modDefinedListener[ id ] = [];
        }

        lowPriority
            ? listeners.unshift( listener )
            : listeners.push( listener );
    }
1259578
function(clientId, options) {
  events.EventEmitter.call(this);

  this.versions = null;
  this.indexes = null;
  this.memory = {};

  options = options || {};
  if (typeof options === 'string') {
    options = { path: options };
  }
  this.params = {
    clientId: clientId,
    path: options.path ? String(options.path) : '',
    storage: options.storage || DEFAULT_STORAGE,
    writePerms: options.writePerms || DEFAULT_WRITE_PERMS,
    indexLimit: options.indexLimit || DEFAULT_INDEX_LIMIT,
  };

  if (this.params.storage !== 'memory') {
    throw new Error(this.params.storage + ' storage is not currently supported');
  }
}
781917
function on(emitter, eventName, handler, context) {
  handler = context ? handler.bind(context) : handler;
  emitter.addEventListener(eventName, handler);
  return handler;
}
89158
function emitter(event, payload)
{
  // get proper name
  var normalized = normalize(event);

  // notify listeners
  this.emit(normalized, payload);

  // notify listeners of the specific type
  if (payload['template_type'])
  {
    this.emit([normalized, payload['template_type']].join('.'), payload);
  }
  else if (payload.type)
  {
    this.emit([normalized, normalize(payload.type)].join('.'), payload);
  }
}
1317627
function (eventName,handler) {
			var event;
			if (event = this.getEvent(eventName))
			{
				if (!event.addListener(handler))
					this.e.log('Invalid handler sent to event `'+eventName+'` on `'+this.getConfig('id')+'`','warning');
			} else
				this.e.log('Not existent event `'+eventName+'` on `'+this.getConfig('id')+'`','warning');
			return this;
		}
93315
function addListener(name) {
  if (name !== 'collect') {
    return;
  }

  if (this._collecting) {
    // Don't add more than once
    return;
  }

  debug('adding collect method', this._readableState.objectMode, this._readableState.encoding);

  let collected;
  if (this._readableState.objectMode) {
    collected = [];
  } else if (this._readableState.encoding === null) {
    collected = Buffer.from('');
  } else {
    collected = '';
  }

  this
    .on('readable', () => {
      let chunk;
      while ((chunk = this.read()) !== null) {
        debug('data', chunk);

        if (chunk !== null) {
          if (this._readableState.objectMode) {
            collected.push(chunk);
          } else if (this._readableState.encoding === null) {
            collected = Buffer.concat([collected, chunk]);
          } else {
            collected += chunk;
          }
        }
      }
    })
    .on('end', () => this.emit('collect', collected));

  this._collecting = true;
}
1073026
private static void sendErrorToCallback(EventCallBackStruct cs, String callbackKey, DevFailed e) {

        int source = (cs.consumer instanceof NotifdEventConsumer)?
                EventData.NOTIFD_EVENT : EventData.ZMQ_EVENT;
        EventData eventData =
                new EventData(cs.device, callbackKey,
                        cs.event_name, source,
                        cs.event_type, null, null, null, null, null, e.errors);

        if (cs.use_ev_queue) {
            EventQueue ev_queue = cs.device.getEventQueue();
            ev_queue.insert_event(eventData);
        } else
            cs.callback.push_event(eventData);
    }