npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-iterator

v1.2.0

Published

[![npm](https://img.shields.io/npm/v/use-iterator.svg)](https://www.npmjs.com/package/use-iterator) [![CI](https://github.com/neet/use-iterator/actions/workflows/ci.yml/badge.svg)](https://github.com/neet/use-iterator/actions/workflows/ci.yml) [![codecov]

Downloads

12

Readme

use-iterator

npm CI codecov

React hooks collection for JavaScript's Iterator/generator.

useForAwaitOf(asyncIterable, deps)

Subscribes to the asyncIterable specified in the argument and as soon as a promise is resolved, immediately starts to await the next promise.

This is similar to for-await-of syntax of JavaScript, but the results are wrapped in React APIs so components will be updated whenever the result changes.

import { on } from 'events-to-async';

const changeEvent = on((handler) => {
  const elm = document.getElementById('text_field');
  elm?.addEventListener('change', handler);
  return () => elm?.removeEventListener('change', handler);
});

const App = () => {
  // subscribe to the change events from #text_field
  const result = useForAwaitOf(changeEvent);

  // latest change event from #text_field
  return <span>{result.value}</span>;
};

Note that this example uses an additional module events-to-async to convert Event objects into AsyncIterator.

useGenerator(generator, deps)

Wraps generator function into a React state. You can call next() to retrieve the next value, and read the latest value from the iterator via value.

const App = () => {
  const result =
    useGenerator <
    string >
    (function* () {
      yield "Let's get started";
      yield 'Choose your username';
      yield 'Upload the avatar';
    },
    []);

  if (result.done) {
    return <div>Tutorial completed!</div>;
  }

  return (
    <div>
      <h2>{result.value}</h2>

      <button onClick={() => result.next()}>Next</button>
    </div>
  );
};

useAsyncGenerator(generator, deps)

Wraps async generator function into a React state. You can call next() to retrieve the next value, and read the latest value from the iterator via value.

This is similar to useGenerator described above, but with async you can return Promises from the generator. This would be useful for sequential network requests such as Web APIs with pagination.


const App = () => {
  const result = useAsyncGenerator<string>(function* () {
    yield fetch('https://example.com?page=1');
    yield fetch('https://example.com?page=2');
    yield fetch('https://example.com?page=3');
  }, []);

  if (result.loading) {
    return <span>Loading...</span>
  }

  return (
    <div>
      <ul>
        {result.value.map((item) => (
          <li>...</li>
        ))}
      </ul>

      <button onClick={()) => result.next()}>
        Next
      </button>
    </div>
  );
};

useIterable(iterable, deps)

Wraps iterable into a React state. You can call next() to retrieve the next value, and read the latest value from the iterator via value.

Argument can be any object that implements [Symbol.iterator] protocol such as String and Array.

const result = useIterable('abcd');

result.next();
result.value === 'a';

result.next();
result.value === 'b';

useAsyncIterable(asyncIterable, deps)

Wraps async iterable into a React state. You can call next() to retrieve the next value, and read the latest value from the iterator via value.

Argument can be any object that implements [Symbol.asyncIterator] protocol.

const result = useAsyncIterable({
  [Symbol.asyncIterator]() {
    return {
      i: 0,
      next() {
        if (this.i < 3) {
          return Promise.resolve({ value: this.i++, done: false });
        }

        return Promise.resolve({ done: true });
      },
    };
  },
});

result.next();
result.loading; // === true while loading
result.value === 1;

useIterator(iterator, deps)

A low-level hook for useIterable and useGenerator. Accepts iterator object and returns a reactive state and a dispatcher.

const result = useIterator({
  next: () {...},
  return: () {...},
  throw: () {...},
});

useAsyncIterator(asyncIterator, deps)

A low-level hook for useAsyncIterable and useAsyncGenerator. Accepts async iterator object and returns a reactive state and a dispatcher.

const result = useAsyncIterator({
  next: () {...},
  return: () {...},
  throw: () {...},
});