在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Tinder/StateMachine开源软件地址(OpenSource Url):https://github.com/Tinder/StateMachine开源编程语言(OpenSource Language):Kotlin 50.6%开源软件介绍(OpenSource Introduction):StateMachineA state machine library in Kotlin and Swift.
Example State DiagramThe examples below create a Kotlin UsageDefine states, events and side effects: sealed class State {
object Solid : State()
object Liquid : State()
object Gas : State()
}
sealed class Event {
object OnMelted : Event()
object OnFroze : Event()
object OnVaporized : Event()
object OnCondensed : Event()
}
sealed class SideEffect {
object LogMelted : SideEffect()
object LogFrozen : SideEffect()
object LogVaporized : SideEffect()
object LogCondensed : SideEffect()
} Initialize state machine and declare state transitions: val stateMachine = StateMachine.create<State, Event, SideEffect> {
initialState(State.Solid)
state<State.Solid> {
on<Event.OnMelted> {
transitionTo(State.Liquid, SideEffect.LogMelted)
}
}
state<State.Liquid> {
on<Event.OnFroze> {
transitionTo(State.Solid, SideEffect.LogFrozen)
}
on<Event.OnVaporized> {
transitionTo(State.Gas, SideEffect.LogVaporized)
}
}
state<State.Gas> {
on<Event.OnCondensed> {
transitionTo(State.Liquid, SideEffect.LogCondensed)
}
}
onTransition {
val validTransition = it as? StateMachine.Transition.Valid ?: return@onTransition
when (validTransition.sideEffect) {
SideEffect.LogMelted -> logger.log(ON_MELTED_MESSAGE)
SideEffect.LogFrozen -> logger.log(ON_FROZEN_MESSAGE)
SideEffect.LogVaporized -> logger.log(ON_VAPORIZED_MESSAGE)
SideEffect.LogCondensed -> logger.log(ON_CONDENSED_MESSAGE)
}
}
} Perform state transitions: assertThat(stateMachine.state).isEqualTo(Solid)
// When
val transition = stateMachine.transition(OnMelted)
// Then
assertThat(stateMachine.state).isEqualTo(Liquid)
assertThat(transition).isEqualTo(
StateMachine.Transition.Valid(Solid, OnMelted, Liquid, LogMelted)
)
then(logger).should().log(ON_MELTED_MESSAGE) Swift UsageAdopt class MyExample: StateMachineBuilder {
... state machine implementation ...
} Define states, events and side effects: enum State: StateMachineHashable {
case solid, liquid, gas
}
enum Event: StateMachineHashable {
case melt, freeze, vaporize, condense
}
enum SideEffect {
case logMelted, logFrozen, logVaporized, logCondensed
} Initialize state machine and declare state transitions: let stateMachine = StateMachine<State, Event, SideEffect> {
initialState(.solid)
state(.solid) {
on(.melt) {
transition(to: .liquid, emit: .logMelted)
}
}
state(.liquid) {
on(.freeze) {
transition(to: .solid, emit: .logFrozen)
}
on(.vaporize) {
transition(to: .gas, emit: .logVaporized)
}
}
state(.gas) {
on(.condense) {
transition(to: .liquid, emit: .logCondensed)
}
}
onTransition {
guard case let .success(transition) = $0, let sideEffect = transition.sideEffect else { return }
switch sideEffect {
case .logMelted: logger.log(Message.melted)
case .logFrozen: logger.log(Message.frozen)
case .logVaporized: logger.log(Message.vaporized)
case .logCondensed: logger.log(Message.condensed)
}
}
} Perform state transitions: expect(stateMachine.state).to(equal(.solid))
// When
let transition = try stateMachine.transition(.melt)
// Then
expect(stateMachine.state).to(equal(.liquid))
expect(transition).to(equal(
StateMachine.Transition.Valid(fromState: .solid, event: .melt, toState: .liquid, sideEffect: .logMelted))
)
expect(logger).to(log(Message.melted)) Swift Enumerations with Associated ValuesDue to Swift enumerations (as opposed to sealed classes in Kotlin),
any The easiest way to create this boilerplate is by using the Sourcery Swift code generator along with the AutoStateMachineHashable stencil template provided in this repository. Once the codegen is setup and configured, adopt ExamplesMatter State MachineTurnstile State MachineKotlin Download
Snapshots of the development version are available in Sonatype's Maven<dependency>
<groupId>com.tinder.statemachine</groupId>
<artifactId>statemachine</artifactId>
<version>0.3.0</version>
</dependency> Gradleimplementation 'com.tinder.statemachine:statemachine:0.3.0' Swift InstallationSwift Package Manager
Cocoapods
ReferencesA composable pattern for pure state machines with effects - Andy Matuschak VisualizationThanks to @nvinayshetty, you can visualize your state machines right in the IDE using the State Arts Intellij plugin. License
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论