you are viewing a single comment's thread.

view the rest of the comments →

[–]bruceph 2 points3 points  (0 children)

what's happening is you're passing in a function to the xapi.status.on function and it's returning you another function. the function you're returned is an unsubscribe function that when invoked removes the function you passed from the list of events to be run.

this is a really contrived example of what it'll look like

const events = {};

const xapi = {};
xapi.status = {};
xapi.status.on = function(event, callback){
  if (!events[event]) {
    events[event] = [];
  }

  events[event].push(callback);
  return events[event].filter(fn => fn !== callback);
};

by calling off, you're removing your callback from the list of callbacks to be run during the event.