You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
motivation: allow lifecycle handlers to have the library manage the state for them so they do not need to do that manually
changes:
* introduce LifecycleStartHandler and LifecycleShutdownHandler which can handle state on behalf of the lifecycle item
* add registerStateful function to regsiter stateful handlers
* add tests
Copy file name to clipboardExpand all lines: README.md
+26Lines changed: 26 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -214,6 +214,32 @@ In more complex cases, when `Signal`-trapping-based shutdown is not appropriate,
214
214
215
215
`shutdown` is an asynchronous operation. Errors will be logged and bubbled up to the provided completion handler.
216
216
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
+
structFoo {
224
+
funcstart() throws-> Connection {
225
+
return...
226
+
}
227
+
228
+
funcshutdown(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
+
217
243
### Complex Systems and Nesting of Subsystems
218
244
219
245
In larger Applications (Services) `ComponentLifecycle` can be used to manage the lifecycle of subsystems, such that `ServiceLifecycle` can start and shutdown `ComponentLifecycle`s.
0 commit comments