Skip to content
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

Merged
merged 22 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type {
InspectionEvent,
ActorSystem
} from './system.ts';

export { toPromise } from './toPromise.ts';
export { and, not, or, stateIn } from './guards.ts';
export { setup } from './setup.ts';

Expand Down
43 changes: 43 additions & 0 deletions packages/core/src/toPromise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Actor, AnyActorRef, OutputFrom } from '.';

/**
* Returns a promise that resolves to the `output` of the actor when it is done.
*
* @example
* ```ts
* const machine = createMachine({
* // ...
* output: {
* count: 42
* }
* });
*
* const actor = createActor(machine);
*
* actor.start();
*
* const output = await toPromise(actor);
*
* console.log(output);
* // logs { count: 42 }
* ```
*/
export function toPromise<T extends AnyActorRef>(
actor: T
): Promise<T extends Actor<infer TLogic> ? OutputFrom<TLogic> : unknown> {
return new Promise((resolve, reject) => {
actor.subscribe({
complete: () => {
resolve(
actor.getSnapshot().output
// actor.getOutput()! as T extends Actor<infer TLogic>
// ? OutputFrom<TLogic>
// : unknown
);
},
error: (err) => {
reject(err);
}
});
});
}
70 changes: 70 additions & 0 deletions packages/core/test/toPromise.test.ts
davidkpiano marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { createActor, createMachine, fromPromise, toPromise } from '../src';

describe('toPromise', () => {
it('should be awaitable', async () => {
const promiseActor = createActor(
fromPromise(() => Promise.resolve(42))
).start();

const result = await toPromise(promiseActor);

result satisfies number;

expect(result).toEqual(42);
});

it('should await actors', async () => {
const machine = createMachine({
types: {} as {
output: { count: 42 };
},
initial: 'pending',
states: {
pending: {
on: {
RESOLVE: 'done'
}
},
done: {
type: 'final'
}
},
output: { count: 42 }
});

const actor = createActor(machine).start();

setTimeout(() => {
actor.send({ type: 'RESOLVE' });
}, 1);

const data = await toPromise(actor);

data satisfies { count: number };

expect(data).toEqual({ count: 42 });
});

it('should await already done actors', async () => {
const machine = createMachine({
types: {} as {
output: { count: 42 };
},
initial: 'done',
states: {
done: {
type: 'final'
}
},
output: { count: 42 }
});

const actor = createActor(machine).start();

const data = await toPromise(actor);

data satisfies { count: number };

expect(data).toEqual({ count: 42 });
});
});