Skip to content

Commit

Permalink
Improve event cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkleeman committed Jan 2, 2024
1 parent f9aa7ab commit 6f5eb96
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 172 deletions.
101 changes: 57 additions & 44 deletions typescript/xstate/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,69 @@

import * as restate from "@restatedev/restate-sdk";

import {createMachine} from 'xstate';
import {createMachine, sendTo} from 'xstate';
import {bindXStateRouter} from "./lib";
import {fromPromise} from "./promise";

export const workflow = createMachine(
{
id: 'async-function-invocation',
initial: 'Send email',
types: {} as {
context: {
customer: string;
};
input: {
customer: string;
};
},
context: ({input}) => ({
customer: input.customer
}),
states: {
'Send email': {
invoke: {
src: 'sendEmail',
input: ({context}) => ({
customer: context.customer
}),
onDone: 'Email sent'
}
const authServerMachine = createMachine({
id: 'server',
initial: 'waitingForCode',
states: {
waitingForCode: {
on: {
CODE: {
target: "process"
},
},
'Email sent': {
type: 'final'
},
process: {
invoke: {
id: 'process',
src: 'authorise',
onDone: {
actions: sendTo(
({self}) => self._parent!,
{ type: 'TOKEN' },
{ delay: 1000 },
),
},
}
}
},
{
actors: {
sendEmail: fromPromise(async ({ input }) => {
console.log('Sending email to', input.customer);

await new Promise<void>((resolve) =>
setTimeout(() => {
console.log('Email sent to', input.customer);
resolve();
}, 1000)
);
})
}
},
}
}, {
actors: {
authorise: fromPromise(() => new Promise((resolve) => setTimeout(resolve, 5000))),
}
);
});

const authClientMachine = createMachine({
id: 'client',
initial: 'idle',
states: {
idle: {
on: {
AUTH: {target: 'authorizing'},
},
},
authorizing: {
invoke: {
id: 'auth-server',
src: authServerMachine,
},
entry: sendTo('auth-server', ({self}) => ({
type: 'CODE',
sender: self,
})),
on: {
TOKEN: {target: 'authorized'},
},
},
authorized: {
type: 'final',
},
},
});


bindXStateRouter(restate.createServer(), "foo", workflow).listen()
bindXStateRouter(restate.createServer(), "foo", authClientMachine).listen()

Loading

0 comments on commit 6f5eb96

Please sign in to comment.