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

format with prettier. update getPersistedSnapshot() function call #4610

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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.