How to use events - 10 common examples

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 graalvm / graaljs / test / parallel / test-global-console-exists.js View on Github external
process.on('exit', () => {
  assert.strictEqual(warningTimes, 1);
});

process.stderr.write = (data) => {
  if (writeTimes === 0)
    assert.ok(leakWarning.test(data));
  else
    assert.fail('stderr.write should be called only once');

  writeTimes++;
};

const oldDefault = EventEmitter.defaultMaxListeners;
EventEmitter.defaultMaxListeners = 1;

const e = new EventEmitter();
e.on('hello', () => {});
e.on('hello', () => {});

// TODO: Figure out how to validate console. Currently,
// there is no obvious way of validating that console
// exists here exactly when it should.
github nock / propagate / tests / index.js View on Github external
test('emit on source before destination', t => {
  t.plan(2)

  const source = new EventEmitter()
  const dest = new EventEmitter()

  // Set up test case for "propagate all"
  // `count` should have been incremented by handler on source when handler on dest is invoked
  let count = 0
  propagate(source, dest)
  source.on('event', () => {
    count++
  })
  dest.on('event', () => {
    t.equal(count, 1, 'emit on source first')
  })

  // Emit the events for assertion
  t.true(source.emit('event'))
})
github jbaudanza / rxremote / test / server_spec.js View on Github external
it('should optimize the connection handler', () => {
    // This will fail if tests are run without --allow-natives-syntax
    assert(v8.isNative())

    // This quacks close enough to a WebSocket
    const socket = new EventEmitter();
    socket.readyState = 1;
    socket.upgradeReq = {
      headers: {},
      connection: {
        remoteAddress: '127.0.0.1'
      }
    };

    const observables = {
      foo() { return Rx.Observable.of(1,2,3); }
    };
    const subject = new Rx.Subject();

    function doCall() {
      // We just want this to run, we don't care what it actually does in this
      // specific test.
github rochejul / sequelize-mocking / test / sequelize-mockingSpec.js View on Github external
describe('SequelizeMocking - ', function () {
    const expect = require('chai').expect;
    const sinon = require('sinon');

    const path = require('path');
    const EventEmitter = require('events').EventEmitter;
    const _ = require('lodash');

    const Sequelize = require('sequelize');
    const sequelizeFixtures = require('sequelize-fixtures');
    const SequelizeMocking = require('../lib/sequelize-mocking');

    const defaultMaxListeners = EventEmitter.defaultMaxListeners;

    it('shall exist', function () {
        expect(SequelizeMocking).to.exist;
        expect(_.isPlainObject(SequelizeMocking)).to.be.false;
    });

    let sinonSandbox;

    beforeEach(function () {
        sinonSandbox = sinon.sandbox.create();
        EventEmitter.defaultMaxListeners = 100; // Due to an error when we instanciate too many times fastly some dialects, like the MySql one
    });

    afterEach(function () {
        sinonSandbox.restore();
        EventEmitter.defaultMaxListeners = defaultMaxListeners;
github graalvm / graaljs / test / parallel / test-event-emitter-get-max-listeners.js View on Github external
const EventEmitter = require('events');

const emitter = new EventEmitter();

assert.strictEqual(emitter.getMaxListeners(), EventEmitter.defaultMaxListeners);

emitter.setMaxListeners(0);
assert.strictEqual(emitter.getMaxListeners(), 0);

emitter.setMaxListeners(3);
assert.strictEqual(emitter.getMaxListeners(), 3);

// https://github.com/nodejs/node/issues/523 - second call should not throw.
const recv = {};
EventEmitter.prototype.on.call(recv, 'event', () => {});
EventEmitter.prototype.on.call(recv, 'event', () => {});
github alexbosworth / ln-service / test / routerrpc-integration / test_subscribe_to_probe.js View on Github external
const {channels} = await getChannels({lnd: cluster.remote.lnd});

  const invoice = await createInvoice({tokens, lnd: cluster.remote.lnd});

  await delay(1000);

  const sub = subscribeToProbe({
    lnd,
    destination: cluster.remote_node_public_key,
    tokens: invoice.tokens,
  });

  sub.on('error', () => {});

  const [{route}] = await once(sub, 'probing');

  // On 0.7.1 confidence is not supported
  delete route.confidence;

  deepIs(route, {
    fee: 1,
    fee_mtokens: '1500',
    hops: [
      {
        channel: controlToTargetChan.id,
        channel_capacity: controlToTargetChan.capacity,
        fee: 1,
        fee_mtokens: '1500',
        forward: tokens,
        forward_mtokens: `${tokens}000`,
        public_key: cluster.target_node_public_key,
github aurelia / testing / build / paths.js View on Github external
var path = require('path');
var fs = require('fs');

// hide warning //
var emitter = require('events');
emitter.defaultMaxListeners = 20;

var appRoot = 'src/';
var pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));

var paths = {
  root: appRoot,
  source: appRoot + '**/*.js',
  html: appRoot + '**/*.html',
  style: 'styles/**/*.css',
  output: 'dist/',
  doc:'./doc',
  e2eSpecsSrc: 'test/e2e/src/*.js',
  e2eSpecsDist: 'test/e2e/dist/',
  packageName: pkg.name,
  useTypeScriptForDTS: false,
  importsToAdd: [],
github graalvm / graaljs / test / mjsunit / test-promise.js View on Github external
b.addErrback(function(value) {
  assert.equal(TEST_VALUE, value);
  expectedCallbacks.b2--;
});

// Test late errback binding
var c = new Promise();
c.emitError(TEST_VALUE);
assert.ok(c.addErrback(function(value) {
  assert.equal(TEST_VALUE, value);
  expectedCallbacks.c1--;
}));

// Test errback exceptions
var d = new Promise();
d.emitError(TEST_VALUE);

process.addListener('uncaughtException', function(e) {
  if (e.name === "AssertionError") {
    throw e;
  }

  expectedCallbacks.d1--;
  assert.ok(e.message.match(/unhandled emitError/i));
});

process.addListener('exit', function() {
  for (var name in expectedCallbacks) {
    var count = expectedCallbacks[name];

    assert.equal(
github graalvm / graaljs / test / mjsunit / test-promise.js View on Github external
});
a.addErrback(function(error) {
  assert.notEqual(TEST_VALUE, error, 'normal');
});
a.emitSuccess(TEST_VALUE);

assert.ok(a.addCallback(function(value) {
  assert.equal(TEST_VALUE, value);
  expectedCallbacks.a2--;
}));
assert.ok(a.addErrback(function(error) {
  assert.notEqual(TEST_VALUE, error, 'late');
}));

// Test regular & late errback binding
var b = new Promise();
b.addErrback(function(value) {
  assert.equal(TEST_VALUE, value);
  expectedCallbacks.b1--;
});
b.emitError(TEST_VALUE);

b.addErrback(function(value) {
  assert.equal(TEST_VALUE, value);
  expectedCallbacks.b2--;
});

// Test late errback binding
var c = new Promise();
c.emitError(TEST_VALUE);
assert.ok(c.addErrback(function(value) {
  assert.equal(TEST_VALUE, value);
github graalvm / graaljs / test / mjsunit / test-promise.js View on Github external
var
  Promise = require('events').Promise,

  TEST_VALUE = {some: 'object'},

  expectedCallbacks = {
    a1: 1,
    a2: 1,
    b1: 1,
    b2: 1,
    c1: 1,
    d1: 1,
  };

// Test regular & late callback binding
var a = new Promise();
a.addCallback(function(value) {
  assert.equal(TEST_VALUE, value);
  expectedCallbacks.a1--;
});
a.addErrback(function(error) {
  assert.notEqual(TEST_VALUE, error, 'normal');
});
a.emitSuccess(TEST_VALUE);

assert.ok(a.addCallback(function(value) {
  assert.equal(TEST_VALUE, value);
  expectedCallbacks.a2--;
}));
assert.ok(a.addErrback(function(error) {
  assert.notEqual(TEST_VALUE, error, 'late');
}));