Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add universal event listeners #54

Closed
wants to merge 1 commit into from
Closed

Add universal event listeners #54

wants to merge 1 commit into from

Conversation

1cedsoda
Copy link

@1cedsoda 1cedsoda commented May 24, 2018

Applies changes from pull request #53 on branch node-10 (24th Mai 2018).

Remained added features from #53

  • Listeners listening to the event '*' getting triggered by every emitted event.
  • The handler function of a listener listening to '*' gets the actually emitted event as first argument.
  • .emit only returns true if at least one non-universal listener was triggered.

Changes

  • .emit('*' triggers each listeners listening to '*' only once. (with the actually emitted event as first argument)

Examples

emitter.on('foo', function() {
  console.log("foo");
  console.log(arguments);
})

emitter.on('*', function() {
  console.log("*");
  console.log(arguments);
})

console.log(emitter.emit('foo', 'bar1', 'bar2', 'bar3'));

//Output:
//foo
//[Arguments] { '0': 'bar1', '1': 'bar2', '2': 'bar3' }
//*
//[Arguments] { '0': 'foo', '1': 'bar1', '2': 'bar2', '3': 'bar3' }
//true
emitter.on('*', function() {
  console.log("*");
  console.log(arguments);
})

console.log(emitter.emit('foo', 'bar1', 'bar2', 'bar3'));

//Output:
//*
//[Arguments] { '0': 'foo', '1': 'bar1', '2': 'bar2', '3': 'bar3' }
//false
emitter.on('foo', function() {
  console.log("foo");
  console.log(arguments);
})

emitter.on('*', function() {
  console.log("*");
  console.log(arguments);
})

console.log(emitter.emit('foobar', 'bar1', 'bar2', 'bar3'));

//Output:
//*
//[Arguments] { '0': 'foobar', '1': 'bar1', '2': 'bar2', '3': 'bar3' }
//false
emitter.on('*', function() {
  console.log("*");
  console.log(arguments);
})

console.log(emitter.emit('*', 'bar1', 'bar2', 'bar3'));

//Output:
//*
//[Arguments] { '0': '*', '1': 'bar1', '2': 'bar2', '3': 'bar3' }
//true

@goto-bus-stop
Copy link
Member

Thanks for the PR! I'm afraid though that this module is intended to match the Node events API, and it's used by bundlers like Browserify and Webpack to provide require('events'). Adding features on top of the Node API is not a goal and causes a mismatch between the Node and browser versions.

@1cedsoda
Copy link
Author

1cedsoda commented May 24, 2018

That's totally understandable. But I can't imagine that this code would limit compatibility. Does it mean it's not possible to merge or it just needs to be tested?

@goto-bus-stop
Copy link
Member

goto-bus-stop commented May 24, 2018

I'm afraid it's not possible. Introducing differences between Node and the browser can cause issues when people (unwittingly) rely on those differences; eg., if people start to rely on '*' being a catchall with this module, their code will not work in Node. There might already be code out there using '*' as a plain event name; it would be weird, but there are millions of apps out there that do all kinds of unexpected things! If the Node core functionality is not enough for someone, there are hundreds of other events modules on npm that can implement whatever features they want :)

The point of this module is just to be a 1:1 polyfill for the Node events module. Sorry if that wasn't clear. :( I'll add a note to the readme in a bit to hopefully prevent disappointment in the future.

@1cedsoda
Copy link
Author

1cedsoda commented May 24, 2018

Thanks for your review anyway. I am not disappointed, after all this is a core module.

@1cedsoda 1cedsoda closed this May 24, 2018
@1cedsoda
Copy link
Author

If it is okay, I made a package to use this feature anyway: events-universal-patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants