Code Monkey home page Code Monkey logo

Comments (4)

thetutlage avatar thetutlage commented on May 20, 2024 1

Yup. Or we can have a special function that emits the meta events on a given emitter instance. Something like this.

async function emitMetaEvent(emitter, event, data) {
  metaEventsAllowed = true
  await emitter.emit(event, data)
  metaEventsAllowed = false
}

The only benefit of this function over the callback approach is, we will not end up creating too many anonymous functions.

if (!isMetaEvent(eventName)) {
  emitMetaEvent(this, listenerAdded, {eventName, listener})
}

from emittery.

sindresorhus avatar sindresorhus commented on May 20, 2024

// @lukehorvat

from emittery.

lukehorvat avatar lukehorvat commented on May 20, 2024

Ouch. Sorry for the oversight.

I have one idea. We could have a private withMetaEventsAllowed function which would allow Emittery to run some internal code in a "privileged" way (like running sudo or whatever). All of the internal calls to emit could be wrapped in this function.

let metaEventsAllowed = false;

function withMetaEventsAllowed(callback) {
  metaEventsAllowed = true;
  callback();
  metaEventsAllowed = false;
}

So, for example, this code:

emittery/index.js

Lines 261 to 263 in f61b87d

if (!isMetaEvent(eventName)) {
this.emit(listenerAdded, {eventName, listener}, metaEventsAllowed);
}

Would become this:

if (!isMetaEvent(eventName)) {
  withMetaEventsAllowed(() => {
    this.emit(listenerAdded, {eventName, listener});
  });
}

This would eliminate the need for emit to have a third param and subclasses could do whatever they want. Of course, the downside is that it introduces some global state (but since it wouldn't be exposed to userland maybe it's not so bad?).

Just a rough idea I had; could probably be improved. But if it sounds fine to you then I'll go ahead and make a PR.

from emittery.

sindresorhus avatar sindresorhus commented on May 20, 2024

Or we can have a special function that emits the meta events on a given emitter instance. Something like this.

👍 Sounds good

from emittery.

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.