mardi 21 novembre 2017

Own pubsub implementation vs using addEventListener() + CustomEvent?

Is there a bigger difference between a self made pubsub system and what addEventListener() plus using new CustomEvent?

Pseudocode implementation of pubsub (taken from here):

// Publishing to a topic:
events.publish('/page/load', {
    url: '/some/url/path' // any argument
});
// ...and subscribing to said topic in order to be notified of events:

var subscription = events.subscribe('/page/load', function(obj) {
    // Do something now that the event has occurred
});

// ...sometime later where I no longer want subscription...
subscription.remove();

Native API:

var event = new Event('build');

// Listen for the event.
elem.addEventListener('build', function (e) { ... }, false);

// Dispatch the event.
elem.dispatchEvent(event);

Questions

  • Will both do the same job? (I think so?)
  • So is there actually any noticeable difference? Like performance?
  • What are the pros and cons of both if both ways can be used?

I personally think if you implement it yourself the API looks more elegant than using the provided API. But this is not a technical argument. :)

Resources

Aucun commentaire:

Enregistrer un commentaire