{"id":13431480,"url":"https://github.com/prasannavl/LiquidState","last_synced_at":"2025-03-16T11:31:44.080Z","repository":{"id":23824019,"uuid":"27200892","full_name":"prasannavl/LiquidState","owner":"prasannavl","description":"Efficient asynchronous and synchronous state machines for .NET","archived":false,"fork":false,"pushed_at":"2020-10-03T05:13:56.000Z","size":524,"stargazers_count":244,"open_issues_count":6,"forks_count":29,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-03-13T20:09:16.457Z","etag":null,"topics":["async","c-sharp","state-machine"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prasannavl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-26T23:34:40.000Z","updated_at":"2025-03-06T17:17:57.000Z","dependencies_parsed_at":"2022-07-15T13:47:09.817Z","dependency_job_id":null,"html_url":"https://github.com/prasannavl/LiquidState","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prasannavl%2FLiquidState","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prasannavl%2FLiquidState/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prasannavl%2FLiquidState/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prasannavl%2FLiquidState/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prasannavl","download_url":"https://codeload.github.com/prasannavl/LiquidState/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243863117,"owners_count":20360268,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","c-sharp","state-machine"],"created_at":"2024-07-31T02:01:03.408Z","updated_at":"2025-03-16T11:31:43.534Z","avatar_url":"https://github.com/prasannavl.png","language":"C#","funding_links":[],"categories":["Frameworks, Libraries and Tools","others","框架, 库和工具","State machines","状态机"],"sub_categories":["Scheduler and Job","任务计划"],"readme":"LiquidState\n====\n\nEfficient state machines for .NET with both synchronous and asynchronous support.\nHeavily inspired by the excellent state machine library [**Stateless**](https://github.com/nblumhardt/stateless) by\n**Nicholas Blumhardt.**\n\n[![NuGet badge](https://buildstats.info/nuget/LiquidState)](https://www.nuget.org/packages/LiquidState)\n\nInstallation\n----\n\n**NuGet:**\n\n\u003e Install-Package LiquidState\n\n**Supported Platforms:**\n\u003e .NETPlatform 1.0 (Formerly PCL259 profile - Supports .NETCore, .NETDesktop, Xamarin and Mono) \n\nHighlights\n----\n\n- Zero heap allocations during the machine execution - GC friendly and high-performance. (Awaitable machines still incur the async/await\ncosts).\n- Fully supports async/await methods everywhere =\u003e `OnEntry`, `OnExit`, during trigger, and even trigger conditions.\n- Builds a linked object graph internally during configuration making it a much faster and more efficient implementation than regular dictionary based implementations.\n- Both synchronous, and asynchronous machines with full support for `async-await`.\n- `MoveToState`, to move freely between states, without triggers.\n- `PermitDynamic` to support selection of states dynamically on-the-fly.\n- `Diagnostics` in-built to check for validity of triggers, and currently available triggers.\n\n[**Release Notes**](https://github.com/prasannavl/LiquidState/blob/master/ReleaseNotes.md)\n\nHow To Use\n---\n\nYou only ever create machines with the `StateMachineFactory` static class. This is the factory for both configurations and the machines. The different types of machines given above are automatically chosen based on the parameters specified from the factory.\n\n**Step 1:** Create a configuration:\n\n```c#\nvar config = StateMachineFactory.CreateConfiguration\u003cState, Trigger\u003e();\n```\n\nor for awaitable, or async machine:\n\n```c#\nvar config = StateMachineFactory.CreateAwaitableConfiguration\u003cState, Trigger\u003e();\n```\n\n**Step 2:** Setup the machine configurations using the fluent API.\n\n```c#\n    config.ForState(State.Off)\n        .OnEntry(() =\u003e Console.WriteLine(\"OnEntry of Off\"))\n        .OnExit(() =\u003e Console.WriteLine(\"OnExit of Off\"))\n        .PermitReentry(Trigger.TurnOn)\n        .Permit(Trigger.Ring, State.Ringing,\n                () =\u003e { Console.WriteLine(\"Attempting to ring\"); })\n        .Permit(Trigger.Connect, State.Connected,\n                () =\u003e { Console.WriteLine(\"Connecting\"); });\n\n    var connectTriggerWithParameter =\n                config.SetTriggerParameter\u003cstring\u003e(Trigger.Connect);\n\n    config.ForState(State.Ringing)\n        .OnEntry(() =\u003e Console.WriteLine(\"OnEntry of Ringing\"))\n        .OnExit(() =\u003e Console.WriteLine(\"OnExit of Ringing\"))\n        .Permit(connectTriggerWithParameter, State.Connected,\n                name =\u003e { \n                 Console.WriteLine(\"Attempting to connect to {0}\", name);\n                })\n        .Permit(Trigger.Talk, State.Talking,\n                () =\u003e { Console.WriteLine(\"Attempting to talk\"); });\n```\n\n**Step 3:** Create the machine with the configuration:\n\n```c#\nvar machine = StateMachineFactory.Create(State.Ringing, config);\n```\n\n**Step 4:** Use them!\n\n* Using triggers:\n\n\u003e\n```c#\nmachine.Fire(Trigger.On);\n```\nor \n```\nawait machine.FireAsync(Trigger.On);\n```\n\n* Using direct states:\n\n\u003e\n```c#\nmachine.MoveToState(State.Ringing);\n``` \nor its async variant.\n\n\n* To use parameterized triggers, have a look at the example below.\n\nExamples\n---\n\nA synchronous machine example:\n\n```c#\n    var config = StateMachineFactory.CreateConfiguration\u003cState, Trigger\u003e();\n\n    config.ForState(State.Off)\n        .OnEntry(() =\u003e Console.WriteLine(\"OnEntry of Off\"))\n        .OnExit(() =\u003e Console.WriteLine(\"OnExit of Off\"))\n        .PermitReentry(Trigger.TurnOn)\n        .Permit(Trigger.Ring, State.Ringing,\n                () =\u003e { Console.WriteLine(\"Attempting to ring\"); })\n        .Permit(Trigger.Connect, State.Connected,\n                () =\u003e { Console.WriteLine(\"Connecting\"); });\n\n    var connectTriggerWithParameter =\n                config.SetTriggerParameter\u003cstring\u003e(Trigger.Connect);\n\n    config.ForState(State.Ringing)\n        .OnEntry(() =\u003e Console.WriteLine(\"OnEntry of Ringing\"))\n        .OnExit(() =\u003e Console.WriteLine(\"OnExit of Ringing\"))\n        .Permit(connectTriggerWithParameter, State.Connected,\n                name =\u003e { Console.WriteLine(\"Attempting to connect to {0}\", name); })\n        .Permit(Trigger.Talk, State.Talking,\n                () =\u003e { Console.WriteLine(\"Attempting to talk\"); });\n\n    config.ForState(State.Connected)\n        .OnEntry(() =\u003e Console.WriteLine(\"AOnEntry of Connected\"))\n        .OnExit(() =\u003e Console.WriteLine(\"AOnExit of Connected\"))\n        .PermitReentry(Trigger.Connect)\n        .Permit(Trigger.Talk, State.Talking,\n            () =\u003e { Console.WriteLine(\"Attempting to talk\"); })\n        .Permit(Trigger.TurnOn, State.Off,\n            () =\u003e { Console.WriteLine(\"Turning off\"); });\n\n\n    config.ForState(State.Talking)\n        .OnEntry(() =\u003e Console.WriteLine(\"OnEntry of Talking\"))\n        .OnExit(() =\u003e Console.WriteLine(\"OnExit of Talking\"))\n        .Permit(Trigger.TurnOn, State.Off,\n            () =\u003e { Console.WriteLine(\"Turning off\"); })\n        .Permit(Trigger.Ring, State.Ringing,\n            () =\u003e { Console.WriteLine(\"Attempting to ring\"); });\n\n    var machine = StateMachineFactory.Create(State.Ringing, config);\n\n    machine.Fire(Trigger.Talk);\n    machine.Fire(Trigger.Ring);\n    machine.Fire(connectTriggerWithParameter, \"John Doe\");\n```\n\nNow, let's take the same dumb, and terrible example, but now do it **asynchronously**!\n(Mix and match synchronous code when you don't need asynchrony to avoid the costs.)\n\n```c#\n    // Note the \"CreateAwaitableConfiguration\"\n    var config = StateMachineFactory.CreateAwaitableConfiguration\u003cState, Trigger\u003e();\n\n    config.ForState(State.Off)\n        .OnEntry(async () =\u003e Console.WriteLine(\"OnEntry of Off\"))\n        .OnExit(async () =\u003e Console.WriteLine(\"OnExit of Off\"))\n        .PermitReentry(Trigger.TurnOn)\n        .Permit(Trigger.Ring, State.Ringing,\n            async () =\u003e { Console.WriteLine(\"Attempting to ring\"); })\n        .Permit(Trigger.Connect, State.Connected,\n            async () =\u003e { Console.WriteLine(\"Connecting\"); });\n\n    var connectTriggerWithParameter =\n                config.SetTriggerParameter\u003cstring\u003e(Trigger.Connect);\n\n    config.ForState(State.Ringing)\n        .OnEntry(() =\u003e Console.WriteLine(\"OnEntry of Ringing\"))\n        .OnExit(() =\u003e Console.WriteLine(\"OnExit of Ringing\"))\n        .Permit(connectTriggerWithParameter, State.Connected,\n                name =\u003e { Console.WriteLine(\"Attempting to connect to {0}\", name); })\n        .Permit(Trigger.Talk, State.Talking,\n                () =\u003e { Console.WriteLine(\"Attempting to talk\"); });\n\n    config.ForState(State.Connected)\n        .OnEntry(async () =\u003e Console.WriteLine(\"AOnEntry of Connected\"))\n        .OnExit(async () =\u003e Console.WriteLine(\"AOnExit of Connected\"))\n        .PermitReentry(Trigger.Connect)\n        .Permit(Trigger.Talk, State.Talking,\n            async () =\u003e { Console.WriteLine(\"Attempting to talk\"); })\n        .Permit(Trigger.TurnOn, State.Off,\n            async () =\u003e { Console.WriteLine(\"Turning off\"); });\n\n    config.ForState(State.Talking)\n        .OnEntry(() =\u003e Console.WriteLine(\"OnEntry of Talking\"))\n        .OnExit(() =\u003e Console.WriteLine(\"OnExit of Talking\"))\n        .Permit(Trigger.TurnOn, State.Off,\n            () =\u003e { Console.WriteLine(\"Turning off\"); })\n        .Permit(Trigger.Ring, State.Ringing,\n            () =\u003e { Console.WriteLine(\"Attempting to ring\"); });\n\n    var machine = StateMachineFactory.Create(State.Ringing, config);\n\n    await machine.FireAsync(Trigger.Talk);\n    await machine.FireAsync(Trigger.Ring);\n    await machine.FireAsync(connectTriggerWithParameter, \"John Doe\");\n```\n\nCore APIs\n---\n\n**IStateMachineCore:**\n\n```c#\npublic interface IStateMachineCore\u003cTState, TTrigger\u003e\n{\n    TState CurrentState { get; }\n    bool IsEnabled { get; }\n    void Pause();\n    void Resume();\n\n    event Action\u003cTriggerStateEventArgs\u003cTState, TTrigger\u003e\u003e UnhandledTrigger;\n    event Action\u003cTransitionEventArgs\u003cTState, TTrigger\u003e\u003e InvalidState;\n    event Action\u003cTransitionEventArgs\u003cTState, TTrigger\u003e\u003e TransitionStarted;\n    event Action\u003cTransitionExecutedEventArgs\u003cTState, TTrigger\u003e\u003e\n                                                       TransitionExecuted;\n}\n```\n\n**Synchronous:**\n\n```c#\npublic interface IStateMachine\u003cTState, TTrigger\u003e \n        : IStateMachineCore\u003cTState, TTrigger\u003e\n{\n    IStateMachineDiagnostics\u003cTState, TTrigger\u003e Diagnostics { get; }\n\n    void Fire\u003cTArgument\u003e(\n            ParameterizedTrigger\u003cTTrigger, TArgument\u003e parameterizedTrigger, \n            TArgument argument);\n    void Fire(TTrigger trigger);\n    void MoveToState(TState state, \n            StateTransitionOption option = StateTransitionOption.Default);\n}\n```\n\n**Awaitable:**\n\n```c#\npublic interface IAwaitableStateMachine\u003cTState, TTrigger\u003e \n        : IStateMachineCore\u003cTState, TTrigger\u003e\n{\n    IAwaitableStateMachineDiagnostics\u003cTState, TTrigger\u003e Diagnostics { get; }\n\n    Task FireAsync\u003cTArgument\u003e(\n            ParameterizedTrigger\u003cTTrigger, TArgument\u003e parameterizedTrigger,\n            TArgument argument);\n    Task FireAsync(TTrigger trigger);\n    Task MoveToStateAsync(TState state, \n            StateTransitionOption option = StateTransitionOption.Default);\n}\n```\n\nIn-built Machines\n---\n\n* **Common Roots:**\n    - **AbstractStateMachineCore** - All machines derive from this. This mostly just provide the common boiler plates.\n\n* **Synchronous:**\n    - **RawStateMachine** - Direct, fully functional raw state machine. No protective abstractions or overheads. Typically only used as a base for other machines.\n    - **GuardedStateMachine** - Lock-free protection over raw state machine. Minimal statemachine for independent usage.\n    - **BlockingStateMachine** - Synchronized using Monitor and blocks until all of the requests are completed one by one. Order is not guaranteed, when parallel triggers are fired due to the nature of locks.\n\n* **Awaitable:**\n    - **RawAwaitableStateMachine** - Direct, fully functional raw state machine. No protective abstractions or overheads. Typically only used as a base for other machines.\n    - **GuardedAwaitableStateMachine** - Lock-free protection over raw state machine. Minimal statemachine for independent usage.\n    - **ScheduledAwaitableStateMachine** - Schedules the machine implementation to an external TaskScheduler. Thread-safety, order, and synchronization are the responsibility of the scheduler.\n    - **QueuedAwaitableStateMachine** - A lock-free implementation of a fully asynchronous queued statemachine. Order is guaranteed.\n\nNotes:\n\n- Awaitable state machines are a superset of asynchronous machines. All async machines are awaitable, but the opposite `may or may not` be true.\n- Most of the above machines come with both their own sealed classes as well as `Base` classes, for extending them.\n\nDynamic Triggers\n---\n\nA simple implementation of the dynamic trigger is a part of the sample.\nFor more information or if you want to understand in detail the choices leading up to the design, please have a look at:\nhttps://github.com/prasannavl/LiquidState/pull/20, and https://github.com/prasannavl/LiquidState/pull/7\n\nSupport\n----\n\nPlease use the GitHub issue tracker [here](https://github.com/prasannavl/LiquidState/issues) if you'd like to report problems or discuss features. As always, do a preliminary search in the issue tracker before opening new ones - (*Tip:* include pull requests, closed, and open issues: *[Exhaustive search](https://github.com/prasannavl/LiquidState/issues?q=)* ).\n\n\nCredits\n---\nThanks to [JetBrains](https://www.jetbrains.com) for the OSS license of Resharper Ultimate.\n\nProudly developed using:\n\n\u003ca href=\"https://www.jetbrains.com/resharper/\n\"\u003e\u003cimg src=\"https://blog.jetbrains.com/wp-content/uploads/2014/04/logo_resharper.gif\" alt=\"Resharper logo\" width=\"100\" /\u003e\u003c/a\u003e\n\n\nLicense\n---\n\nThis project is licensed under either of the following, at your choice:\n\n* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or [https://www.apache.org/licenses/LICENSE-2.0](https://www.apache.org/licenses/LICENSE-2.0))\n* GPL 3.0 license ([LICENSE-GPL](LICENSE-GPL) or [https://opensource.org/licenses/GPL-3.0](https://opensource.org/licenses/GPL-3.0))\n\n\nCode of Conduct\n---\n\nContribution to the LiquidState project is organized under the terms of the Contributor Covenant, and as such the maintainer [@prasannavl](https://github.com/prasannavl) promises to intervene to uphold that code of conduct.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprasannavl%2FLiquidState","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprasannavl%2FLiquidState","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprasannavl%2FLiquidState/lists"}