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

Finish renaming Strada to Bridge #17

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let package = Package(
path: "Source",
resources: [
.copy("Turbo/WebView/turbo.js"),
.copy("Bridge/strada.js")
.copy("Bridge/bridge.js")
]
),
.testTarget(
Expand Down
6 changes: 3 additions & 3 deletions Source/Bridge/Bridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public final class Bridge: Bridgable {
private let bridgeGlobal = "window.nativeBridge"

/// The webkit.messageHandlers name
private let scriptHandlerName = "strada"
private let scriptHandlerName = "bridge"

@MainActor
private func callBridgeFunction(_ function: JavaScriptBridgeFunction, arguments: [Any]) async throws {
Expand All @@ -137,7 +137,7 @@ public final class Bridge: Bridgable {

private func makeUserScript() -> WKUserScript? {
guard
let path = Bundle.module.path(forResource: "strada", ofType: "js")
let path = Bundle.module.path(forResource: "bridge", ofType: "js")
else {
return nil
}
Expand All @@ -146,7 +146,7 @@ public final class Bridge: Bridgable {
let source = try String(contentsOfFile: path)
return WKUserScript(source: source, injectionTime: .atDocumentStart, forMainFrameOnly: true)
} catch {
assertionFailure("Could not open strada.js: \(error)")
assertionFailure("Could not open bridge.js: \(error)")
return nil
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public enum Strada {
public enum UserAgent {
public static func userAgentSubstring(for componentTypes: [BridgeComponent.Type]) -> String {
let components = componentTypes.map { $0.name }.joined(separator: " ")
return "bridge-components: [\(components)]"
Expand Down
14 changes: 8 additions & 6 deletions Source/Bridge/strada.js → Source/Bridge/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}

notifyBridgeOfSupportedComponentsUpdate() {
if (this.isStradaAvailable) {
if (this.isWebBridgeAvailable) {
this.webBridge.adapterDidUpdateSupportedComponents()
}
}
Expand All @@ -47,7 +47,7 @@

// Reply to web with message.
replyWith(message) {
if (this.isStradaAvailable) {
if (this.isWebBridgeAvailable) {
this.webBridge.receive(message)
}
}
Expand All @@ -64,15 +64,17 @@
// Native handler

postMessage(message) {
webkit.messageHandlers.strada.postMessage(message)
webkit.messageHandlers.bridge.postMessage(message)
}

get isStradaAvailable() {
return window.Strada
get isWebBridgeAvailable() {
// Fallback to Strada for legacy Strada web JavaScript.
return window.Bridge ?? window.Strada
}

get webBridge() {
return window.Strada.web
// Fallback to Strada for legacy Strada web JavaScript.
return window.Bridge?.web ?? window.Strada.web
}
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Hotwire.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public enum Hotwire {
HotwireWebViewController(url: url)
}

Hotwire.config.userAgent += " \(Strada.userAgentSubstring(for: componentTypes))"
Hotwire.config.userAgent += " \(UserAgent.userAgentSubstring(for: componentTypes))"
bridgeComponentTypes = componentTypes

Hotwire.config.makeCustomWebView = { configuration in
Expand Down
4 changes: 2 additions & 2 deletions Tests/Bridge/JavaScriptTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class JavaScriptTests: XCTestCase {
}

func testToStringWithNestedObject() throws {
let javaScript = JavaScript(object: "window.strada", functionName: "test")
XCTAssertEqual(try javaScript.toString(), "window.strada.test()")
let javaScript = JavaScript(object: "window.bridge", functionName: "test")
XCTAssertEqual(try javaScript.toString(), "window.bridge.test()")
}

func testToStringWithInvalidArgumentTypeThrowsError() {
Expand Down
4 changes: 2 additions & 2 deletions Tests/Bridge/UserAgentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import XCTest

class UserAgentTests: XCTestCase {
func testUserAgentSubstringWithTwoComponents() {
let userAgentSubstring = Strada.userAgentSubstring(for: [OneBridgeComponent.self, TwoBridgeComponent.self])
let userAgentSubstring = UserAgent.userAgentSubstring(for: [OneBridgeComponent.self, TwoBridgeComponent.self])
XCTAssertEqual(userAgentSubstring, "bridge-components: [one two]")
}

func testUserAgentSubstringWithNoComponents() {
let userAgentSubstring = Strada.userAgentSubstring(for: [])
let userAgentSubstring = UserAgent.userAgentSubstring(for: [])
XCTAssertEqual(userAgentSubstring, "bridge-components: []")
}
}