-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
toPromise(actorRef)
#4198
toPromise(actorRef)
#4198
Conversation
🦋 Changeset detectedLatest commit: 6e35374 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 6e35374:
|
This supersedes #3977 , right? I certainly like this one better 😉 |
Looking forward to this, const entryNode = di.editor.getNode(payload.workflowNodeId);
if (!entryNode) {
throw new Error("Entry node not found");
}
let state = entryNode.actor.getSnapshot();
entryNode.actor.subscribe({
next: (data) => {
io.logger.info("STATE", data);
state = data;
},
complete: () => {
io.logger.log("COMPLETE", state.output);
},
});
di.engine.execute(entryNode.id, undefined, payload.executionId);
await waitFor(entryNode.actor, (state) => state.matches("complete"), {
timeout: 1000 * 60 * 5,
});
io.logger.info("STATE", state); Currently, I'm using the let state = entryNode.actor.getSnapshot();
await new Promise((resolve, reject) => {
entryNode.actor.subscribe({
error(err) {
reject(err);
},
next: (data) => {
io.logger.info("STATE", data);
state = data;
},
complete: () => {
resolve(state);
io.logger.log("COMPLETE", state.output);
},
});
}); Which is also doesn't feel like the best. |
This should be enough to "await" the actor with the v5 API: await waitFor(actorRef, s => s.status === 'done') We might still implement |
For reference, this would be more than a one-liner unfortunately: await waitFor(actorRef, s => s.status === 'done');
const output = actorRef.getSnapshot().output!; |
Still a one-liner :P const output = (await waitFor(actorRef, s => s.status === 'done')).output!; |
packages/core/src/toPromise.ts
Outdated
export function toPromise<T extends AnyActorRef>( | ||
actor: T | ||
): Promise<OutputFrom<T>> { | ||
// TODO: this is typed as `any` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't do much about it as long as we use T extends AnyActorRef
pattern. You unwrap here what you provided for the TSnapshot
within ActorRef
but that's just any
(since you are using AnyActorRef
)
packages/core/src/toPromise.ts
Outdated
if (currentSnapshot.status === 'error') { | ||
return Promise.reject(currentSnapshot.error); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interestingly, this one is not redundant right now. I think this is a problem and I even thought that we had some tests related to this (although maybe indirect ones). I'll take a look at this right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that we'll be able to remove this once this lands: #4570
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved ✅
…into v5/toPromise-1
This PR introduces
toPromise(actor)
, which creates a promise from an actor that resolves withoutput
when the actor is done.