-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPocketFlow.swift
More file actions
166 lines (131 loc) · 4.37 KB
/
PocketFlow.swift
File metadata and controls
166 lines (131 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import Foundation
// MARK: - Action Alias
public typealias Action = String
// MARK: - Protocol
public protocol FlowNode: AnyObject {
associatedtype Shared
associatedtype Prep: Sendable
associatedtype Exec: Sendable
associatedtype Act: Hashable & Sendable = Action
func prep(shared: inout Shared) async throws -> Prep
func exec(prep: Prep) async throws -> Exec
func post(shared: inout Shared, prep: Prep, exec: Exec) async throws -> Act?
}
extension FlowNode {
public func _run(shared: inout Shared) async throws -> Act? {
let p = try await prep(shared: &shared)
let e = try await exec(prep: p)
return try await post(shared: &shared, prep: p, exec: e)
}
}
// MARK: - Base Node
open class BaseNode<PrepRes: Sendable, ExecRes: Sendable>: FlowNode {
public typealias Shared = [String: Sendable]
public typealias Prep = PrepRes
public typealias Exec = ExecRes
public typealias Act = Action
public var maxRetries: Int = 1
public var waitInSeconds: UInt64 = 0
public var successors: [Action: any AnyNode] = [:]
public var params: [String: Sendable] = [:]
public init() {}
open func prep(shared: inout Shared) async throws -> PrepRes {
fatalError("Must override prep")
}
open func exec(prep: PrepRes) async throws -> ExecRes {
fatalError("Must override exec")
}
open func post(shared: inout Shared, prep: PrepRes, exec: ExecRes) async throws -> Action? {
fatalError("Must override post")
}
open func execFallback(prep: PrepRes, error: any Error) async throws -> ExecRes {
throw error
}
public func _exec(prep: PrepRes) async throws -> ExecRes {
for i in 0..<maxRetries {
do {
return try await exec(prep: prep)
} catch {
if i == (maxRetries - 1) {
return try await execFallback(prep: prep, error: error)
}
if waitInSeconds > 0 {
try await Task.sleep(nanoseconds: UInt64(waitInSeconds) * 1_000_000_000)
}
}
}
throw NSError(domain: "RetryFailed", code: -1)
}
public func setNext(_ action: Action = "default", _ node: some AnyNode) {
successors[action] = node
}
}
// MARK: - Type Erasure
public protocol AnyNode: AnyObject {
var params: [String: Sendable] { get set }
var successors: [Action: any AnyNode] { get set }
func _run(shared: inout [String: Sendable]) async throws -> Action?
}
extension BaseNode: AnyNode {
public func _run(shared: inout [String: Sendable]) async throws -> Action? {
let prep = try await prep(shared: &shared)
let exec = try await _exec(prep: prep)
return try await post(shared: &shared, prep: prep, exec: exec)
}
}
// MARK: - Conditional Transition
public struct _ConditionalTransition<P: Sendable, E: Sendable> {
let node: BaseNode<P, E>
let action: Action
public init(node: BaseNode<P, E>, action: Action) {
self.node = node
self.action = action
}
}
// MARK: - Flow
public final class Flow {
public var startNode: any AnyNode
public init(start: any AnyNode) {
self.startNode = start
}
public func run(shared: inout [String: Sendable]) async throws -> Action? {
var current: AnyNode? = startNode
var lastAction: Action? = nil
while let node = current {
lastAction = try await node._run(shared: &shared)
current = node.successors[lastAction ?? "default"]
}
return lastAction
}
}
// MARK: - Operators
precedencegroup FlowConnectPrecedence {
associativity: left
}
precedencegroup FlowTransitionPrecedence {
associativity: left
higherThan: FlowConnectPrecedence
}
infix operator >>> : FlowConnectPrecedence
infix operator <> : FlowTransitionPrecedence
@discardableResult
public func >>> <P: Sendable, E: Sendable, N: AnyNode>(
lhs: BaseNode<P, E>,
rhs: N
) -> N {
lhs.setNext("default", rhs)
return rhs
}
@discardableResult
public func >>> <P: Sendable, E: Sendable, N: AnyNode>(
lhs: _ConditionalTransition<P, E>,
rhs: N
) -> N {
lhs.node.setNext(lhs.action, rhs)
return rhs
}
public func <> <P: Sendable, E: Sendable>(lhs: BaseNode<P, E>, action: Action)
-> _ConditionalTransition<P, E>
{
_ConditionalTransition(node: lhs, action: action)
}