Skip to content

davidkpiano/durable-entities-xstate

Repository files navigation

durable-entities-xstate

XState + Durable Entities = 🚀

Prerequisites

  • Ensure you have all the prerequisites satisfied in the Azure Functions documentation.
  • Install the DurableTask extension:
    func extensions install --package Microsoft.Azure.WebJobs.Extensions.DurableTask -v 2.3.1
    
  • Copy the local.settings.template.json file and rename to local.settings.json, and configure the AzureWebJobsStorage value to be a valid storage connection string. See the documentation for more info

Quick Start

  1. Run npm install to install the required dependencies.
  2. Hit F5 or run npm start to start the function.

Interacting with the State Machine

In this sample, the DonutEntity is controlled by the following statechart:

View this statechart on XState Viz

To send an event to an entity:

Open up an API client, such as Postman, and POST a JSON event object, which is an object that contains a { type: "someEventType" } property, to http://localhost:7071/api/DonutTrigger?id=<ENTITY ID>:

POST http://localhost:7071/api/DonutTrigger?id=donut1

{
  "type": "NEXT"
}

Or using curl:

curl -d '{"type": "NEXT"}' -H "Content-Type: application/json" -X POST http://localhost:7071/api/DonutTrigger?id=donut1

The text response should be similar to:

Event "NEXT" sent to entity "donut1".

To view the state of an entity:

In the same API client, send a GET request to http://localhost:7071/api/DonutTrigger?id=<ENTITY ID>

GET http://localhost:7071/api/DonutTrigger?id=donut1

Or using curl:

curl http://localhost:7071/api/DonutTrigger?id=donut1

Example response:

{
  "entityExists": true,
  "entityState": {
    ...
    "value": {
      "directions": "makeDough"
    },
    ...
    "event": {
      "type": "NEXT"
    },
    ...
  }
}

About

XState + Durable Entities = 🚀

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published