Skip to content

Conversation

@scottinet
Copy link
Contributor

@scottinet scottinet commented Dec 7, 2017

Description

  • Remove the possibility of having duplicate listeners added on a same event. Duplicates are now silently discarded instead of being added. To counterbalance this, event emitter methods throw only on rare occasions
  • add the addOnceListener method, as it's more in line with other method names than once, which is now an alias (see below about aliases)

Other related changes

  • since we have rewritten all methods from NodeJS' event emitter class, there is no point in extending it, so I've removed window manipulation and the need to require events when on NodeJS side
  • the KuzzleEventListener object is now an ES6 class (added to babel configuration for conversion to ES5)
  • simplify the way once listeners are handled
  • make sure functions returned values are consistent
  • asking for a list of listeners on an unknown event won't create an empty array (potential memleak, albeit small)
  • the socketio network layer now correctly expose the whole event emitter API, with a bridge between our KuzzleEventEmitter class and socketio's dedicated event emitter
  • aliases: I think we do not want too many aliases, as this leads to many questions about the difference between a method and its corresponding alias, so I removed most of them:
    • off + removeListener => removeListener
    • emitEvent + emit => emit
    • on and once are now aliases specific to our Javascript SDK implementation, as those two are very commonplace when it comes to event emitters in JS. This is (and should be) the only case where an alias exists

Other changes

  • convert Kuzzle object into an ES6 class

@scottinet scottinet self-assigned this Dec 7, 2017
@codecov-io
Copy link

codecov-io commented Dec 7, 2017

Codecov Report

Merging #265 into 6.x will increase coverage by 0.09%.
The diff coverage is 96.93%.

Impacted file tree graph

@@            Coverage Diff             @@
##              6.x     #265      +/-   ##
==========================================
+ Coverage   98.27%   98.36%   +0.09%     
==========================================
  Files          17       17              
  Lines        2140     2085      -55     
  Branches      623      606      -17     
==========================================
- Hits         2103     2051      -52     
+ Misses         37       34       -3
Impacted Files Coverage Δ
src/networkWrapper/protocols/abstract/realtime.js 98.63% <100%> (ø) ⬆️
src/networkWrapper/protocols/websocket.js 100% <100%> (ø) ⬆️
src/networkWrapper/protocols/socketio.js 94.82% <91.66%> (-5.18%) ⬇️
src/eventEmitter/index.js 97.14% <96.22%> (+4.66%) ⬆️
src/Kuzzle.js 97.44% <97.35%> (-0.09%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7f56496...bb2395e. Read the comment docs.


prependListener(event, callback, once = false) {
this._addEventWrapper(event, callback, once);
return this.prependListener(event, callback, once);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

infinite loop ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced super by this as a last-minute change, I don't even know why 😞

* @param {string} [event]
*/
removeAllListeners(event) {
if (event !== undefined && this.eventsWrapper[event] !== undefined) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if event is defined but this.eventsWrapper[event] is not, we will execute the else code, and so remove all listeners of all events ??

*/
once(roomId, callback) {
this.socket.once(roomId, callback);
addOnceListener(event, callback) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it necessary to redefine these methods ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is not. I removed it along with a few others (on, prependOnceListener, ...)

@scottinet
Copy link
Contributor Author

@ballinette > good catches 👍

@scottinet scottinet merged commit 60814dd into 6.x Dec 18, 2017
@scottinet scottinet deleted the custom_EventEmitter branch December 18, 2017 13:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants