diff --git a/0292-cross-platform-pt3/Counter/Package.swift b/0292-cross-platform-pt3/Counter/Package.swift index 3be6380e..07ac5bff 100644 --- a/0292-cross-platform-pt3/Counter/Package.swift +++ b/0292-cross-platform-pt3/Counter/Package.swift @@ -10,6 +10,10 @@ let package = Package( name: "Counter", targets: ["Counter"] ), + .library( + name: "FactClient", + targets: ["FactClient"] + ), ], dependencies: [ .package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.0.0"), @@ -23,6 +27,7 @@ let package = Package( name: "WasmApp", dependencies: [ "Counter", + "FactClientLive", .product(name: "SwiftNavigation", package: "swift-navigation"), .product(name: "JavaScriptEventLoop", package: "JavaScriptKit"), .product(name: "JavaScriptKit", package: "JavaScriptKit"), @@ -31,14 +36,26 @@ let package = Package( .target( name: "Counter", dependencies: [ - .product(name: "JavaScriptKit", package: "JavaScriptKit", condition: .when(platforms: [.wasi])), - .product(name: "JavaScriptEventLoop", package: "JavaScriptKit", condition: .when(platforms: [.wasi])), - .product(name: "Dependencies", package: "swift-dependencies"), - .product(name: "DependenciesMacros", package: "swift-dependencies"), + "FactClient", .product(name: "SwiftNavigation", package: "swift-navigation"), .product(name: "Perception", package: "swift-perception") ] ), + .target( + name: "FactClient", + dependencies: [ + .product(name: "Dependencies", package: "swift-dependencies"), + .product(name: "DependenciesMacros", package: "swift-dependencies"), + ] + ), + .target( + name: "FactClientLive", + dependencies: [ + "FactClient", + .product(name: "JavaScriptKit", package: "JavaScriptKit", condition: .when(platforms: [.wasi])), + .product(name: "JavaScriptEventLoop", package: "JavaScriptKit", condition: .when(platforms: [.wasi])), + ] + ) ], swiftLanguageVersions: [.v6] ) diff --git a/0292-cross-platform-pt3/Counter/Sources/Counter/CounterModel.swift b/0292-cross-platform-pt3/Counter/Sources/Counter/CounterModel.swift index 13fb25c7..826eb023 100644 --- a/0292-cross-platform-pt3/Counter/Sources/Counter/CounterModel.swift +++ b/0292-cross-platform-pt3/Counter/Sources/Counter/CounterModel.swift @@ -1,4 +1,5 @@ import Dependencies +import FactClient import Foundation import Perception import SwiftNavigation diff --git a/0292-cross-platform-pt3/Counter/Sources/FactClient/FactClient.swift b/0292-cross-platform-pt3/Counter/Sources/FactClient/FactClient.swift new file mode 100644 index 00000000..d0d1998f --- /dev/null +++ b/0292-cross-platform-pt3/Counter/Sources/FactClient/FactClient.swift @@ -0,0 +1,11 @@ +import Dependencies +import DependenciesMacros + +@DependencyClient +public struct FactClient: Sendable { + public var fetch: @Sendable (Int) async throws -> String +} + +extension FactClient: TestDependencyKey { + public static let testValue = FactClient() +} diff --git a/0292-cross-platform-pt3/Counter/Sources/Counter/FactClient.swift b/0292-cross-platform-pt3/Counter/Sources/FactClientLive/FactClientLive.swift similarity index 72% rename from 0292-cross-platform-pt3/Counter/Sources/Counter/FactClient.swift rename to 0292-cross-platform-pt3/Counter/Sources/FactClientLive/FactClientLive.swift index 6157694a..e0bffb16 100644 --- a/0292-cross-platform-pt3/Counter/Sources/Counter/FactClient.swift +++ b/0292-cross-platform-pt3/Counter/Sources/FactClientLive/FactClientLive.swift @@ -1,5 +1,6 @@ +import FactClient import Dependencies -import DependenciesMacros + #if canImport(JavaScriptKit) @preconcurrency import JavaScriptKit #endif @@ -7,17 +8,8 @@ import DependenciesMacros import JavaScriptEventLoop #endif -@DependencyClient -struct FactClient: Sendable { - var fetch: @Sendable (Int) async throws -> String -} - -extension FactClient: TestDependencyKey { - static let testValue = FactClient() -} - extension FactClient: DependencyKey { - static let liveValue = FactClient { number in + public static let liveValue = FactClient { number in #if canImport(JavaScriptKit) && canImport(JavaScriptEventLoop) let response = try await JSPromise( JSObject.global.fetch!("http://www.numberapi.com/\(number)").object! diff --git a/0292-cross-platform-pt3/Counter/Sources/WasmApp/App.swift b/0292-cross-platform-pt3/Counter/Sources/WasmApp/App.swift index 2a77592c..7439077d 100644 --- a/0292-cross-platform-pt3/Counter/Sources/WasmApp/App.swift +++ b/0292-cross-platform-pt3/Counter/Sources/WasmApp/App.swift @@ -1,14 +1,32 @@ import Counter +import IssueReporting import JavaScriptEventLoop import JavaScriptKit import SwiftNavigation +struct JavaScriptConsoleWarning: IssueReporter { + func reportIssue( + _ message: @autoclosure () -> String?, + fileID: StaticString, + filePath: StaticString, + line: UInt, + column: UInt + ) { + #if DEBUG + _ = JSObject.global.console.warn(""" + \(fileID):\(line) - \(message() ?? "") + """) + #endif + } +} + @main @MainActor struct App { static var tokens: Set = [] static func main() { + IssueReporters.current = [JavaScriptConsoleWarning()] JavaScriptEventLoop.installGlobalExecutor() let model = CounterModel()