Skip to content

abstract registration function into a protocol #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 40 additions & 62 deletions Sources/Lifecycle/Lifecycle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public struct LifecycleHandler {

// MARK: - ServiceLifecycle

/// `ServiceLifecycle` provides a basic mechanism to cleanly startup and shutdown the application, freeing resources in order before exiting.
/// By default, also install shutdown hooks based on `Signal` and backtraces.
public struct ServiceLifecycle {
private let configuration: Configuration

Expand Down Expand Up @@ -194,41 +196,10 @@ extension ServiceLifecycle {
}
}

public extension ServiceLifecycle {
/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
///
/// - parameters:
/// - tasks: one or more `LifecycleTask`.
func register(_ tasks: LifecycleTask ...) {
self.underlying.register(tasks)
}

/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
///
/// - parameters:
/// - tasks: array of `LifecycleTask`.
func register(_ tasks: [LifecycleTask]) {
extension ServiceLifecycle: LifecycleTasksContainer {
public func register(_ tasks: [LifecycleTask]) {
self.underlying.register(tasks)
}

/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
///
/// - parameters:
/// - label: label of the item, useful for debugging.
/// - start: `LifecycleHandler` to perform the startup.
/// - shutdown: `LifecycleHandler` to perform the shutdown.
func register(label: String, start: LifecycleHandler, shutdown: LifecycleHandler) {
self.underlying.register(label: label, start: start, shutdown: shutdown)
}

/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
///
/// - parameters:
/// - label: label of the item, useful for debugging.
/// - handler: `LifecycleHandler` to perform the shutdown.
func registerShutdown(label: String, _ handler: LifecycleHandler) {
self.underlying.registerShutdown(label: label, handler)
}
}

extension ServiceLifecycle {
Expand Down Expand Up @@ -265,7 +236,7 @@ struct ShutdownError: Error {

// MARK: - ComponentLifecycle

/// `Lifecycle` provides a basic mechanism to cleanly startup and shutdown the application, freeing resources in order before exiting.
/// `ComponentLifecycle` provides a basic mechanism to cleanly startup and shutdown a subsystem in a larger application, freeing resources in order before exiting.
public class ComponentLifecycle: LifecycleTask {
public let label: String
private let logger: Logger
Expand Down Expand Up @@ -480,42 +451,35 @@ public class ComponentLifecycle: LifecycleTask {
}
}

public extension ComponentLifecycle {
internal struct Task: LifecycleTask {
let label: String
let start: LifecycleHandler
let shutdown: LifecycleHandler

func start(_ callback: @escaping (Error?) -> Void) {
self.start.run(callback)
extension ComponentLifecycle: LifecycleTasksContainer {
public func register(_ tasks: [LifecycleTask]) {
self.stateLock.withLock {
guard case .idle = self.state else {
preconditionFailure("invalid state, \(self.state)")
}
}

func shutdown(_ callback: @escaping (Error?) -> Void) {
self.shutdown.run(callback)
self.tasksLock.withLock {
self.tasks.append(contentsOf: tasks)
}
}
}

/// A container of `LifecycleTask`, used to register additional `LifecycleTask`
public protocol LifecycleTasksContainer {
/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
///
/// - parameters:
/// - tasks: one or more `LifecycleTask`.
func register(_ tasks: LifecycleTask ...) {
self.register(tasks)
}
/// - tasks: array of `LifecycleTask`.
func register(_ tasks: [LifecycleTask])
}

extension LifecycleTasksContainer {
/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
///
/// - parameters:
/// - tasks: array of `LifecycleTask`.
func register(_ tasks: [LifecycleTask]) {
self.stateLock.withLock {
guard case .idle = self.state else {
preconditionFailure("invalid state, \(self.state)")
}
}
self.tasksLock.withLock {
self.tasks.append(contentsOf: tasks)
}
/// - tasks: one or more `LifecycleTask`.
public func register(_ tasks: LifecycleTask ...) {
self.register(tasks)
}

/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
Expand All @@ -524,16 +488,30 @@ public extension ComponentLifecycle {
/// - label: label of the item, useful for debugging.
/// - start: `Handler` to perform the startup.
/// - shutdown: `Handler` to perform the shutdown.
func register(label: String, start: LifecycleHandler, shutdown: LifecycleHandler) {
self.register(Task(label: label, start: start, shutdown: shutdown))
public func register(label: String, start: LifecycleHandler, shutdown: LifecycleHandler) {
self.register(_LifecycleTask(label: label, start: start, shutdown: shutdown))
}

/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
///
/// - parameters:
/// - label: label of the item, useful for debugging.
/// - handler: `Handler` to perform the shutdown.
func registerShutdown(label: String, _ handler: LifecycleHandler) {
public func registerShutdown(label: String, _ handler: LifecycleHandler) {
self.register(label: label, start: .none, shutdown: handler)
}
}

internal struct _LifecycleTask: LifecycleTask {
let label: String
let start: LifecycleHandler
let shutdown: LifecycleHandler

func start(_ callback: @escaping (Error?) -> Void) {
self.start.run(callback)
}

func shutdown(_ callback: @escaping (Error?) -> Void) {
self.shutdown.run(callback)
}
}
40 changes: 20 additions & 20 deletions Tests/LifecycleTests/ComponentLifecycleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ final class ComponentLifecycleTests: XCTestCase {

let items = (1 ... Int.random(in: 10 ... 20)).map { index -> LifecycleTask in
let id = "item-\(index)"
return ComponentLifecycle.Task(label: id,
start: .sync {
dispatchPrecondition(condition: .onQueue(.global()))
startCalls.append(id)
},
shutdown: .sync {
dispatchPrecondition(condition: .onQueue(.global()))
XCTAssertTrue(startCalls.contains(id))
stopCalls.append(id)
})
return _LifecycleTask(label: id,
start: .sync {
dispatchPrecondition(condition: .onQueue(.global()))
startCalls.append(id)
},
shutdown: .sync {
dispatchPrecondition(condition: .onQueue(.global()))
XCTAssertTrue(startCalls.contains(id))
stopCalls.append(id)
})
}
lifecycle.register(items)

Expand Down Expand Up @@ -81,16 +81,16 @@ final class ComponentLifecycleTests: XCTestCase {

let items = (1 ... Int.random(in: 10 ... 20)).map { index -> LifecycleTask in
let id = "item-\(index)"
return ComponentLifecycle.Task(label: id,
start: .sync {
dispatchPrecondition(condition: .onQueue(testQueue))
startCalls.append(id)
},
shutdown: .sync {
dispatchPrecondition(condition: .onQueue(testQueue))
XCTAssertTrue(startCalls.contains(id))
stopCalls.append(id)
})
return _LifecycleTask(label: id,
start: .sync {
dispatchPrecondition(condition: .onQueue(testQueue))
startCalls.append(id)
},
shutdown: .sync {
dispatchPrecondition(condition: .onQueue(testQueue))
XCTAssertTrue(startCalls.contains(id))
stopCalls.append(id)
})
}
lifecycle.register(items)

Expand Down