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

@repeaterjs/react-hooks

v0.1.1

Published

React hooks for using async iterators in components

Downloads

4

Readme

@repeaterjs/react-hooks

React hooks for working with async iterators/generators.

These functions are implemented with repeaters. For more information about repeaters, visit repeater.js.org.

Installation

npm install @repeaterjs/react-hooks
yarn add @repeaterjs/react-hooks

API

useResult

declare function useResult<T, TDeps extends any[]>(
  callback: (deps: AsyncIterableIterator<TDeps>) => AsyncIterableIterator<T>,
  deps?: TDeps,
): IteratorResult<T> | undefined;

import {useResult} from "@repeaterjs/react-hooks";

const result = useResult(async function *() {
  /* ... */
});

callback is a function which returns an async iterator, usually an async generator function. The callback is called when the component initializes and the returned iterator updates the component whenever it produces results. useResult returns an IteratorResult, an object of type {value: T, done: boolean}, where T is the type of the produced values, and done indicates whether the iterator has returned. The first return value from this hook will be undefined, indicating that the iterator has yet to produce any values.

function Timer() {
  const result = useResult(async function*() {
    let i = 0;
    while (true) {
      yield i++
      await new Promise((resolve) => setTimeout(resolve, 1000));
    }
  });

  return <div>Seconds: {result && result.value}</div>;
}

Similar to the useEffect hook, useResult accepts an array of dependencies as a second argument. However, rather than being referenced via closure, the dependencies are passed into the callback as an async iterator which updates whenever any of the dependencies change. We pass the dependencies in manually because callback is only called once, and dependencies referenced via closure become stale as the component updates.

function ProductDetail({productId}) {
  const result = useResult(async function *(deps) {
    for await (const [productId] of deps) {
      const data = await fetchProductData(productId);
      yield data.description;
    }
  }, [productId]);

  if (result == null) {
    return <div>Loading...</div>;
  }

  return <div>Description: {result.value}</div>;
}

useValue

declare function useValue<T, TDeps extends any[]>(
  callback: (deps: AsyncIterableIterator<TDeps>) => AsyncIterableIterator<T>,
  deps?: TDeps,
): T | undefined;

import {useValue} from "@repeaterjs/react-hooks";

const value = useValue(async function *() {
  /* ... */
});

Similar to useResult, except the IteratorResult’s value is returned rather than the IteratorResult object itself. Prefer useValue over useResult when you don’t need to distinguish between whether values were yielded or returned.

useAsyncIter

declare function useAsyncIter<T, TDeps extends any[]>(
  callback: (deps: AsyncIterableIterator<TDeps>) => AsyncIterableIterator<T>,
  deps: TDeps = ([] as unknown) as TDeps,
): AsyncIterableIterator<T>;

import {useAsyncIter} from "@repeaterjs/react-hooks";

function MyComponent() {
  const iter = useAsyncIter(async function *() {
    /* ... */
  });
  /* ... */
}

Similar to useResult and useValue, except that useAsyncIter returns the async iterator itself rather than consuming it. The returned async iterator can be referenced via closure in further useResult calls. Prefer useAsyncIter over useResult or useValue when you want to use an async iterator without updating each time a value is produced.

const konami = ["ArrowUp", "ArrowUp", "ArrowDown", "ArrowDown", "ArrowLeft", "ArrowRight", "ArrowLeft", "ArrowRight", "b", "a"];
function Cheats() {
  const keys = useAsyncIter(() => {
    return new Repeater(async (push, stop) => {
      const listener = (ev) => push(ev.key);
      window.addEventListener("keyup", listener);
      await stop;
      window.removeEventListener("keyup", listener);
    });
  });

  const result = useResult(async function *() {
    let i = 0;
    yield konami[i];
    for await (const key of keys) {
      if (key === konami[i]) {
        i++;
      } else {
        i = 0;
      }

      if (i < konami.length) {
        yield konami[i];
      } else {
        return "Cheats activated";
      }
    }
  });

  if (result == null) {
    return null;
  } else if (result.done) {
    return <div>🎉 {result.value} 🎉</div>;
  }

  return <div>Next key: {result.value}</div>;
}

useRepeater

declare function useRepeater<T>(
  buffer?: RepeaterBuffer<T>,
): [Repeater<T>, Push<T>, Stop];

import { useRepeater } from "@repeaterjs/react-hooks";
import { SlidingBuffer } from "@repeaterjs/repeater";

function MyComponent() {
  const [repeater, push, stop] = useRepeater(new SlidingBuffer(10));
  /* ... */
}

Creates a repeater which can be used in useResult callbacks. push and stop can be used in later callbacks to update the repeater. For more information about the push and stop functions or the buffer argument, refer to the repeater.js docs.

function MarkdownEditor() {
  const [inputs, pushInput] = useRepeater();
  const result = useResult(async function*() {
    const md = new Remarkable();
    for await (const input of inputs) {
      yield md.render(input);
    }
  });
  return (
    <div>
      <h3>Input</h3>
      <textarea
        defaultValue="Hello, **world**!"
        onChange={(ev) => push(ev.target.value)}
      />
      <h3>Output</h3>
      <div dangerouslySetInnnerHTML={{ __html: result && result.value }} />
    </div>
  );
}

See also:

  • react-coroutine Define React components with generators, async functions and async generators.