decoupled / magic

decoupled/magic
2 stars 0 forks source link

Netlify triggers accept only one handler #4

Open mbucchi opened 4 years ago

mbucchi commented 4 years ago

Currently, magic.netlify.on(...) accepts only one handler per event type.

This is mainly because Netlify triggers lambdas on events depending on how the function files are named (as noted here).

Some ideas on how to deal with this from within @decoupled/magic

Every idea has some pros and cons.

This problem also raises the question of wether it would be possible for Netlify to add multiple lambdas to a trigger event.

aldonline commented 4 years ago

I think we should definitely allow developers to declare multiple trigger handlers throughout the codebase, including repeated ones. The best option seems to be N2 (Bundle all handlers for the same event type into one big handler). This should work as long as the resulting lambda does not need to be split for size/performance reasons. How often would this happen? This is a discussion we should have with the Netlify team.

mbucchi commented 4 years ago

A few things we need to take into consideration if we take that route:

Side effects of each "sub-handler"

For example, take a look at this:

let global = 0
magic.netlify.on("deploy-succeeded", () => {
  global++
})

magic.netlify.on("deploy-succeeded", () => {
  global--
})

This example is pretty dumb, but imagine a case in which both handlers use a side-effecting third party package.

Expensive / slow "sub-handlers"

If the users set time / memory limits for lambdas, bundling handlers together may result in some functions not finishing or even not executing at all.