Skip to content

Troubleshooting

Eric M. Dantas edited this page Aug 19, 2016 · 1 revision

My listener isn't being called, what could be wrong?

There are a few things that could be happening, here are some of the most common:

  • You're calling bus.emit('ev') before called bus.on('ev', () => {}), so there's nobody listening what you just sent:
  let bus = new Bus();

  bus.emit('yo', {hey: '!'});

  bus.on('yo', (info) => {
    console.log(info); // not going to be called, the info has already been sent
  });
  • You're using different instances of the Bus. Say you do the following:
  let bus1 = new Bus();
  let bus2 = new Bus();

  bus1.on('yo', (info) => {
    console.log(info);
  });

  bus2.emit('yo', {hey: '!'});

The above example will never trigger the listener, because bus1 and bus2 are different instances of Bus, to fix that, use the same instance:

  let bus1 = new Bus();

  bus1.on('yo', (info) => {
    console.log(info); // {hey: '!'};
  });

  bus1.emit('yo', {hey: '!'});
  • You have different tokens for the publisher and listeners - they have to be equal as in tokenListener === tokenPublisher:
  let bus = new Bus();

  // notice the space in the end, it should be 'ev', not 'ev '
  bus.on('ev ', (info) => {
    console.log(info);
  });

  bus.emit('ev', '!');
Clone this wiki locally