{"id":51918418,"url":"https://github.com/orchetect/swift-state-machines","last_synced_at":"2026-07-27T14:30:35.987Z","repository":{"id":369817315,"uuid":"1291515588","full_name":"orchetect/swift-state-machines","owner":"orchetect","description":"General-purpose state machine types for Swift.","archived":false,"fork":false,"pushed_at":"2026-07-14T20:59:26.000Z","size":163,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-07-14T22:28:03.403Z","etag":null,"topics":["ios","macos","state-machine","state-machines","statemachine","statemachines","swift","tvos","visionos","watchos"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/orchetect.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"orchetect"}},"created_at":"2026-07-06T21:49:37.000Z","updated_at":"2026-07-14T20:59:04.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/orchetect/swift-state-machines","commit_stats":null,"previous_names":["orchetect/swift-state-machines"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/orchetect/swift-state-machines","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orchetect%2Fswift-state-machines","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orchetect%2Fswift-state-machines/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orchetect%2Fswift-state-machines/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orchetect%2Fswift-state-machines/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/orchetect","download_url":"https://codeload.github.com/orchetect/swift-state-machines/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orchetect%2Fswift-state-machines/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35955550,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-07-20T02:08:10.276Z","status":"online","status_checked_at":"2026-07-27T02:00:06.776Z","response_time":101,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["ios","macos","state-machine","state-machines","statemachine","statemachines","swift","tvos","visionos","watchos"],"created_at":"2026-07-27T14:30:34.925Z","updated_at":"2026-07-27T14:30:35.936Z","avatar_url":"https://github.com/orchetect.png","language":"Swift","funding_links":["https://github.com/sponsors/orchetect"],"categories":[],"sub_categories":[],"readme":"# SwiftStateMachines\n\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Forchetect%2Fswift-state-machines%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/orchetect/swift-state-machines) [![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Forchetect%2Fswift-state-machines%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/orchetect/swift-state-machines) [![License: MIT](http://img.shields.io/badge/license-MIT-lightgrey.svg?style=flat)](https://github.com/orchetect/swift-state-machines/blob/main/LICENSE)\n\nGeneral-purpose state machine types for Swift.\n\nThis package aims to offer flexible, modern, thread-safe abstractions.\n\n\u003e [!NOTE]\n\u003e\n\u003e For the time being, this library is under active development and is not considered API-stable.\n\u003e\n\u003e Prior to release 1.0.0, refactors and new features will likely introduce code-breaking changes along the way.\n\n## Overview\n\nThis package provides building blocks to define your own set of states, conditional transition logic, and abstractions to serialize transition changes so that a overlapping calls to the same transition are not repeated.\n\n## Start/Stop State Machine\n\nOne of the high-level abstractions available combines all of these building blocks into a general-purpose, ready-to-use object lifecycle manager called `StartStopStateMachine`.\n\nIn its most basic form, it acts as a start/stop state machine:\n\n```swift\npublic class MyService {\n    private let lifecycle = StartStopStateMachine()\n\n    public func start() {\n        lifecycle.start {\n            // perform synchronized work to start the service\n        }\n    }\n\n    public func stop() {\n        lifecycle.stop {\n            // perform synchronized work to teardown the service\n        }\n    }\n}\n```\n\n## Managed Resources\n\nThe `StartStopStateMachine` type may be specialized to contain resources that are created and prepared during the transition to the **started** state, and torn down during the transition to the **stopped** state.\n\nThe lifecycle of the inner resources is managed by the state machine, and concurrent state transitions are serialized to retain the integrity of the state machine and its held resources.\n\n```swift\npublic class MyService {\n    private let lifecycle = StartStopStateMachine\u003cModel\u003e()\n\n    public func start() {\n        lifecycle.start {\n            let model = Model()\n            model.setup()\n            return model\n        }\n    }\n\n    public func stop() {\n        lifecycle.stop { model in\n            model.teardown()\n        }\n    }\n}\n\nextension MyService {\n    private struct Model: Sendable {\n        var value: Int = 0\n        init() { }\n\n        func setup() { }\n        func teardown() { }\n        mutating func increment() { value += 1 }\n    }\n}\n```\n\nThere are various ways to check the current state and interact with managed resources. These are a few basic examples:\n\n```swift\nextension MyService {\n    public func someActionThatRequiresStartedState() throws {\n        guard lifecycle.assertState(is: .started) else { throw SomeError() }\n        // asserts state is started, but we don't need access to the model instance\n    }\n\n    public func readModel() throws {\n        guard let model = lifecycle.startedResources else { throw SomeError() }\n        // the started resources are returned as an immutable copy\n        print(model.value)\n    }\n\n    public func mutateModel() throws {\n        try lifecycle.withStartedResources { model in\n            // the started resources are available as mutable `inout` within scope\n            model.increment()\n        } wrongState: { throw SomeError() }\n    }\n}\n```\n\nThe library includes many more methods and types to provide maximum flexibility to fit your implementation requirements.\n\n## Getting Started\n\nThis library is available as a Swift Package Manager (SPM) package.\n\n1. Add the **swift-state-machines** repo as a dependency.\n\n   ```swift\n   .package(url: \"https://github.com/orchetect/swift-state-machines\", from: \"0.3.0\")\n   ```\n\n2. Add **SwiftStateMachines** to your target.\n\n   ```swift\n   .product(name: \"SwiftStateMachines\", package: \"swift-state-machines\")\n   ```\n\n3. Import **SwiftStateMachines** to use it.\n\n   ```swift\n   import SwiftStateMachines\n   ```\n\n## Documentation\n\nFormal documentation is currently a WIP. Complete docs coverage and additional example code is planned for a future releasee.\n\n## Author\n\nCoded by a bunch of 🐹 hamsters in a trenchcoat that calls itself [@orchetect](https://github.com/orchetect).\n\n## License\n\nLicensed under the MIT license. See [LICENSE](https://github.com/orchetect/swift-state-machines/blob/master/LICENSE) for details.\n\n## Community \u0026 Support\n\nPlease do not email maintainers for technical support. Several options are available for issues and questions:\n\n- Questions and feature ideas can be posted to [Discussions](https://github.com/orchetect/swift-state-machines/discussions).\n- If an issue is a verifiable bug with reproducible steps it may be posted in [Issues](https://github.com/orchetect/swift-state-machines/issues).\n\n## Contributions\n\nContributions are welcome. Posting in [Discussions](https://github.com/orchetect/swift-state-machines/discussions) first prior to new submitting PRs for features or modifications is encouraged.\n\n## Code Quality \u0026 AI Contribution Policy\n\nIn an effort to maintain a consistent level of code quality and safety, this repository was built by hand and is maintained without the use of AI code generation.\n\nAI-assisted contributions are welcome, but must remain modest in scope, maintain the same degree of quality and care, and be thoroughly vetted before acceptance.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Forchetect%2Fswift-state-machines","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Forchetect%2Fswift-state-machines","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Forchetect%2Fswift-state-machines/lists"}