htm icon indicating copy to clipboard operation
htm copied to clipboard

Add support iterator

Open sirenkovladd opened this issue 1 year ago • 1 comments

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 avatar Jan 31 '24 07:01 sirenkovladd

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

sirenkovladd avatar Jan 31 '24 08:01 sirenkovladd