How to use the events.EventEmitter.listenerCount function in events

To help you get started, we’ve selected a few events examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stimulant / ampm / node_modules / request / node_modules / bl / node_modules / readable-stream / lib / _stream_readable.js View on Github external
src.emit('data', chunk);

    // if anyone needs a drain, then we have to wait for that.
    if (state.awaitDrain > 0)
      return;
  }

  // if every destination was unpiped, either before entering this
  // function, or in the while loop, then stop flowing.
  //
  // NB: This is a pretty rare edge case.
  if (state.pipesCount === 0) {
    state.flowing = false;

    // if there were data event listeners added, then switch to old mode.
    if (EE.listenerCount(src, 'data') > 0)
      emitDataEvents(src);
    return;
  }

  // at this point, no one needed a drain, so we just ran out of data
  // on the next readable event, start it over again.
  state.ranOut = true;
}
github LinkedDataFragments / Client.js / lib / iterators / Iterator.js View on Github external
function readAll() {
        // Try to read an item while there are still data listeners
        var item;
        while (EventEmitter.listenerCount(self, 'data') !== 0 &&
               (item = self.read()) !== null)
          self.emit('data', item);
        // Detach the listener if nobody is listening anymore
        if (EventEmitter.listenerCount(self, 'data') === 0) {
          self.removeListener('readable', readAll);
          self.removeListener('newListener', waitForDataListener); // A listener could already be attached
          self.addListener('newListener', waitForDataListener);
        }
      }
      this.removeListener('readable', readAll); // A listener could already be attached
github node-app / Nodelike / lib / http.js View on Github external
req._hadError = true;
  } else if (parser.incoming && parser.incoming.upgrade) {
    // Upgrade or CONNECT
    var bytesParsed = ret;
    var res = parser.incoming;
    req.res = res;

    socket.ondata = null;
    socket.onend = null;
    parser.finish();

    // This is start + byteParsed
    var bodyHead = d.slice(start + bytesParsed, end);

    var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
    if (EventEmitter.listenerCount(req, eventName) > 0) {
      req.upgradeOrConnect = true;

      // detach the socket
      socket.emit('agentRemove');
      socket.removeListener('close', socketCloseListener);
      socket.removeListener('error', socketErrorListener);

      req.emit(eventName, res, socket, bodyHead);
      req.emit('close');
    } else {
      // Got Upgrade header or CONNECT method, but have no handler.
      socket.destroy();
    }
    freeParser(parser, req);
  } else if (parser.incoming && parser.incoming.complete &&
             // When the status code is 100 (Continue), the server will
github nodyn / nodyn / src / main / javascript / readline.js View on Github external
if (r) emitKeys(stream, r);
    } else {
      // Nobody's watching anyway
      stream.removeListener('data', onData);
      stream.on('newListener', onNewListener);
    }
  }

  function onNewListener(event) {
    if (event == 'keypress') {
      stream.on('data', onData);
      stream.removeListener('newListener', onNewListener);
    }
  }

  if (EventEmitter.listenerCount(stream, 'keypress') > 0) {
    stream.on('data', onData);
  } else {
    stream.on('newListener', onNewListener);
  }
}
exports.emitKeypressEvents = emitKeypressEvents;
github node-app / Nodelike / lib / stream.js View on Github external
function onerror(er) {
    cleanup();
    if (EE.listenerCount(this, 'error') === 0) {
      throw er; // Unhandled stream error in pipe.
    }
  }
github apigee / trireme / node12 / node12src / src / main / javascript / io / apigee / trireme / node12 / node / readline.js View on Github external
if (r) emitKeys(stream, r);
    } else {
      // Nobody's watching anyway
      stream.removeListener('data', onData);
      stream.on('newListener', onNewListener);
    }
  }

  function onNewListener(event) {
    if (event == 'keypress') {
      stream.on('data', onData);
      stream.removeListener('newListener', onNewListener);
    }
  }

  if (EventEmitter.listenerCount(stream, 'keypress') > 0) {
    stream.on('data', onData);
  } else {
    stream.on('newListener', onNewListener);
  }
}
exports.emitKeypressEvents = emitKeypressEvents;
github azuqua / node-token-sockjs / test / integration / authentication.js View on Github external
httpClient.request(opts, null, function(error, resp){
				var token = JSON.parse(resp.body).token;
				assert.ok(token, "Token exists");

				var socket = new TokenSocket();
				tokenServer.socketServer._connection(socket);

				assert.property(socket, "sid", "Socket as UUID");
				assert.property(socket, "created", "Server tracks socket created timestamp");
				assert.isObject(socket.channels, "Socket tracks channels");
				assert.notOk(socket.auth, "Socket does not start in auth state");
				assert.lengthOf(Object.keys(tokenServer._sockets), 1, "Server has one connected socket");
				assert.equal(EventEmitter.listenerCount(socket._emitter, "data"), 1, "Socket on data has one listener");
				assert.equal(EventEmitter.listenerCount(socket._emitter, "close"), 1, "Socket on close has one listener");

				socket._emit("close");
				assert.lengthOf(Object.keys(tokenServer._sockets), 0, "Server ends with zero connected sockets");
				done();
			});
		});
github elastic / elasticsearch-js / src / lib / Log.js View on Github external
Log.prototype.trace = function (method, requestUrl, body, responseBody, responseStatus) {
  if (EventEmitter.listenerCount(this, 'trace')) {
    if (typeof requestUrl === 'object') {
      requestUrl = _.formatUrl(requestUrl);
    }
    return this.emit('trace', method, requestUrl, body, responseBody, responseStatus);
  }
};
github joyent / smartos-live / src / vm / sbin / vmadmd.js View on Github external
function _cleanupWaiter() {
        clearTimeout(timeout);
        stateReporter.removeListener(state, _gotState);
        log.trace({
            count: EventEmitter.listenerCount(stateReporter, state),
            state: state,
            vmUuid: vmUuid
        }, 'stateReporter listeners after cleanup');
    }
github arangodb / arangodb / js / node / node_modules / htmlparser2 / node_modules / readable-stream / lib / _stream_readable.js View on Github external
return function() {
    var state = src._readableState;
    debug('pipeOnDrain', state.awaitDrain);
    if (state.awaitDrain)
      state.awaitDrain--;
    if (state.awaitDrain === 0 && EE.listenerCount(src, 'data')) {
      state.flowing = true;
      flow(src);
    }
  };
}