Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Aug 30, 2024
1 parent c0bdf61 commit cd5419e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
25 changes: 21 additions & 4 deletions 0292-cross-platform-pt3/Counter/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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"),
Expand All @@ -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]
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Dependencies
import FactClient
import Foundation
import Perception
import SwiftNavigation
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import FactClient
import Dependencies
import DependenciesMacros

#if canImport(JavaScriptKit)
@preconcurrency import JavaScriptKit
#endif
#if canImport(JavaScriptEventLoop)
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!
Expand Down
18 changes: 18 additions & 0 deletions 0292-cross-platform-pt3/Counter/Sources/WasmApp/App.swift
Original file line number Diff line number Diff line change
@@ -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<ObservationToken> = []

static func main() {
IssueReporters.current = [JavaScriptConsoleWarning()]
JavaScriptEventLoop.installGlobalExecutor()

let model = CounterModel()
Expand Down

0 comments on commit cd5419e

Please sign in to comment.