Skip to content

Finite State Machine implementation with Durable Objects

License

Notifications You must be signed in to change notification settings

drivly/state.do

Repository files navigation

Finite State Machine implementation with Durable Objects based on xstate. Use the editor to create and export your state machine, then optionally add callback fetch URLs/inits to automate the machine events.

API

Read current state:

https://state.do/:key

Send event to machine:

https://state.do/:key/:event

Initialize machine:

https://state.do/:key?{"id":"fetch","initial":"init","states":{"init":{"on":{"FETCH":"loading"}},"loading":{"callback":"https://fetcher.do/60sec/https://example.com/","on":{"4XX":"fault","5XX":"failure","*":"success"}},"failure":{"callback":"https://alarms.do/?fromnow=10sec&callback=https://state.do/:key/RETRY","on":{"RETRY":{"target":"loading"}}},"fault":{"callback":"https://example.logging.do/error?message=","type":"final"},"success":{"callback":"https://graphology.do.cf/:key?newnode?example|","type":"final"}}}

or POST the machine to https://state.do/:key

Callbacks

Callbacks can be added to the state's meta property as a url or as a configuration object with url and init or body:

"State": {
  "meta": {
    "callback": "https://webhooks.do/state",
    "body": {
      "Hello": "world"
    }
  },
  "on": {
    "Event": {
      "target": "State 2"
    }
  }
},

More than one callback can be called by passing an array of callbacks.

HTTP status codes from the callback can be passed as events to the state machine by adding the status code as an event, optionally with wildcards (e.g. 4XX to trap client-side errors).

Example machine with callbacks

stateDiagram-v2
[*]-->loading: FETCH
loading-->⦿ fault: 4XX
loading-->failure: 5XX
loading-->⦿ success: *
failure-->loading: RETRY
{
  "id": "fetch",
  "initial": "init",
  "states": {
    "init": {
      "on": {
        "FETCH": "loading"
      }
    },
    "loading": {
      "callback": "https://fetcher.do/60sec/https://example.com/",
      "on": {
        "4XX": "fault",
        "5XX": "failure",
        "*": "success"
      }
    },
    "failure": {
      "callback": "https://alarms.do/?fromnow=10sec&callback=https://state.do/:key/RETRY",
      "on": {
        "RETRY": {
          "target": "loading"
        }
      }
    },
    "fault": {
      "callback": "https://example.logging.do/error",
      "type": "final"
    },
    "success": {
      "callback": {
        "url": "https://graphology.do.cf/:key?newnode?example",
        "init": {
          "method": "POST"
        }
      },
      "type": "final"
    }
  }
}

Driv.ly is simple APIs to buy & sell cars online, funded by some of the biggest names in automotive and finance & insurance

We're building our entire infrastructure on Cloudflare Workers, Durable Objects, KV, R2, and PubSub. If you're as passionate about these transformational technologies as we are, we'd love for you to join our rapidly-growing team.

About

Finite State Machine implementation with Durable Objects

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •