@@ -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
234203extension 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+ }
0 commit comments