runem / lit-analyzer

Monorepository for tools that analyze lit-html templates
MIT License
314 stars 35 forks source link

Allow setting event listener to asyncReplace #343

Open nmattia opened 6 months ago

nmattia commented 6 months ago

It would be nice if lit-analyzer allowed settings event listeners to (well-typed) asyncReplace instances. Here's a simple (though somewhat silly) example where the click function changes depending on the date (there's definitely better ways to do this but lit seems to accept this):

import {html, LitElement} from 'lit';
import {customElement} from 'lit/decorators.js';
import {asyncReplace} from 'lit/directives/async-replace.js';

async function *getUrl() {
  while (true) {
    const date = new Date();
    const day = date.getDay();
    yield "https://example.com/days/" + day;
    await new Promise(resolve => setTimeout(resolve, 1000));
  }
}

@customElement('button-date')
export class ButtonDate extends LitElement {
  render() {
    return html`<button 
         @click=${asyncReplace(getUrl(), url => fetch(url as any))}>
          submit today</button>`;
  }
}