Skip to content

Commit 5e4546c

Browse files
tomerdyim-lee
andcommitted
fixup
Co-authored-by: Yim Lee <[email protected]>
1 parent f389916 commit 5e4546c

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,32 @@ In more complex cases, when `Signal`-trapping-based shutdown is not appropriate,
214214

215215
`shutdown` is an asynchronous operation. Errors will be logged and bubbled up to the provided completion handler.
216216

217+
### Stateful handlers
218+
219+
In some cases it is useful to have the Start handlers return a state that can be passed on to the Shutdown handlers for shutdown.
220+
For example, when establishing some sort of a connection that needs to be closed at shutdown.
221+
222+
```swift
223+
struct Foo {
224+
func start() throws -> Connection {
225+
return ...
226+
}
227+
228+
func shutdown(state: Connection) throws {
229+
...
230+
}
231+
}
232+
```
233+
234+
```swift
235+
let foo = ...
236+
lifecycle.registerStateful(
237+
label: "foo",
238+
start: .sync(foo.start),
239+
shutdown: .sync(foo.shutdown)
240+
)
241+
```
242+
217243
### Complex Systems and Nesting of Subsystems
218244

219245
In larger Applications (Services) `ComponentLifecycle` can be used to manage the lifecycle of subsystems, such that `ServiceLifecycle` can start and shutdown `ComponentLifecycle`s.

Sources/Lifecycle/Lifecycle.swift

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,24 @@ extension LifecycleTask {
4141

4242
/// Supported startup and shutdown method styles
4343
public struct LifecycleHandler {
44-
@available(*, deprecated, renamed: "Handler")
44+
@available(*, deprecated)
4545
public typealias Callback = (@escaping (Error?) -> Void) -> Void
4646

47-
public typealias Handler = (@escaping (Error?) -> Void) -> Void
48-
49-
private let underlying: Handler?
47+
private let underlying: ((@escaping (Error?) -> Void) -> Void)?
5048

5149
/// Initialize a `LifecycleHandler` based on a completion handler.
5250
///
5351
/// - parameters:
5452
/// - handler: the underlying completion handler
55-
public init(_ handler: Handler?) {
53+
public init(_ handler: ((@escaping (Error?) -> Void) -> Void)?) {
5654
self.underlying = handler
5755
}
5856

5957
/// Asynchronous `LifecycleHandler` based on a completion handler.
6058
///
6159
/// - parameters:
6260
/// - handler: the underlying async handler
63-
public static func async(_ handler: @escaping Handler) -> LifecycleHandler {
61+
public static func async(_ handler: @escaping (@escaping (Error?) -> Void) -> Void) -> LifecycleHandler {
6462
return LifecycleHandler(handler)
6563
}
6664

@@ -100,27 +98,25 @@ public struct LifecycleHandler {
10098

10199
/// LifecycleHandler for starting stateful tasks. The state can then be fed into a LifecycleShutdownHandler
102100
public struct LifecycleStartHandler<State> {
103-
public typealias Handler = (@escaping (Result<State, Error>) -> Void) -> Void
104-
105-
private let underlying: Handler
101+
private let underlying: (@escaping (Result<State, Error>) -> Void) -> Void
106102

107103
/// Initialize a `LifecycleHandler` based on a completion handler.
108104
///
109105
/// - parameters:
110106
/// - callback: the underlying completion handler
111-
public init(_ handler: @escaping Handler) {
107+
public init(_ handler: @escaping (@escaping (Result<State, Error>) -> Void) -> Void) {
112108
self.underlying = handler
113109
}
114110

115111
/// Asynchronous `LifecycleStartHandler` based on a completion handler.
116112
///
117113
/// - parameters:
118114
/// - handler: the underlying async handler
119-
public static func async(_ handler: @escaping Handler) -> LifecycleStartHandler {
115+
public static func async(_ handler: @escaping (@escaping (Result<State, Error>) -> Void) -> Void) -> LifecycleStartHandler {
120116
return LifecycleStartHandler(handler)
121117
}
122118

123-
/// Asynchronous `LifecycleStartHandler` based on a blocking, throwing function.
119+
/// Synchronous `LifecycleStartHandler` based on a blocking, throwing function.
124120
///
125121
/// - parameters:
126122
/// - body: the underlying function
@@ -142,23 +138,21 @@ public struct LifecycleStartHandler<State> {
142138

143139
/// LifecycleHandler for shutting down stateful tasks. The state comes from a LifecycleStartHandler
144140
public struct LifecycleShutdownHandler<State> {
145-
public typealias Handler = (State, @escaping (Error?) -> Void) -> Void
146-
147-
private let underlying: Handler
141+
private let underlying: (State, @escaping (Error?) -> Void) -> Void
148142

149143
/// Initialize a `LifecycleShutdownHandler` based on a completion handler.
150144
///
151145
/// - parameters:
152146
/// - handler: the underlying completion handler
153-
public init(_ handler: @escaping Handler) {
147+
public init(_ handler: @escaping (State, @escaping (Error?) -> Void) -> Void) {
154148
self.underlying = handler
155149
}
156150

157151
/// Asynchronous `LifecycleShutdownHandler` based on a completion handler.
158152
///
159153
/// - parameters:
160154
/// - handler: the underlying async handler
161-
public static func async(_ handler: @escaping Handler) -> LifecycleShutdownHandler {
155+
public static func async(_ handler: @escaping (State, @escaping (Error?) -> Void) -> Void) -> LifecycleShutdownHandler {
162156
return LifecycleShutdownHandler(handler)
163157
}
164158

@@ -605,7 +599,7 @@ extension LifecycleTasksContainer {
605599
self.register(label: label, start: .none, shutdown: handler)
606600
}
607601

608-
/// Adds a stateful `LifecycleTask` to a `LifecycleTasks` collection.
602+
/// Add a stateful `LifecycleTask` to a `LifecycleTasks` collection.
609603
///
610604
/// - parameters:
611605
/// - label: label of the item, useful for debugging.
@@ -616,6 +610,7 @@ extension LifecycleTasksContainer {
616610
}
617611
}
618612

613+
// internal for testing
619614
internal struct _LifecycleTask: LifecycleTask {
620615
let label: String
621616
let shutdownIfNotStarted: Bool
@@ -638,6 +633,7 @@ internal struct _LifecycleTask: LifecycleTask {
638633
}
639634
}
640635

636+
// internal for testing
641637
internal class StatefulLifecycleTask<State>: LifecycleTask {
642638
let label: String
643639
let shutdownIfNotStarted: Bool = false

0 commit comments

Comments
 (0)