vitaly-t / sub-events

Lightweight, strongly-typed events, with monitored subscriptions.
https://vitaly-t.github.io/sub-events
MIT License
38 stars 2 forks source link

Add support for `this` during subscription #2

Closed vitaly-t closed 5 years ago

vitaly-t commented 5 years ago

It will be a useful feature, to let method subscribe take a second optional parameter - this context for the notification, so the client won't need to rely on bind for the class, and write the code like this:

event.subscribe(onEvent, this); // pass in the right call context

onEvent() {
    // event-handling class method
   // 'this' context is set correctly
}

Currently we need to write it like this:

event.subscribe(onEvent.bind(this));

which is somewhat less friendly.


Thanks to @babakks for the idea.

vitaly-t commented 5 years ago

Implemented and released in version 0.3.0.