Skip to content

A lightweight library for generators in react 😤

Notifications You must be signed in to change notification settings

SagnikPradhan/really-async

Repository files navigation

really-async

really-async

A lightweight library for generators in react 😤

Install

pnpm i really-async

License

MIT Copyright (c) 2022-Present Sagnik Pradhan

Table of contents

Classes

Interfaces

Type Aliases

Functions

Type Aliases

Gen

Ƭ Gen<T>: AsyncGenerator<T, T, never>

Async generator that yields and returns

Type parameters

Name
T

Defined in

index.tsx:17

Functions

useAsyncGenerator

â–¸ useAsyncGenerator<T>(generatorFactory, rush?): UseAsyncGeneratorReturnValue<T>

A collector for all the values generated and returned by the generator.

Example

import { useAsyncGenerator } from "really-async";
import { fetchMessages, fetchUser } from "$/api/messages";

async function* getTransformedMessages(channelId: string) {
	for await (const message of fetchMessages(channelId)) {
		const user = fetchUser(message.partialUser.id);
		yield { message, user };
	}
}

export function Component({ channelId }: { channelId: string }) {
	const { values: messages } = useAsyncGenerator(() =>
		getTransformedMessages(channelId)
	);

	return (
		<div>
			{messages.map((message) => (
				<Message key={message.id} data={message} />
			))}
		</div>
	);
}

Type parameters

Name
T

Parameters

Name Type Default value Description
generatorFactory () => Gen<T> undefined Async generator function
rush boolean true Immediately render the component

Returns

UseAsyncGeneratorReturnValue<T>

An object containing values, error and execute function

Defined in

index.tsx:64


wrapAsyncGeneratorComponent

â–¸ wrapAsyncGeneratorComponent<P>(component): (props: P) => Element

HOC for generator components. Renders the last value

Example

import fs from "fs/promises";
import { wrapAsyncGeneratorComponent } from "really-async";
import {
	getAllNodes,
	getAllDependantNodes,
	optimizeNodes,
} from "$/utils/nodes";

async function* CreateTree() {
	const nodes = yield* getAllNodes();
	const dependantNodes = yield* getAllDependantNodes(nodes);
	const result = yield* optimizeNodes(dependantNodes);

	yield "Writing file";
	await fs.writeFile(result.print());

	return <Result result={result} />;
}

export default wrapAsyncGeneratorComponent(CreateTree);

Type parameters

Name
P

Parameters

Name Type Description
component (props: P) => Gen<ReactNode> Component returning an async generator

Returns

fn

HOC

â–¸ (props): Element

Parameters
Name Type
props P
Returns

Element

Defined in

index.tsx:124

About

A lightweight library for generators in react 😤

Topics

Resources

Stars

Watchers

Forks