developit / htm

Hyperscript Tagged Markup: JSX alternative using standard tagged templates, with compiler support.
Apache License 2.0
8.73k stars 170 forks source link

Add support iterator #255

Open sirenkovladd opened 9 months ago

sirenkovladd commented 9 months ago

example

const { on } = require('events');
const { request } = require('https');
const html = require('htm');

function h(type, props, ...children) {
  return { type, props, children };
}

request('https://nodejs.org/en', async (res) => {
  const iterator = on(res, "data");
  const domIterator = html.bind(h).iterator(iterator);
  for await (const element of emitter) {
    console.log(element);
  }
}).end();

request('https://nodejs.org/en', async (res) => {
  const iterator = on(res, "data");
  const domEmitter = html.bind(h).emitter(iterator);
  domEmitter.on('div', (element) => {
    console.log('emitter', element);
  });
  domEmitter.on('end', () => {
    console.log('end');
  });
}).end();
sirenkovladd commented 9 months ago

Motivation sometimes the page is too big, but you need to get the elements that are at the beginning an iterator or emitter would allow you to get the information you need without going through the entire page