Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coarse Grained (Inferred) Observability for JSX #87

Closed
thescientist13 opened this issue Aug 10, 2022 · 0 comments · Fixed by #94
Closed

Coarse Grained (Inferred) Observability for JSX #87

thescientist13 opened this issue Aug 10, 2022 · 0 comments · Fixed by #94
Assignees
Labels
0.7.0 documentation Improvements or additions to documentation expirement feature New feature or request JSX

Comments

@thescientist13
Copy link
Member

thescientist13 commented Aug 10, 2022

Type of Change

  • New Feature Request

Summary

Related to #84 , it would be cool if for something like this

class TodoListItem extends HTMLElement {
  constructor() {
    super();
    this.todo = {};
  }

  render() {
    const { completed, task } = this.todo;
    const completionStatus = completed ? '✅' : '⛔';
    
    return (
      <span>
        {task} <span>{completionStatus}</span>
        
         <button onclick={() => this.dispatchDeleteTodoEvent(this.todo.id)}></button>          
      </span>
    );
  }
}

customElements.define('todo-list-item', TodoListItem);

Details

So the compiled output would look something like this

class TodoListItem extends HTMLElement {
  constructor() {
    super();
    this.todo = {};
  }

  static get observedAttributes () {
    return ['todo'];
  }

  attributeChangedCallback(name, oldValue, newValue) {
    if (newValue !== oldValue) {
      this.render();
    }
  }

  render() {
    ...
  }
}

customElements.define('todo-list-item', TodoListItem);

In this case we can automatically infer generate the observedAttributes based on what is getting used in the render function, and then from there compute the an attributeChangedCallback function. 🤩

Details

To be fair, the reason this is considered coarse-grained is because attributeChangedCallback just reruns the render function completely, so all the (inner) HTML is blown out, so not very efficient, but certainly helpful nonetheless IMO!

But... as the name also implies, this means that there could / should be room for fine-grained observability as well, such that we can the compiled knowledge about the template and component and instead run an update function that knows hows to more tactically call textContent or setAttribute to the specific DOM node, rather than just re-computing innerHTML. One thing at a time though. 😅

@thescientist13 thescientist13 added feature New feature or request expirement JSX labels Aug 10, 2022
@thescientist13 thescientist13 changed the title automatic observability Automatic Observability for JSX Aug 10, 2022
@thescientist13 thescientist13 changed the title Automatic Observability for JSX Automatic (Inferred) Observability for JSX Sep 2, 2022
@thescientist13 thescientist13 self-assigned this Dec 28, 2022
@thescientist13 thescientist13 changed the title Automatic (Inferred) Observability for JSX Coarse Grained (Inferred) Observability for JSX Dec 30, 2022
@thescientist13 thescientist13 added the documentation Improvements or additions to documentation label Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.7.0 documentation Improvements or additions to documentation expirement feature New feature or request JSX
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant