Replies: 1 comment
-
Here's an example in userland. export const createIterator = <A extends AnyActor, C extends ContextFrom<A>>(actor: A, finalState = 'idle') => {
let p = Promise.withResolvers<{ value: C; done: boolean }>();
const subscription = actor.subscribe((state: { context: C; matches: (s: string) => boolean }) => {
p.resolve({ value: state.context, done: state.matches(finalState) ? true : false });
if (state.matches(finalState)) {
subscription.unsubscribe();
}
});
return (async function* () {
while (true) {
const result = await p.promise;
p = Promise.withResolvers();
yield result.value;
if (result.done) break;
}
})();
};
const iterator = createIterator(exampleActor);
exampleActor.send({ type: 'fetch' });
for await (const context of iterator) {
const { streamed } = context;
// Perform operations
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Use case:
Allows streaming of data while waiting for state machine to complete.
Example
Beta Was this translation helpful? Give feedback.
All reactions