WebReflection / lighterhtml

The hyperHTML strength & experience without its complexity ๐ŸŽ‰
https://medium.com/@WebReflection/lit-html-vs-hyperhtml-vs-lighterhtml-c084abfe1285
ISC License
734 stars 20 forks source link

Question about dom-tagger and observables #102

Closed danny-andrews closed 3 years ago

danny-andrews commented 3 years ago

Hi Andrea,

I've been playing around with the dom-tagger, trying to get observables to render. Here's a simple example below:

import { custom } from "lighterhtml";
import Atom from "kefir.atom";

const { html, render } = custom({
  any: (callback) => (node, childNodes) => {
    const cb = callback.call({ type: "html" }, node, childNodes);

    return (value) => {
      if (value.observe) {
        value.observe({
          value: (newValue) => {
            cb(newValue);
          },
        });
      } else {
        cb(value);
      }
    };
  },
});

const mySimpleComp = (name = Atom("Dan")) => {
  const changeName = () => name.set("Mark");

  return html`
    <h1>Hello ๐Ÿ‘‹ ${name}</h1>
    <button onClick=${changeName}>Change Name</button>
  `;
};

This all works great! However, this results in a memory leak, because I never close the subscription to the observable. Is there anyway to do "cleanup" on primitives rendered inside templates?

Related issue in lit-html: https://github.com/Polymer/lit-html/issues/283.

Thanks!

WebReflection commented 3 years ago

You can either use lighterhtml-plus or simply observe the node via uconnect and frop the observer on disconnected ๐Ÿ‘‹