From 050a1d6dc2fbcfbd001012687763fdd7eea2e7a5 Mon Sep 17 00:00:00 2001 From: TizianoCoroneo Date: Tue, 18 Jul 2023 17:28:49 +0200 Subject: [PATCH] refactor: added MainActor annotations where needed. --- Sources/Deeplink/AnyDeeplink.swift | 8 ++++---- Sources/Deeplink/DeeplinksCenter.swift | 10 +++++----- .../Tutorials/Adding a Deeplink.tutorial | 1 - Sources/Deeplink/SampleDeeplink.swift | 6 +++--- Tests/DeeplinkTests/DeeplinkBuilderTests.swift | 1 + Tests/DeeplinkTests/DeeplinksCenterTests.swift | 7 ++++--- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/Sources/Deeplink/AnyDeeplink.swift b/Sources/Deeplink/AnyDeeplink.swift index a7f0a39..a162c0c 100644 --- a/Sources/Deeplink/AnyDeeplink.swift +++ b/Sources/Deeplink/AnyDeeplink.swift @@ -9,7 +9,7 @@ import Foundation /// Type-erased version of ``Deeplink/Deeplink``. Used to keep a list of Deeplinks in the ``Deeplink/DeeplinksCenter`` where every element of the list might have different type parameters. public struct AnyDeeplink: CustomStringConvertible { - var parseURLIntoInstance: (URL) throws -> Bool + var parseURLIntoInstance: @MainActor (URL) throws -> Bool public let description: String /// Designated initializer. @@ -21,7 +21,7 @@ public struct AnyDeeplink: CustomStringConvertible { init( deeplink: Deeplink, assigningTo instance: Value, - ifMatching completion: @escaping (URL, Value) throws -> Bool + ifMatching completion: @escaping @MainActor (URL, Value) throws -> Bool ) { self.parseURLIntoInstance = { url in var newInstance = instance @@ -39,7 +39,7 @@ public struct AnyDeeplink: CustomStringConvertible { /// - completion: The completion handler to invoke if the `URL` passed to the `parse(_ url:)` function matches the deeplink template. init( deeplink: Deeplink, - ifMatching completion: @escaping (URL, Value) throws -> Bool + ifMatching completion: @escaping @MainActor (URL, Value) throws -> Bool ) where Value: DefaultInitializable { @@ -55,7 +55,7 @@ extension AnyDeeplink { /// Attempts to parse the argument `url` using the `deeplink` previously passed to the initializer. /// If the pattern is recognized successfully, the arguments of the template will get assigned to the `assigningTo` parameter, which will be then forwarded to the `ifMatching` closure, together with the matching `url`. /// - Parameter url: The `URL` to match. - func parse( + @MainActor func parse( _ url: URL ) throws -> Bool { try self.parseURLIntoInstance(url) diff --git a/Sources/Deeplink/DeeplinksCenter.swift b/Sources/Deeplink/DeeplinksCenter.swift index 4e99244..419b6fe 100644 --- a/Sources/Deeplink/DeeplinksCenter.swift +++ b/Sources/Deeplink/DeeplinksCenter.swift @@ -104,7 +104,7 @@ public class DeeplinksCenter { public func register( deeplink: Deeplink, assigningTo: Value, - ifMatching completion: @escaping (URL, Value) throws -> Bool + ifMatching completion: @escaping @MainActor (URL, Value) throws -> Bool ) -> DeeplinksCenter { let typeErasedDeeplink = AnyDeeplink( deeplink: deeplink, @@ -152,7 +152,7 @@ public class DeeplinksCenter { public func register( deeplinks: [Deeplink], assigningTo: Value, - ifMatching completion: @escaping (URL, Value) throws -> Bool + ifMatching completion: @escaping @MainActor (URL, Value) throws -> Bool ) -> DeeplinksCenter { self.deeplinks.append(contentsOf: deeplinks.map { @@ -200,7 +200,7 @@ public class DeeplinksCenter { @discardableResult public func register( deeplink: Deeplink, - ifMatching completion: @escaping (URL) throws -> Bool + ifMatching completion: @escaping @MainActor (URL) throws -> Bool ) -> DeeplinksCenter { self.register( deeplink: deeplink, @@ -243,7 +243,7 @@ public class DeeplinksCenter { @discardableResult public func register( deeplinks: [Deeplink], - ifMatching completion: @escaping (URL) throws -> Bool + ifMatching completion: @escaping @MainActor (URL) throws -> Bool ) -> DeeplinksCenter { self.register( deeplinks: deeplinks, @@ -257,7 +257,7 @@ public class DeeplinksCenter { /// If a match is found, the corresponding argument segments will be assigned to the keypaths specified in the deeplink interpolation, and the `ifMatching` closure defined when calling `register:` will be executed. /// If no match is found, an error is thrown. /// - Parameter url: The `URL` to parse. - public func parse( + @MainActor public func parse( url: URL ) throws { diff --git a/Sources/Deeplink/Documentation.docc/Tutorials/Adding a Deeplink.tutorial b/Sources/Deeplink/Documentation.docc/Tutorials/Adding a Deeplink.tutorial index 0ca7cb3..2a86c07 100644 --- a/Sources/Deeplink/Documentation.docc/Tutorials/Adding a Deeplink.tutorial +++ b/Sources/Deeplink/Documentation.docc/Tutorials/Adding a Deeplink.tutorial @@ -64,7 +64,6 @@ Now we need to make a template that describes how to parse the information from a link. This time, we'll use a keypath to our `[String]` property, and pass an additional argument to tell the system what separator should we use to parse the arguments. - As before, the data present in the URL at the location where the keypath is inserted will be assigned to the property indicated by the keypath; each item in its own String object. @Code(name: "Defining Deeplink Data Lists.swift", file: "AddingADeeplinkList 2.swift") diff --git a/Sources/Deeplink/SampleDeeplink.swift b/Sources/Deeplink/SampleDeeplink.swift index dcd7373..5330cc4 100644 --- a/Sources/Deeplink/SampleDeeplink.swift +++ b/Sources/Deeplink/SampleDeeplink.swift @@ -39,7 +39,7 @@ public struct SampleDeeplink { /// - deeplinkTemplate: The deeplink template that you want to test. It is recommended to pass the exact same template instance that you use in your `DeeplinkCenter` to avoid having to manually keep them in sync. /// - urlToParse: The test URL that you want to use for testing. /// - assigningToInstance: The instance of the object to which assign the values of the parsed parameters. - /// - expectation: A closure where you should declare an expectation to fulfill, using ``XCTest/XCTestCase/expectation(description:)``. + /// - expectation: A closure where you should declare an expectation to fulfill, using `XCTestCase.expectation(description:)`. /// - assertions: A closure that you can use to verify the correctness of the parsed parameters. public init( deeplinkTemplate: Deeplink, @@ -67,7 +67,7 @@ public extension SampleDeeplink where Value == Void { /// - Parameters: /// - deeplinkTemplate: The deeplink template that you want to test. It is recommended to pass the exact same template instance that you use in your `DeeplinkCenter` to avoid having to manually keep them in sync. /// - urlToParse: The test URL that you want to use for testing. - /// - expectation: A closure where you should declare an expectation to fulfill, using ``XCTest/XCTestCase/expectation(description:)``. + /// - expectation: A closure where you should declare an expectation to fulfill, using `XCTestCase.expectation(description:)`. init( deeplinkTemplate: Deeplink, urlToParse: URL, @@ -102,7 +102,7 @@ public extension DeeplinksCenter { /// /// See ``SampleDeeplink`` for more information. /// - Parameter sample: The ``SampleDeeplink`` to test. - func testSampleDeeplink( + @MainActor func testSampleDeeplink( _ sample: SampleDeeplink ) throws { guard let anyDeeplinkIndex = self.deeplinks diff --git a/Tests/DeeplinkTests/DeeplinkBuilderTests.swift b/Tests/DeeplinkTests/DeeplinkBuilderTests.swift index ac0328c..d54d47c 100644 --- a/Tests/DeeplinkTests/DeeplinkBuilderTests.swift +++ b/Tests/DeeplinkTests/DeeplinkBuilderTests.swift @@ -19,6 +19,7 @@ fileprivate struct TestData2 { var arg2: String? } +@MainActor class DeeplinkBuilderTests: XCTestCase { func testFunctionBuilder() throws { diff --git a/Tests/DeeplinkTests/DeeplinksCenterTests.swift b/Tests/DeeplinkTests/DeeplinksCenterTests.swift index 8cb40ac..1568ebc 100644 --- a/Tests/DeeplinkTests/DeeplinksCenterTests.swift +++ b/Tests/DeeplinkTests/DeeplinksCenterTests.swift @@ -9,6 +9,7 @@ import Foundation import XCTest @testable import Deeplink +@MainActor class DeeplinksCenterTests: XCTestCase { struct Artist: Equatable { @@ -360,7 +361,7 @@ class DeeplinksCenterTests: XCTestCase { XCTAssertNoThrow(try repo .parse(url: "https://ticketswap.com/test1/test3")) - waitForExpectations(timeout: 0.1, handler: nil) + waitForExpectations(timeout: 0.01, handler: nil) } func testMultipleMatchingLiteralFormatsCallSameClosure() { @@ -462,7 +463,7 @@ class DeeplinksCenterTests: XCTestCase { XCTAssertNoThrow(try repo .parse(url: url3)) - waitForExpectations(timeout: 0.1, handler: nil) + waitForExpectations(timeout: 0.01, handler: nil) } func testNoMatchFound() { @@ -597,7 +598,7 @@ class DeeplinksCenterTests: XCTestCase { deeplinkError) }) - waitForExpectations(timeout: 0.1, handler: nil) + waitForExpectations(timeout: 0.01, handler: nil) } func testMatchingSuccessDoesNotRollToNextMatches() {