Please note: This site is in an active redesign. Some things might be a little off 🧐

EH

Thinking through hooks versus event callbacks

I have tried to keep my website's functionality pretty separate and clean, but there are definitely some areas I want to clean up.

First is my "routing" modules that create and respond to the individual url calls.

Second is my Micropub handling code. Currently, my Micropub code is just a big mess of changes and adaptions. I've prefer to have separate areas for the attribute processing, file saving, and webmention sending.

For both of these things I essential want a way to be able to write a new module and have it interact with my primary software by saying "Hey, I have a new route" or "Hey, I want to do something after a Micropub request is finished". From my experience there are two ways to do that.

First is what I'll call the "hook" method. That is where you add the module to a specific folder that the software looks for new add-ons. The module has a specific file name like module.js and inside it, it will have pre-specified function names like hook_after_micropub. Then the software will call that function in all the modules that have it. This method is used in Drupal 7 as well as is kind of how Apple's delegate methods work.

Second is what I'll call the "event" method. This is where a new module would import an event hub and you would register your functions to respond to different events. For example you might put

  1. event.register('after_micropub', sendWebmentions).

I can't decide which way I like better. What pros and cons have you found when working with systems like the two above? Or are you familiar with a third way I haven't mentioned?

92.79 ℉☀️techcode-examples
posted using quill.p3k.io

Replies

@EddieHinkle I much prefer the evented model (depending on how you implement it), because the piece generating events isn't coupled with the consumer, and your consumers can generate events as well, that other systems can consume.