Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/natserract/xstate
A State machines library for elixir
https://github.com/natserract/xstate
elixir fsm functional-programming genserver state-machine
Last synced: 18 days ago
JSON representation
A State machines library for elixir
- Host: GitHub
- URL: https://github.com/natserract/xstate
- Owner: natserract
- Created: 2022-07-14T09:01:01.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-07-19T13:14:30.000Z (over 2 years ago)
- Last Synced: 2024-10-13T03:11:02.601Z (3 months ago)
- Topics: elixir, fsm, functional-programming, genserver, state-machine
- Language: Elixir
- Homepage: https://hex.pm/packages/xstate
- Size: 47.9 KB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Xstate
`Xstate` is a State Machine library for Elixir## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `xstate` to your list of dependencies in `mix.exs`:```elixir
def deps do
[
{:xstate, "~> 0.1.0"}
]
end
```## Usage
```elixir
import Xstate
alias Xstate.StateMachinefunc = fn msg, inside ->
IO.puts("#{msg} in #{inside}")
{:ok, msg}
endmachine =
%StateMachine.Machine{
initial_state: "created",
mapping: %{
:created => %StateMachine.Transitions{
target: "customer_confirmed",
before: fn _ ->
func.(:before_transition_, "created")
end,
callback: fn _ ->
func.(:after_transition, "created")
end
},
:customer_confirmed => %StateMachine.Transitions{
target: "awaiting_payment",
before: nil,
callback: nil
}
},
modifiable_states: MapSet.new(["created"])
}
|> StateMachine.new()# state before transition
assert "created" == StateMachine.get_states(machine)# Invoke transition
StateMachine.transition(machine, "created")# state after transition
assert "customer_confirmed" == StateMachine.get_states(machine)
```