{"id":21035230,"url":"https://github.com/bamlab/kstate","last_synced_at":"2025-05-15T13:33:23.082Z","repository":{"id":47778021,"uuid":"310898223","full_name":"bamlab/kstate","owner":"bamlab","description":"Kotlin MP State Machine Library","archived":false,"fork":false,"pushed_at":"2021-08-13T14:22:51.000Z","size":1138,"stargazers_count":20,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-03T10:37:28.630Z","etag":null,"topics":["kotlin","kotlin-multiplatform","state-machine"],"latest_commit_sha":null,"homepage":"https://bamlab.github.io/kstate","language":"Kotlin","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/bamlab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-11-07T17:35:36.000Z","updated_at":"2023-04-27T20:03:38.000Z","dependencies_parsed_at":"2022-08-20T02:41:30.526Z","dependency_job_id":null,"html_url":"https://github.com/bamlab/kstate","commit_stats":null,"previous_names":["tpucci/kstate"],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bamlab%2Fkstate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bamlab%2Fkstate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bamlab%2Fkstate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bamlab%2Fkstate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bamlab","download_url":"https://codeload.github.com/bamlab/kstate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254349684,"owners_count":22056395,"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":["kotlin","kotlin-multiplatform","state-machine"],"created_at":"2024-11-19T13:14:06.591Z","updated_at":"2025-05-15T13:33:18.041Z","avatar_url":"https://github.com/bamlab.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"![kstate](./kstate.svg)\n\n![JVM Continuous Integration](https://github.com/bamlab/kstate/workflows/JVM%20Continuous%20Integration/badge.svg)\n\n![JVM Continuous Deployment](https://github.com/bamlab/kstate/workflows/JVM%20Continuous%20Deployment/badge.svg)\n\n## Install\n\n![Maven Central](https://img.shields.io/maven-central/v/tech.bam.kstate/kstate-jvm)\n\n```groovy\nimplementation(\"tech.bam.kstate:kstate-core:VERSION\")\n```\n\n### Snapshot releases\n\n```groovy\nrepositories {\n    maven { url \"https://oss.sonatype.org/content/repositories/snapshots\" } // Add this line\n}\n// ...\n\nimplementation(\"tech.bam.kstate:kstate-core:VERSION-SNAPSHOT\")\n```\n\n## Usage\n\n### State ids and event declaration\n\nExtend the `StateId` and the `Event` types.\n\n```kotlin\nsealed class TrafficLightStateId : StateId {\n    object RED : TrafficLightStateId()\n    object YELLOW : TrafficLightStateId()\n    object GREEN : TrafficLightStateId()\n}\n\nsealed class TrafficLightEvent : Event {\n    object TIMER : TrafficLightEvent()\n    object SHORT_TIMER : TrafficLightEvent()\n}\n\nsealed class PedestrianLightStateId : StateId {\n    object WALK : PedestrianLightStateId()\n    object WAIT : PedestrianLightStateId()\n}\n```\n\n### Transition between states\n\n```kotlin\nval machine = createMachine {\n    initial(RED)\n    state(RED) {\n        transition(on = TIMER, target = GREEN)\n    }\n    state(GREEN) {\n        transition(on = TIMER, target = YELLOW)\n    }\n    state(YELLOW) {\n        transition(on = TIMER, target = RED)\n    }\n}\n\nmachine.send(TIMER)\n\nassertEquals(GREEN, machine.currentStateId)\n```\n\n### Transition between nested states\n\n```kotlin\nval machine = createMachine {\n    initial(RED)\n    state(RED) {\n        initial(WALK)\n        state(WALK) {\n            transition(on = SHORT_TIMER, target = WAIT)\n        }\n        state(WAIT)\n    }\n}\n\nmachine.send(SHORT_TIMER)\n\nassertEquals(listOf(RED, WAIT), machine.activeStateIds())\n```\n\n### Transition between nested state with the internal strategy\n\nWith the internal strategy, all events are passed to children **before** they are passed to the\ncompound state.\n\n```kotlin\nval machine = createMachine(strategy = KSStrategyType.Internal) {\n    initial(RED)\n    state(RED) {\n        transition(on = TIMER, target = YELLOW)\n\n        initial(WALK)\n        state(WALK) {\n            transition(on = TIMER, target = WAIT)\n        }\n        state(WAIT)\n    }\n    state(YELLOW)\n}\n\nmachine.send(TIMER)\n\nassertEquals(listOf(RED, WAIT), machine.activeStateIds())\n```\n\n### Parallel state machine\n\n```kotlin\nval machine = createMachine(type = Type.Parallel) {\n    state(TRAFFIC_LIGHT) {\n        initial(RED)\n        state(RED) {\n            transition(on = TIMER, target = GREEN)\n        }\n        state(GREEN) {\n            transition(on = TIMER, target = RED)\n        }\n    }\n    state(PEDESTRIAN_LIGHT) {\n        initial(WAIT)\n        state(WAIT) {\n            transition(on = TIMER, target = WALK)\n        }\n        state(WALK) {\n            transition(on = TIMER, target = WAIT)\n        }\n    }\n}\n\nmachine.send(TIMER)\n\nassertEquals(\n    listOf(TRAFFIC_LIGHT, GREEN, PEDESTRIAN_LIGHT, WALK),\n    machine.activeStateIds()\n)\n```\n\n### Listen for transitions\n\n```kotlin\nval machine = createMachine {\n    initial(RED)\n    state(RED)\n    state(YELLOW)\n    state(GREEN)\n}\n\nval listener = MachineTransitionListener { previousActiveStateIds, nextActiveStateIds -\u003e\n    print(\"I'm listening.\")\n}\n\nmachine.subscribe(listener)\nmachine.unsubscribe(listener)\n\n// OR\n\nmachine.onTransition { previousActiveStateIds, nextActiveStateIds -\u003e\n    print(\"I'm listening.\")\n}\n```\n\n## Developed with IntelliJ IDEA\n\n[![JetBrains](./jetbrains.svg)](https://www.jetbrains.com/?from=kstate)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbamlab%2Fkstate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbamlab%2Fkstate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbamlab%2Fkstate/lists"}