rxooks
TypeScript icon, indicating that this package has built-in type declarations

0.0.5 • Public • Published

rxooks

GADZOOKS! RXOOKS!

(Basically every other iteration of "rxjs", "rx", "react", and "hooks" was taken)

This is a library of hooks that are useful with async code. Generally, these small, lightweight hooks are all you will need in most case.

What is this good for?

  1. async effects that are cancellable and give you values using:
  • promises (and async/await)
  • async iterators (async function*)
  • Observables! (obviously)
  1. creating updatable state that will give you something reactive, rather than firing a new render (ala useState)

  2. Dealing with any type that returns an "unsubscribable" { unsubscribe: () => void }, like an RxJS Subscription

Installation

npm i -S rxooks

Hooks

useAsyncValues

This is a hook that allows you to start an async effect that will self-cancel on dependency changes, pretty much like useEffect only it outputs a value, and you can use async function, async function* or any function that returns Promise, AsyncIterable, or Observable, (including things that implement Symbol.observable).

async functions

The code below will display "Loading..", and then "Hi!" after 2 seconds.

import { useAsyncValues } from 'rxooks';

function sleep(ms: number): Promise<void> {
	return new Promise((resolve) => setTimeout(resolve, ms));
}

function MyComp() {
	const value = useAsyncValues(
		async function () {
			await sleep(2000);

			return 'Hi!';
		},
		[
			/* deps */
		],
		{ initialValue: 'Loading...' }
	);

	return <div>{value}</div>;
}

async iterables

The following creates a component that increments a counter once per second using an async generator.

import { useAsyncValues } from 'rxooks';

function MyComp() {
	const value = useAsyncValues(
		async function* () {
			let n = 0;
			while (true) {
				yield n;
				await sleep(1000);
			}
		},
		[
			/* deps */
		]
	);

	return <div>Count: {value}</div>;
}

observables

import { useAsyncValues } from 'rxooks';
import { interval } from 'rxjs';

function App() {
	const value = useAsyncValues(
		() => interval(1000),
		[
			/* deps */
		]
	);

	return <div>Count: {value}</div>;
}

export default App;

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.0.5
    3
    • latest

Version History

Package Sidebar

Install

npm i rxooks

Weekly Downloads

3

Version

0.0.5

License

none

Unpacked Size

11.7 kB

Total Files

6

Last publish

Collaborators

  • blesh