Skip to content

Commit 765e58f

Browse files
committed
abstract registration function into a protocol
motivation: allow libraries that want to expose the lifecycle so that downstream code could register additional items to do so regardless of the lifecycle backend changes: abstract registration function into a protocol and conform ComponentLifecycle and ServiceLifecycle to it
1 parent adb3939 commit 765e58f

File tree

2 files changed

+56
-81
lines changed

2 files changed

+56
-81
lines changed

Sources/Lifecycle/Lifecycle.swift

Lines changed: 36 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -194,41 +194,10 @@ extension ServiceLifecycle {
194194
}
195195
}
196196

197-
public extension ServiceLifecycle {
198-
/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
199-
///
200-
/// - parameters:
201-
/// - tasks: one or more `LifecycleTask`.
202-
func register(_ tasks: LifecycleTask ...) {
203-
self.underlying.register(tasks)
204-
}
205-
206-
/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
207-
///
208-
/// - parameters:
209-
/// - tasks: array of `LifecycleTask`.
210-
func register(_ tasks: [LifecycleTask]) {
197+
extension ServiceLifecycle: LifecycleRegistrar {
198+
public func register(_ tasks: [LifecycleTask]) {
211199
self.underlying.register(tasks)
212200
}
213-
214-
/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
215-
///
216-
/// - parameters:
217-
/// - label: label of the item, useful for debugging.
218-
/// - start: `LifecycleHandler` to perform the startup.
219-
/// - shutdown: `LifecycleHandler` to perform the shutdown.
220-
func register(label: String, start: LifecycleHandler, shutdown: LifecycleHandler) {
221-
self.underlying.register(label: label, start: start, shutdown: shutdown)
222-
}
223-
224-
/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
225-
///
226-
/// - parameters:
227-
/// - label: label of the item, useful for debugging.
228-
/// - handler: `LifecycleHandler` to perform the shutdown.
229-
func registerShutdown(label: String, _ handler: LifecycleHandler) {
230-
self.underlying.registerShutdown(label: label, handler)
231-
}
232201
}
233202

234203
extension ServiceLifecycle {
@@ -480,42 +449,34 @@ public class ComponentLifecycle: LifecycleTask {
480449
}
481450
}
482451

483-
public extension ComponentLifecycle {
484-
internal struct Task: LifecycleTask {
485-
let label: String
486-
let start: LifecycleHandler
487-
let shutdown: LifecycleHandler
488-
489-
func start(_ callback: @escaping (Error?) -> Void) {
490-
self.start.run(callback)
452+
extension ComponentLifecycle: LifecycleRegistrar {
453+
public func register(_ tasks: [LifecycleTask]) {
454+
self.stateLock.withLock {
455+
guard case .idle = self.state else {
456+
preconditionFailure("invalid state, \(self.state)")
457+
}
491458
}
492-
493-
func shutdown(_ callback: @escaping (Error?) -> Void) {
494-
self.shutdown.run(callback)
459+
self.tasksLock.withLock {
460+
self.tasks.append(contentsOf: tasks)
495461
}
496462
}
463+
}
497464

465+
public protocol LifecycleRegistrar {
498466
/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
499467
///
500468
/// - parameters:
501-
/// - tasks: one or more `LifecycleTask`.
502-
func register(_ tasks: LifecycleTask ...) {
503-
self.register(tasks)
504-
}
469+
/// - tasks: array of `LifecycleTask`.
470+
func register(_ tasks: [LifecycleTask])
471+
}
505472

473+
extension LifecycleRegistrar {
506474
/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
507475
///
508476
/// - parameters:
509-
/// - tasks: array of `LifecycleTask`.
510-
func register(_ tasks: [LifecycleTask]) {
511-
self.stateLock.withLock {
512-
guard case .idle = self.state else {
513-
preconditionFailure("invalid state, \(self.state)")
514-
}
515-
}
516-
self.tasksLock.withLock {
517-
self.tasks.append(contentsOf: tasks)
518-
}
477+
/// - tasks: one or more `LifecycleTask`.
478+
public func register(_ tasks: LifecycleTask ...) {
479+
self.register(tasks)
519480
}
520481

521482
/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
@@ -524,16 +485,30 @@ public extension ComponentLifecycle {
524485
/// - label: label of the item, useful for debugging.
525486
/// - start: `Handler` to perform the startup.
526487
/// - shutdown: `Handler` to perform the shutdown.
527-
func register(label: String, start: LifecycleHandler, shutdown: LifecycleHandler) {
528-
self.register(Task(label: label, start: start, shutdown: shutdown))
488+
public func register(label: String, start: LifecycleHandler, shutdown: LifecycleHandler) {
489+
self.register(LifecycleTaskImpl(label: label, start: start, shutdown: shutdown))
529490
}
530491

531492
/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
532493
///
533494
/// - parameters:
534495
/// - label: label of the item, useful for debugging.
535496
/// - handler: `Handler` to perform the shutdown.
536-
func registerShutdown(label: String, _ handler: LifecycleHandler) {
497+
public func registerShutdown(label: String, _ handler: LifecycleHandler) {
537498
self.register(label: label, start: .none, shutdown: handler)
538499
}
539500
}
501+
502+
internal struct LifecycleTaskImpl: LifecycleTask {
503+
let label: String
504+
let start: LifecycleHandler
505+
let shutdown: LifecycleHandler
506+
507+
func start(_ callback: @escaping (Error?) -> Void) {
508+
self.start.run(callback)
509+
}
510+
511+
func shutdown(_ callback: @escaping (Error?) -> Void) {
512+
self.shutdown.run(callback)
513+
}
514+
}

Tests/LifecycleTests/ComponentLifecycleTests.swift

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ final class ComponentLifecycleTests: XCTestCase {
4343

4444
let items = (1 ... Int.random(in: 10 ... 20)).map { index -> LifecycleTask in
4545
let id = "item-\(index)"
46-
return ComponentLifecycle.Task(label: id,
47-
start: .sync {
48-
dispatchPrecondition(condition: .onQueue(.global()))
49-
startCalls.append(id)
50-
},
51-
shutdown: .sync {
52-
dispatchPrecondition(condition: .onQueue(.global()))
53-
XCTAssertTrue(startCalls.contains(id))
54-
stopCalls.append(id)
55-
})
46+
return LifecycleTaskImpl(label: id,
47+
start: .sync {
48+
dispatchPrecondition(condition: .onQueue(.global()))
49+
startCalls.append(id)
50+
},
51+
shutdown: .sync {
52+
dispatchPrecondition(condition: .onQueue(.global()))
53+
XCTAssertTrue(startCalls.contains(id))
54+
stopCalls.append(id)
55+
})
5656
}
5757
lifecycle.register(items)
5858

@@ -81,16 +81,16 @@ final class ComponentLifecycleTests: XCTestCase {
8181

8282
let items = (1 ... Int.random(in: 10 ... 20)).map { index -> LifecycleTask in
8383
let id = "item-\(index)"
84-
return ComponentLifecycle.Task(label: id,
85-
start: .sync {
86-
dispatchPrecondition(condition: .onQueue(testQueue))
87-
startCalls.append(id)
88-
},
89-
shutdown: .sync {
90-
dispatchPrecondition(condition: .onQueue(testQueue))
91-
XCTAssertTrue(startCalls.contains(id))
92-
stopCalls.append(id)
93-
})
84+
return LifecycleTaskImpl(label: id,
85+
start: .sync {
86+
dispatchPrecondition(condition: .onQueue(testQueue))
87+
startCalls.append(id)
88+
},
89+
shutdown: .sync {
90+
dispatchPrecondition(condition: .onQueue(testQueue))
91+
XCTAssertTrue(startCalls.contains(id))
92+
stopCalls.append(id)
93+
})
9494
}
9595
lifecycle.register(items)
9696

0 commit comments

Comments
 (0)