Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Demo/Sources/Demo/StateMachineExamples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class StateMachineExamples {
try await stateMachine.process(event: .start)
try await stateMachine.process(event: .stop)
try await stateMachine.process(event: .start)
try await stateMachine.process(event: .execute)
try await stateMachine.process(event: .execute) {
print("Execution block called.")
}
}
}
8 changes: 5 additions & 3 deletions Framework/Sources/StateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public actor StateMachine<State: Hashable & Sendable, Event: Hashable & Sendable
}
}

public func process(event: Event) throws {
public func process(event: Event, execution: ExecutionBlock? = nil) async throws {
let transitions = transitionsByEvent[event]
let performableTransitions = transitions?.filter { $0.source == currentState } ?? []

Expand All @@ -41,15 +41,17 @@ public actor StateMachine<State: Hashable & Sendable, Event: Hashable & Sendable
let transition = performableTransitions.first!

log(message: "Processing event '\(event)' from '\(currentState)'")
transition.executePreBlock()
try await transition.executePreBlock()

log(message: "Processed pre condition for event '\(event)' from '\(transition.source)' to '\(transition.destination)'")

try await execution?()

let previousState = currentState
currentState = transition.destination

log(message: "Processed state change from '\(previousState)' to '\(transition.destination)'")
transition.executePostBlock()
try await transition.executePostBlock()

log(message: "Processed post condition for event '\(event)' from '\(transition.source)' to '\(transition.destination)'")
}
Expand Down
10 changes: 5 additions & 5 deletions Framework/Sources/Transition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import Foundation

public typealias ExecutionBlock = @Sendable () -> Void
public typealias ExecutionBlock = @Sendable () async throws -> Void

public struct Transition<State: Sendable, Event: Sendable>: Sendable {

Expand All @@ -24,11 +24,11 @@ public struct Transition<State: Sendable, Event: Sendable>: Sendable {
self.postBlock = postBlock
}

func executePreBlock() {
preBlock?()
func executePreBlock() async throws {
try await preBlock?()
}

func executePostBlock() {
postBlock?()
func executePostBlock() async throws {
try await postBlock?()
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ do {
} catch {
print("Event 'pause' cannot currently be processed.")
}

try await stateMachine.process(event: .start) {
print("Execution block called.")
}
```

### Logging
Expand Down
10 changes: 5 additions & 5 deletions Tests/Sources/TransitionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ struct TransitionTests {
#expect(transition.destination == to)
}

@Test func callback() async {
await confirmation("preBlock called") { preConfirmation in
await confirmation("postBlock called") { postConfirmation in
@Test func callback() async throws {
try await confirmation("preBlock called") { preConfirmation in
try await confirmation("postBlock called") { postConfirmation in
let transition = Transition(with: "event", from: "from", to: "to", preBlock: {
preConfirmation()
}, postBlock: {
postConfirmation()
})
transition.executePreBlock()
transition.executePostBlock()
try await transition.executePreBlock()
try await transition.executePostBlock()
}
}
}
Expand Down
Loading