0

So this should be relatively simple. In fact it should be super trivial. But I am blanking on how to handle this, consider the following method on a class:

  trigger(name, ...args) {
    let returnValue = null;

    if (!this.events.length > 0) {
      throw Error('There are no registered events.');
    }

    this.events.forEach((event) => {
      console.log(event.name, name)
      if (event.name === name) {
        if (args !== undefined) {
          returnValue = event.cb(...args, this);
        } else {
          returnValue = event.cb(this);
        }
      } else {
        throw Error('could not find ' + name + ' in the list of registered events.');
      }
    });

    return returnValue;
  }

Straight forward and simple. Heres the error:

console.log src/events/handler.js:51 event.with.params event.with.params

console.log src/events/handler.js:51 event.with.no.params event.with.params

It errors out if you have multiple events registered. I thought the easiest fix wad to do a return; once the name matches, as you can see it does match once. Heres the test:

  test('success in triggering multiple events', () => {
    eventHandler.register('event.with.params', (a, b) => {
      return a + ' ' + b;
    });

    eventHandler.register('event.with.no.params', () => {
      return 'no params'
    });

    expect(eventHandler.trigger('event.with.params', 'a', 'b')).toEqual('a b');
    expect(eventHandler.trigger('event.with.no.params')).toEqual('no params');
  });

Here you can see we register two events, then we attempt to trigger both of them. The error in the test is:

could not find event.with.params in the list of registered events.
    at Error (native)

Accept by console.log it did find it ... Then it continued on.

I know this is trivial to fix but I cant seem to figure it out. help ??

6
  • 1
    ps: if you use ...args, it will never be undefined unless your function was called as trigger(someName, undefined) - if trigger(someName) is called, args will be []. On a similar note,callback(...[], this) is the same as callback(this). Commented Apr 18, 2017 at 19:21
  • How do you handle the returnValue if multiple handler are registered for this event? At the moment you'll overwrite returnValue and return the value from the last callback called. Commented Apr 18, 2017 at 19:28
  • looks like your handler code will throw an error -if the first thing in events does not match the name- if there are any events registered with a different name, thanks @Thomas Commented Apr 18, 2017 at 19:29
  • @naomik, args will never be falsy. Even if called as trigger(someName, undefined) args will be [undefined] Commented Apr 18, 2017 at 19:29
  • @thedude, no it will throw if there is any event registered for a different name. Commented Apr 18, 2017 at 19:31

1 Answer 1

1

.forEach literally does it for each. So if you have ANY event with different name it will throw on it.

You could first filter events.

const filtered = this.events.filter(event => event.name === name);

Then check if there is any

if(!filtered.length) throw new Error(...)

Then run

filtered.forEach(event => /* run handlers */)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.