Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions packages/restate-xstate-test/src/lib/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export type MachineApi = {
send: (context: unknown, args: { event: AnyEventObject }) => Promise<unknown>;

snapshot: (context: unknown) => Promise<unknown>;

nextEvents: (context: unknown) => Promise<string[]>;
};

export type RunMachineOptions = {
Expand All @@ -34,6 +36,7 @@ export type RunMachineOptions = {
export type RunningMachine<SnapshotType> = {
send: (event: AnyEventObject) => Promise<SnapshotType>;
snapshot(): Promise<SnapshotType>;
nextEvents(): Promise<SnapshotType>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the return type be Promise<string[]> ?

[Symbol.dispose](): void;
};

Expand Down Expand Up @@ -67,6 +70,10 @@ export async function createRestateTestActor<SnapshotType>(
return (await client.snapshot()) as SnapshotType;
},

nextEvents: async () => {
return (await client.nextEvents()) as SnapshotType;
},

[Symbol.dispose]: () => {
env.stop().catch((err: unknown) => {
console.error("Error stopping environment:", err);
Expand Down
20 changes: 20 additions & 0 deletions packages/restate-xstate/src/lib/actorObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,26 @@ export function actorObject<

return root.getPersistedSnapshot();
},
nextEvents: async (
ctx: restate.ObjectContext<State>,
): Promise<string[]> => {
await validateStateMachineIsNotDisposed(ctx);
const systemName = ctx.key;

// no need to set the version here if we are just getting a snapshot
let version = await ctx.get("version");
if (version == null) {
version = latestLogic.id;
}
const logic = getLogic(latestLogic, versions, version);

const root = await createActor<
LatestStateMachine | PreviousStateMachine
>(ctx, api, systemName, version, logic);

const snapshot = root.getSnapshot();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be root.getPersistedSnapshot() ?

return [...new Set([...snapshot._nodes.flatMap((sn) => sn.ownEvents)])];
},
invokePromise: restate.handlers.object.shared(
async (
ctx: restate.ObjectSharedContext<State>,
Expand Down
3 changes: 2 additions & 1 deletion packages/tests/src/lib/example.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ describe("Simple count machine", () => {
}>({
machine: counter,
});

const nextEvents = await machine.nextEvents();
expect(nextEvents).toStrictEqual(['inc', 'dec'])
await machine.send({ type: "inc" });
await machine.send({ type: "inc" });
await machine.send({ type: "dec" });
Expand Down
Loading