really-async
A lightweight library for generators in react 😤
Install
pnpm i really-async
License
MIT Copyright (c) 2022-Present Sagnik Pradhan
Ƭ Gen<T
>: AsyncGenerator
<T
, T
, never
>
Async generator that yields and returns
Name |
---|
T |
index.tsx:17
â–¸ 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>
);
}
Name |
---|
T |
Name | Type | Default value | Description |
---|---|---|---|
generatorFactory |
() => Gen <T > |
undefined |
Async generator function |
rush |
boolean |
true |
Immediately render the component |
UseAsyncGeneratorReturnValue
<T
>
An object containing values, error and execute function
index.tsx:64
â–¸ 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);
Name |
---|
P |
Name | Type | Description |
---|---|---|
component |
(props : P ) => Gen <ReactNode > |
Component returning an async generator |
fn
HOC
â–¸ (props
): Element
Name | Type |
---|---|
props |
P |
Element
index.tsx:124