Skip to content

Commit b5ead64

Browse files
authored
remove closure variants to reduce API confusion (#46)
motivation: API now includes closure based variants, saving the need to wrap them in .sync Users have complained that these overloads add confusion, and probably not worth the additional API surface area changes: * remove the sync closure API overloads * update readme, examples and tests
1 parent e1ec2b7 commit b5ead64

File tree

3 files changed

+31
-64
lines changed

3 files changed

+31
-64
lines changed

README.md

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ let lifecycle = ServiceLifecycle()
4242
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
4343
lifecycle.registerShutdown(
4444
label: "eventLoopGroup",
45-
eventLoopGroup.syncShutdownGracefully
45+
.sync(eventLoopGroup.syncShutdownGracefully)
4646
)
4747

4848
// register another resource that should be started when the application starts
@@ -93,27 +93,24 @@ In larger Applications (Services) `ComponentLifecycle` can be used to manage the
9393

9494
### Registering items
9595

96-
`ServiceLifecycle` and `ComponentLifecycle` are containers for `LifecycleTask`s which need to be registered via one of the following variants:
96+
`ServiceLifecycle` and `ComponentLifecycle` are containers for `LifecycleTask`s which need to be registered using a `LifecycleHandler` - a container for synchronous or asynchronous closures.
9797

98-
You can register simple blocking throwing handlers using:
98+
Synchronous handlers are defined as `() throws -> Void`.
9999

100-
```swift
101-
func register(label: String, start: @escaping () throws -> Void, shutdown: @escaping () throws -> Void)
102-
103-
func registerShutdown(label: String, _ handler: @escaping () throws -> Void)
104-
```
100+
Asynchronous handlers defined are as `(@escaping (Error?) -> Void) -> Void`.
105101

106-
or, you can register asynchronous and more complex handlers using:
102+
`LifecycleHandler` comes with static helpers named `async` and `sync` designed to help simplify the registration call to:
107103

108104
```swift
109-
func register(label: String, start: Handler, shutdown: Handler)
110-
111-
func registerShutdown(label: String, _ handler: Handler)
105+
let foo = ...
106+
lifecycle.register(
107+
label: "foo",
108+
start: .sync(foo.syncStart),
109+
shutdown: .sync(foo.syncShutdown)
110+
)
112111
```
113112

114-
where `LifecycleHandler` is a container for an asynchronous closure defined as `(@escaping (Error?) -> Void) -> Void`
115-
116-
`LifecycleHandler` comes with static helpers named `async` and `sync` designed to help simplify the registration call to:
113+
Or the async version:
117114

118115
```swift
119116
let foo = ...
@@ -130,10 +127,18 @@ or, just shutdown:
130127
let foo = ...
131128
lifecycle.registerShutdown(
132129
label: "foo",
133-
.async(foo.asyncShutdown)
130+
.sync(foo.syncShutdown)
134131
)
135132
```
133+
Or the async version:
136134

135+
```swift
136+
let foo = ...
137+
lifecycle.registerShutdown(
138+
label: "foo",
139+
.async(foo.asyncShutdown)
140+
)
141+
```
137142

138143
you can also register a collection of `LifecycleTask`s (less typical) using:
139144

Sources/Lifecycle/Lifecycle.swift

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -218,25 +218,6 @@ public extension ServiceLifecycle {
218218
func registerShutdown(label: String, _ handler: LifecycleHandler) {
219219
self.lifecycle.registerShutdown(label: label, handler)
220220
}
221-
222-
/// Adds a `Task` to a `Tasks` collection.
223-
///
224-
/// - parameters:
225-
/// - label: label of the item, useful for debugging.
226-
/// - start: closure to perform the shutdown.
227-
/// - shutdown: closure to perform the shutdown.
228-
func register(label: String, start: @escaping () throws -> Void, shutdown: @escaping () throws -> Void) {
229-
self.lifecycle.register(label: label, start: start, shutdown: shutdown)
230-
}
231-
232-
/// Adds a `Task` to a `Tasks` collection.
233-
///
234-
/// - parameters:
235-
/// - label: label of the item, useful for debugging.
236-
/// - handler: closure to perform the shutdown.
237-
func registerShutdown(label: String, _ handler: @escaping () throws -> Void) {
238-
self.lifecycle.registerShutdown(label: label, handler)
239-
}
240221
}
241222

242223
extension ServiceLifecycle {
@@ -538,23 +519,4 @@ public extension ComponentLifecycle {
538519
func registerShutdown(label: String, _ handler: LifecycleHandler) {
539520
self.register(label: label, start: .none, shutdown: handler)
540521
}
541-
542-
/// Adds a `Task` to a `Tasks` collection.
543-
///
544-
/// - parameters:
545-
/// - label: label of the item, useful for debugging.
546-
/// - start: closure to perform the shutdown.
547-
/// - shutdown: closure to perform the shutdown.
548-
func register(label: String, start: @escaping () throws -> Void, shutdown: @escaping () throws -> Void) {
549-
self.register(label: label, start: .sync(start), shutdown: .sync(shutdown))
550-
}
551-
552-
/// Adds a `Task` to a `Tasks` collection.
553-
///
554-
/// - parameters:
555-
/// - label: label of the item, useful for debugging.
556-
/// - handler: closure to perform the shutdown.
557-
func registerShutdown(label: String, _ handler: @escaping () throws -> Void) {
558-
self.register(label: label, start: .none, shutdown: .sync(handler))
559-
}
560522
}

Tests/LifecycleTests/ComponentLifecycleTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,22 +199,22 @@ final class ComponentLifecycleTests: XCTestCase {
199199
do {
200200
let id = UUID().uuidString
201201
lifecycle.register(label: id,
202-
start: {
202+
start: .sync {
203203
startCalls.append(id)
204204
blockStartSemaphore.wait()
205205
},
206-
shutdown: {
206+
shutdown: .sync {
207207
XCTAssertTrue(startCalls.contains(id))
208208
stopCalls.append(id)
209209
})
210210
}
211211
do {
212212
let id = UUID().uuidString
213213
lifecycle.register(label: id,
214-
start: {
214+
start: .sync {
215215
startCalls.append(id)
216216
},
217-
shutdown: {
217+
shutdown: .sync {
218218
XCTAssertTrue(startCalls.contains(id))
219219
stopCalls.append(id)
220220
})
@@ -416,7 +416,7 @@ final class ComponentLifecycleTests: XCTestCase {
416416
let lifecycle = ComponentLifecycle(label: "test")
417417
let items = (5 ... Int.random(in: 10 ... 20)).map { _ in Sync() }
418418
items.forEach { item in
419-
lifecycle.register(label: item.id, start: item.start, shutdown: item.shutdown)
419+
lifecycle.register(label: item.id, start: .sync(item.start), shutdown: .sync(item.shutdown))
420420
}
421421

422422
lifecycle.start { error in
@@ -435,8 +435,8 @@ final class ComponentLifecycleTests: XCTestCase {
435435
lifecycle.register(label: "item1", start: .eventLoopFuture(item1.start), shutdown: .eventLoopFuture(item1.shutdown))
436436

437437
lifecycle.register(label: "blocker",
438-
start: { () -> Void in try eventLoopGroup.next().makeSucceededFuture(()).wait() },
439-
shutdown: { () -> Void in try eventLoopGroup.next().makeSucceededFuture(()).wait() })
438+
start: .sync { try eventLoopGroup.next().makeSucceededFuture(()).wait() },
439+
shutdown: .sync { try eventLoopGroup.next().makeSucceededFuture(()).wait() })
440440

441441
let item2 = NIOItem(eventLoopGroup: eventLoopGroup)
442442
lifecycle.register(label: "item2", start: .eventLoopFuture(item2.start), shutdown: .eventLoopFuture(item2.shutdown))
@@ -493,8 +493,8 @@ final class ComponentLifecycleTests: XCTestCase {
493493

494494
let item = Sync()
495495
lifecycle.register(label: "test",
496-
start: item.start,
497-
shutdown: item.shutdown)
496+
start: .sync(item.start),
497+
shutdown: .sync(item.shutdown))
498498

499499
lifecycle.start { error in
500500
XCTAssertNil(error, "not expecting error")
@@ -526,7 +526,7 @@ final class ComponentLifecycleTests: XCTestCase {
526526
let lifecycle = ComponentLifecycle(label: "test")
527527

528528
let item = Sync()
529-
lifecycle.registerShutdown(label: "test", item.shutdown)
529+
lifecycle.registerShutdown(label: "test", .sync(item.shutdown))
530530

531531
lifecycle.start { error in
532532
XCTAssertNil(error, "not expecting error")

0 commit comments

Comments
 (0)