Skip to content

Commit

Permalink
Update process management, Add Persistable Startup Processes
Browse files Browse the repository at this point in the history
  • Loading branch information
dejanskledar committed Sep 5, 2024
1 parent 89b6e8a commit 5a7bef8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
9 changes: 5 additions & 4 deletions Sources/Utilities/StartupService/StartupProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import Foundation

/// An abstraction for a predefined set of functionality, aimed to be ran once, at app startup.
@available(*, deprecated, message: "The `StartupProcess` protocol with `run(:completion)` is deprecated. Instead, use `StartupableProcess` with `run()` only.")
public protocol StartupProcess {
func run(completion: @escaping (Bool) -> Void)
}

extension StartupProcess {
public var id: String {
UUID().uuidString
}
public protocol StartupableProcess {
func run()
}

public protocol PersistableProcess {}
13 changes: 12 additions & 1 deletion Sources/Utilities/StartupService/StartupProcessService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,20 @@ import Foundation
public final class StartupProcessService {
public init() {}

public var persistingProcesses = [StartupableProcess & PersistableProcess]()

@discardableResult
@available(*, deprecated, message: "Use `execute(process: StartupableProcess` instead with new process types.")
public func execute(process: StartupProcess) -> StartupProcessService {
process.run { guard $0 else { return } }
process.run { _ in }
return self
}

public func execute(process: StartupableProcess) -> StartupProcessService {
process.run()
if let process = process as? (StartupableProcess & PersistableProcess) {
persistingProcesses.append(process)
}
return self
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,32 @@ import PovioKitUtilities

class StartupProcessServiceTests: XCTestCase {
func testShouldCompleteProccesWhenCallExecution() {
let mock = MockedStartupProcess()
let mockPersisted = MockedPersistedStartupProcess()

let sut = StartupProcessService()
let mock = MockedStartupService()
sut.execute(process: mock)
XCTAssertEqual(mock.completed, true)
}

func test_id_isUniqueForDifferentInstances() {
let process1 = MockedStartupService()
let process2 = MockedStartupService()
.execute(process: mock)
.execute(process: mockPersisted)

XCTAssertEqual(MockedStartupProcess.completed, true)
XCTAssertEqual(MockedPersistedStartupProcess.completed, true)

XCTAssertNotEqual(process1.id, process2.id)
XCTAssertTrue(sut.persistingProcesses.contains(where: { $0 is MockedPersistedStartupProcess }))
}
}

private class MockedStartupService: StartupProcess {
var completed: Bool = false
func run(completion: @escaping (Bool) -> Void) {
completed = true
return
private struct MockedStartupProcess: StartupableProcess {
static var completed = false

func run() {
MockedStartupProcess.completed = true
}
}

private struct MockedPersistedStartupProcess: StartupableProcess, PersistableProcess {
static var completed = false

func run() {
MockedPersistedStartupProcess.completed = true
}
}

0 comments on commit 5a7bef8

Please sign in to comment.