Coder Social home page Coder Social logo

Comments (4)

Meiguro avatar Meiguro commented on August 25, 2024

pebble.js internally uses its own Emitter or event dispatcher implementation which calls all listeners or callbacks with three parameters, event, eventType, and listenerIndex where listenerIndex is the listener's position among all listeners currently registered to the same object's eventType.

This is not documented because this is a feature specific to the underlying Emitter implementation that none of the classes that depend on it rely on. It may change before a stable release.

Thinking about it again, I may actually remove those other two parameters. eventType might be removed because some events also have a subtype, and yet it's not being passed as a parameter. listenerIndex is for cases where listener order is important for dynamic listeners, although receiving that information after registration isn't quite useful except possibly for debugging to figure out if something else is registered before the current handler executing. There should have probably been a fourth parameter totalListeners.

The original reason for eventType was in case the event object is missing the type field. This is not possible now since Emitter sets the type field automatically, unless a frozen object is emitted. As you've noticed, eventType continues to be passed for dynamic listeners despite type being automatically set.

Thanks for having me look into this again. If the eventType is actually useful for you or anyone else, I can instead consider changing listenerIndex into eventSubtype instead of removing both parameters. And of course, the final Emitter behavior would then be documented.

from pebblejs.

Rocketeer007 avatar Rocketeer007 commented on August 25, 2024

For the app I'm currently working on, the eventType is not important. I can't comment on whether or not it might be useful in the future though!

This issue probably would never impact anyone unless they have a similar coding style to me...
I have an event handler function that also takes an optional parameter that is only expected when the function is called directly. Reading the documentation, I expected parameter 2 to be undefined - but actually, it was populated, so my "optional" parameter had to be number 4!

I'm not normally a JavaScript developer (more Java & C actually), so I'm not used to having such flexible callback function definitions :-)

from pebblejs.

Meiguro avatar Meiguro commented on August 25, 2024

Ah, I see, I'm leaning towards removing them then as that sounds like a fairly common use case that I overlooked by only using anonymous functions most of the time.

In the mean time you could use an additional anonymous function Menu.on('select', function(e) { myFunction(e); }). It seems wasteful, but with this arrangement, you could, for example, move logic that only performs if it was called by the event (if any) into this new function. Or at the very least, guard against this API change.

I have to admit this could be a breaking change, so it might be awhile for it to land.

from pebblejs.

Rocketeer007 avatar Rocketeer007 commented on August 25, 2024

With more JS experience under my belt, clearly the anonymous callback is the correct solution!

from pebblejs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.