[whatwg/dom] Composable lifecycle hooks (Issue #1284)

### What problem are you trying to solve?

Currently web component lifecycle hooks exist as instance functions with special names like `connectedCallback()`, `attributeChangedCallback()`, `disconnectedCallback()` and so on.

This means that composing multiple listeners from different sources has a very poor DX. You basically have to _wrap_ the existing `connectedCallback()` with a new one that calls it. Remember how event handling was before `addEventListener()` / `attachEvent()`? Yes, that. And good luck undoing this operation.

Why would you need to do that?
- Having mixins for common custom element behaviors, e.g. defining reflected props to which you pass a class
- If any of the custom attributes proposals gets adopted, this will become even more important.

### What solutions exist today?

_No response_

### How would you solve it?

You basically need a lightweight pub/sub mechanism that doesn't carry the baggage of events, but allows independently adding and removing callbacks and passing arguments to them (for `attributeChangedCallback()`).

As a strawman just to start the conversation, it could look similar to `addEventListener()` (`addLifecycleListener`?) with `type` being `connected`, `disconnected`, `adopted`, `attribute`.

This can co-exist with the current lifecycle hooks, as long as the order of execution is defined (presumably the current lifecycle hooks would be executed first).

### Anything else?

_No response_

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/1284
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/dom/issues/1284@github.com>

Received on Saturday, 27 April 2024 02:02:22 UTC