Skip to content

Commit

Permalink
Bring Express example up to date with v5 (#4610)
Browse files Browse the repository at this point in the history
* format with prettier. update gerPersistedSnapshot() function call

* Update examples/express-workflow/index.ts

Co-authored-by: David Khourshid <davidkpiano@gmail.com>

---------

Co-authored-by: David Khourshid <davidkpiano@gmail.com>
  • Loading branch information
gavination and davidkpiano authored Dec 21, 2023
1 parent 998a669 commit 6b74bbe
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
28 changes: 14 additions & 14 deletions examples/express-workflow/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { createActor } from 'xstate';
import bodyParser from 'body-parser';
import { createActor } from "xstate";
import bodyParser from "body-parser";

function generateActorId() {
return Math.random().toString(36).substring(2, 8);
}

const persistedStates: Record<string, unknown> = {};

import express from 'express';
import { machine } from './machine';
import express from "express";
import { machine } from "./machine";

const app = express();

Expand All @@ -19,12 +19,12 @@ app.use(bodyParser.json());
// - Starts the actor
// - Persists the actor state
// - Returns the actor ID in the response
app.post('/workflows', (req, res) => {
app.post("/workflows", (req, res) => {
const workflowId = generateActorId(); // generate a unique ID
const actor = createActor(machine).start();

// @ts-ignore
persistedStates[workflowId] = actor.getPersistedState();
persistedStates[workflowId] = actor.getPersistedSnapshot();

res.send({ workflowId });
});
Expand All @@ -35,21 +35,21 @@ app.post('/workflows', (req, res) => {
// - Sends the event from the request body to the actor
// - Persists the updated state
// - Returns the updated state in the response
app.post('/workflows/:workflowId', (req, res) => {
app.post("/workflows/:workflowId", (req, res) => {
const { workflowId } = req.params;
const snapshot = persistedStates[workflowId];

if (!snapshot) {
return res.status(404).send('Actor not found');
return res.status(404).send("Actor not found");
}

const event = req.body;
const actor = createActor(machine, { state: snapshot }).start();
const actor = createActor(machine, { snapshot }).start();

actor.send(event);

// @ts-ignore
persistedStates[workflowId] = actor.getPersistedState();
persistedStates[workflowId] = actor.getPersistedSnapshot();

actor.stop();

Expand All @@ -60,18 +60,18 @@ app.post('/workflows/:workflowId', (req, res) => {
// - Gets the actor ID from request params
// - Gets the persisted state for that actor
// - Returns the persisted state in the response
app.get('/workflows/:workflowId', (req, res) => {
app.get("/workflows/:workflowId", (req, res) => {
const { workflowId } = req.params;
const persistedState = persistedStates[workflowId];

if (!persistedState) {
return res.status(404).send('Actor not found');
return res.status(404).send("Actor not found");
}

res.json(persistedState);
});

app.get('/', (_, res) => {
app.get("/", (_, res) => {
res.send(`
<html>
<body style="font-family: sans-serif;">
Expand All @@ -88,5 +88,5 @@ app.get('/', (_, res) => {
});

app.listen(4242, () => {
console.log('Server listening on port 4242');
console.log("Server listening on port 4242");
});
1 change: 1 addition & 0 deletions examples/express-workflow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@types/body-parser": "^1.19.5",
"body-parser": "^1.20.2",
"express": "^4.18.2",
"prettier": "^3.1.1",
"ts-node": "^10.9.1",
"xstate": "^5.0.0"
},
Expand Down
21 changes: 17 additions & 4 deletions examples/express-workflow/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6b74bbe

Please sign in to comment.