Re: [whatwg/dom] Improving ergonomics of events with Observable (#544)

This might be a bit off-topic, but what about a stronger integration with DOM beyond events? From my perspective, `Observable`s can not only contribute to ergonomics of DOM event handling, but also to creating _dynamic_ DOM content. `Observable`s are the ideal type for reflecting values changing overtime, so if DOM elements could _bind_ to them (which would be analogous to setting attributes to values that do not change overtime, e.g. plain strings), it could greatly simplify the computation and tooling required for creating dynamic UIs.

What I mean is something like this:
```cs
partial interface Node {
  void bindTextContent(Observable<string> observable, optional AbortSignal signal);
};

partial interface Element {
  void bindAttribute(string name, Observable<string> observable, optional AbortSignal signal);
  void bindAttribute(string name, Observable<bool> observable, optional AbortSignal signal);
};
```
```js
// --> so the element will become a dynamic counter
element.bindTextContent(timer(0, 1000));

// --> the element will change class each second
element.bindAttribute('class', 
  interval(1000).pipe(
    map(x => x % 2 == 0?'red':'blue')
  )
);

// --> the element would disappear and reappear each second
// --> the `hidden` attribute basically goes away when the observable value is `false`
element.bindAttribute('hidden', interval(1000).pipe(map(x => x % 2 ==0))); 
```

Of course this raises some further questions, most importantly when to subscribe and when to cancel that subscription. I guess (but am not sure) that automatically subscribing to bound `Observable`s _when the element is rendered on main document_ and unsubscribing when _the element is being cleaned up_, combined with the fact that one could clean up the subscription earlier via the `AbortSignal`, should suffice. But still, this sort of would add a concept of a _life cycle_ to DOM elements, so it is noteworthy.

And again, sorry if this is off-topic, but I assume that this issue is one of the main reasons for pushing forward standardization of `Observable`s, and wanted to help by providing further use-case via further integration with DOM.


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

Received on Wednesday, 20 May 2020 10:57:20 UTC