How to find the sender of a subscribed event?



I’m trying to write a package which modifies the TextBuffer just before it is saved to the disk. I subscribed the TextBuffer::onWillSave() event. But in the callback function, how how can I find out which TextBuffer triggered this event?

Presumably there should be something like a sender() function? But I couldn’t find anything related to this in the documentation.



This is what closures are for:

function addHandler(buffer) {
  let subscription = buffer.onWillSave(() => {
    // Do something to the contents of the buffer

  buffer.onDidDestroy(() => {

Because JavaScript functions create closures, you can access the buffer and subscription variables inside the handlers.


Thanks leedohm. It works perfectly.

I’m a C++ programmer, so not quite familiar with the fancy features in JavaScript.:yum:


Newer versions of C++ include lambda functions and closures too :grinning: But yeah, they’re very new to C++ whereas JavaScript has had them for a long, long time.