Skip to content
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

Support MainActor #43

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ on:
pull_request:
branches:
- '*'
env:
DEVELOPER_DIR: /Applications/Xcode_13.2.app
jobs:
SwiftPM:
runs-on: macos-11
runs-on: macos-12
steps:
- uses: actions/checkout@v2
- name: Run Tests
Expand Down
2 changes: 1 addition & 1 deletion Crossroad.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Pod::Spec.new do |s|
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "giginet" => "giginet.net@gmail.com" }
s.social_media_url = "http://twitter.com/giginet"
s.platforms = { :ios => "9.0", :tvos => "9.0" }
s.platforms = { :ios => "13.0", :tvos => "13.0" }
s.source = { :git => "https://github.com/giginet/Crossroad.git", :tag => "#{s.version}" }
s.source_files = "Sources/Crossroad/**/*.{h,swift}"
s.requires_arc = true
Expand Down
18 changes: 16 additions & 2 deletions Crossroad.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
072F8EE2271C397C002F767C /* Parsable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 072F8ECA271C397C002F767C /* Parsable.swift */; };
54019C7C271D92AD002D00D6 /* ContextParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54019C7B271D92AD002D00D6 /* ContextParser.swift */; };
54019C7D271D92AD002D00D6 /* ContextParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54019C7B271D92AD002D00D6 /* ContextParser.swift */; };
54675FA72768937100185500 /* AsyncAssertion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54675FA62768937100185500 /* AsyncAssertion.swift */; };
54675FA82768937100185500 /* AsyncAssertion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54675FA62768937100185500 /* AsyncAssertion.swift */; };
546DEA8420AD77F800923325 /* Crossroad.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 546DEA7A20AD77F800923325 /* Crossroad.framework */; };
FF4DE71B238CA19200FAC931 /* Crossroad.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FF4DE70C238C9B7600FAC931 /* Crossroad.framework */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -91,6 +93,7 @@
072F8EC9271C397C002F767C /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
072F8ECA271C397C002F767C /* Parsable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Parsable.swift; sourceTree = "<group>"; };
54019C7B271D92AD002D00D6 /* ContextParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContextParser.swift; sourceTree = "<group>"; };
54675FA62768937100185500 /* AsyncAssertion.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AsyncAssertion.swift; sourceTree = "<group>"; };
546DEA7A20AD77F800923325 /* Crossroad.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Crossroad.framework; sourceTree = BUILT_PRODUCTS_DIR; };
546DEA8320AD77F800923325 /* CrossroadTests-iOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "CrossroadTests-iOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
FF4DE70C238C9B7600FAC931 /* Crossroad.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Crossroad.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -174,6 +177,7 @@
546DEAAB20B098C800923325 /* CrossroadTests */ = {
isa = PBXGroup;
children = (
54675FA62768937100185500 /* AsyncAssertion.swift */,
072F8EA8271C3962002F767C /* ContextTests.swift */,
072F8EA4271C3962002F767C /* DSLTests.swift */,
072F8EA2271C3962002F767C /* Info.plist */,
Expand Down Expand Up @@ -386,6 +390,7 @@
072F8EB5271C3962002F767C /* Router_AcceptanceTests.swift in Sources */,
072F8EAB271C3962002F767C /* RouterTests.swift in Sources */,
072F8EB7271C3962002F767C /* OpenURLOptionTests.swift in Sources */,
54675FA72768937100185500 /* AsyncAssertion.swift in Sources */,
072F8EB3271C3962002F767C /* Router_ValidationTests.swift in Sources */,
072F8EB1271C3962002F767C /* DSLTests.swift in Sources */,
072F8EB9271C3962002F767C /* ContextTests.swift in Sources */,
Expand Down Expand Up @@ -419,6 +424,7 @@
072F8EB6271C3962002F767C /* Router_AcceptanceTests.swift in Sources */,
072F8EAC271C3962002F767C /* RouterTests.swift in Sources */,
072F8EB8271C3962002F767C /* OpenURLOptionTests.swift in Sources */,
54675FA82768937100185500 /* AsyncAssertion.swift in Sources */,
072F8EB4271C3962002F767C /* Router_ValidationTests.swift in Sources */,
072F8EB2271C3962002F767C /* DSLTests.swift in Sources */,
072F8EBA271C3962002F767C /* ContextTests.swift in Sources */,
Expand Down Expand Up @@ -497,7 +503,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
Expand Down Expand Up @@ -555,7 +561,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MTL_ENABLE_DEBUG_INFO = NO;
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OPTIMIZATION_LEVEL = "-O";
Expand Down Expand Up @@ -594,6 +600,7 @@
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TVOS_DEPLOYMENT_TARGET = 13.0;
};
name = Debug;
};
Expand Down Expand Up @@ -624,6 +631,7 @@
SKIP_INSTALL = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TVOS_DEPLOYMENT_TARGET = 13.0;
};
name = Release;
};
Expand All @@ -647,6 +655,7 @@
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TVOS_DEPLOYMENT_TARGET = 13.0;
};
name = Debug;
};
Expand All @@ -669,6 +678,7 @@
SDKROOT = iphoneos;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TVOS_DEPLOYMENT_TARGET = 13.0;
};
name = Release;
};
Expand Down Expand Up @@ -698,6 +708,7 @@
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
TVOS_DEPLOYMENT_TARGET = 13.0;
};
name = Debug;
};
Expand Down Expand Up @@ -726,6 +737,7 @@
SDKROOT = appletvos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 5.0;
TVOS_DEPLOYMENT_TARGET = 13.0;
};
name = Release;
};
Expand All @@ -748,6 +760,7 @@
SDKROOT = appletvos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
TVOS_DEPLOYMENT_TARGET = 13.0;
};
name = Debug;
};
Expand All @@ -769,6 +782,7 @@
PROVISIONING_PROFILE_SPECIFIER = "";
SDKROOT = appletvos;
SWIFT_VERSION = 5.0;
TVOS_DEPLOYMENT_TARGET = 13.0;
};
name = Release;
};
Expand Down
1 change: 1 addition & 0 deletions Demo/Demo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extension EnvironmentValues {
}

@main
@MainActor
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PackageDescription

let package = Package(
name: "Crossroad",
platforms: [.iOS(.v9),.tvOS(.v9)],
platforms: [.iOS(.v13),.tvOS(.v13)],
products: [
.library(
name: "Crossroad",
Expand Down
5 changes: 2 additions & 3 deletions Sources/Crossroad/OpenURLOption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ public typealias DefaultRouter = Router<OpenURLOption>

public extension Router where UserInfo == OpenURLOption {
@discardableResult
func openIfPossible(_ url: URL, options: ApplicationOpenURLOptions) -> Bool {
@MainActor func openIfPossible(_ url: URL, options: ApplicationOpenURLOptions) -> Bool {
return openIfPossible(url, userInfo: OpenURLOption(options: options))
}
}

@available(iOS 13.0, *)
extension Router where UserInfo == OpenURLOption {
@discardableResult
public func openIfPossible(_ url: URL, options: UIScene.OpenURLOptions) -> Bool {
@MainActor public func openIfPossible(_ url: URL, options: UIScene.OpenURLOptions) -> Bool {
return openIfPossible(url, userInfo: OpenURLOption(options: options))
}
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/Crossroad/Route.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

public struct Route<UserInfo> {
public typealias Handler = (Context<UserInfo>) throws -> Void
public typealias Handler = @MainActor (Context<UserInfo>) throws -> Void
var pattern: Pattern
var acceptPolicy: AcceptPolicy
var handler: Handler
Expand All @@ -24,6 +24,7 @@ public struct Route<UserInfo> {
self.handler = handler
}

@MainActor
func executeHandler(context: Context<UserInfo>) throws -> Bool {
do {
try handler(context)
Expand Down
20 changes: 11 additions & 9 deletions Sources/Crossroad/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ public final class Router<UserInfo> {
}

@discardableResult
public func openIfPossible(_ url: URL, userInfo: UserInfo) -> Bool {
searchMatchingRoutes(to: url)
.first { result in
do {
return try result.route.executeHandler(context: result.context.attach(userInfo))
} catch {
return false
}
} != nil
@MainActor public func openIfPossible(_ url: URL, userInfo: UserInfo) -> Bool {
for result in searchMatchingRoutes(to: url) {
do {
let succeeded = try result.route.executeHandler(context: result.context.attach(userInfo))
if succeeded { return true }
} catch {
continue
}
}
return false
}

public func responds(to url: URL) -> Bool {
Expand Down Expand Up @@ -95,6 +96,7 @@ public final class Router<UserInfo> {

public extension Router where UserInfo == Void {
@discardableResult
@MainActor
func openIfPossible(_ url: URL) -> Bool {
return openIfPossible(url, userInfo: ())
}
Expand Down
14 changes: 14 additions & 0 deletions Tests/CrossroadTests/AsyncAssertion.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation
import XCTest

func assertTrueAsynchronously(_ closure: @autoclosure () async -> Bool, message: String = "", file: StaticString = #file, line: UInt = #line) async {
let result = await closure()
XCTAssertTrue(result, message, file: file, line: line)
}

func assertFalseAsynchronously(_ closure: @autoclosure () async -> Bool, message: String = "", file: StaticString = #file, line: UInt = #line) async {
let result = await closure()
if result {
XCTFail(message, file: file, line: line)
}
}