Coder Social home page Coder Social logo

Context binding feature about tseep HOT 8 CLOSED

lpinca avatar lpinca commented on May 25, 2024
Context binding feature

from tseep.

Comments (8)

lpinca avatar lpinca commented on May 25, 2024 1

No, thank you. I actually don't need the feature as I'm not planning to use an external dependency for the EventEmitter. Maybe for the browser but I'm not writing any front-end code at the moment.

from tseep.

Morglod avatar Morglod commented on May 25, 2024 1

Found only one solution without impact on performance.
The problem is that any change around context binding inside hot path (.emit) results in bad JIT code & allocations.

Added addListenerBound / removeListenerBound that binds listener to EventEmitter or other specified context.

552b310

from tseep.

Morglod avatar Morglod commented on May 25, 2024

I agree that I should write something like (not drop-in replacement for node's eventemitter).

But there is nothing "fundamental" in it.
Its some strange anti-pattern as most of other "fundamental" things in node's emitter, which no one should rely on.

from tseep.

lpinca avatar lpinca commented on May 25, 2024

Its some strange anti-pattern as most of other "fundamental" things in node's emitter, which no one should rely on.

That is your personal opinion which I respect but I disagree. It is feature present since the early days of the EventEmitter and is heavily relied on in Node.js core and many user-land modules. It allows to avoid calling Function.prototype.bind() every time.

from tseep.

Morglod avatar Morglod commented on May 25, 2024

There are also 'newListener'/'removeListener'/'error' events which also presents since the same early days. Which I think is more useful than this fundamental thing, but ee3 has no implementations for this.

Without this fundamental thing, you could just:

ee.on('foo', function () {
  const self = ee;
  // do whatever you like
  assert.strictEqual(self, ee);
});

Could you give please some examples of packages which rely on this thing?

from tseep.

lpinca avatar lpinca commented on May 25, 2024

Some examples on top of my mind are:

Without this fundamental thing, you could just:

Which means using a wrapper function for every listener just like Function.prototype.bind().

from tseep.

Morglod avatar Morglod commented on May 25, 2024

I see, thanks for links!
So real example is inheritance thing where you have no reference to instance other than this.

I dislike inheritance thing, but agree that its common pattern everywhere
Will try to implement it (or make some kind of "compatibility" version)

Do you have any other missing features needed?

from tseep.

mustafa519 avatar mustafa519 commented on May 25, 2024

Fixing this issue will break the __proto__ = null optimization?

Well, I mostly have to bind the instance otherwise it causes Cannot read properties of undefined (reading 'events') error. Especially, if I need to pass the events variable, which was defined as EventEmitter instance.

So, if it kills the optimization, what would you suggest instead? Or would you consider adding an option like autoBind: true?

from tseep.

Related Issues (4)

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.