Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/davidkpiano/durable-entities-xstate

XState + Durable Entities = 🚀
https://github.com/davidkpiano/durable-entities-xstate

Last synced: about 2 months ago
JSON representation

XState + Durable Entities = 🚀

Awesome Lists containing this project

README

        

# durable-entities-xstate

XState + Durable Entities = 🚀

## Prerequisites

- Ensure you have all the prerequisites satisfied [in the Azure Functions documentation](https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local).
- 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](https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local#local-settings-file)

## Quick Start

1. Run `npm install` to install the required dependencies.
1. 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](https://xstate.js.org/viz/?gist=529435f997b4276799778db37f64b0da)

**To send an event to an entity:**

Open up an API client, such as [Postman](https://postman.com), and `POST` a JSON event object, which is an object that contains a `{ type: "someEventType" }` property, to `http://localhost:7071/api/DonutTrigger?id=`:

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

{
"type": "NEXT"
}
```

Or using `curl`:

```bash
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=`

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

Or using `curl`:

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

Example response:

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