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

Avoid accessing the bridge directly from within the BridgeComponent. #16

Merged
merged 1 commit into from
Aug 22, 2023
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
8 changes: 1 addition & 7 deletions Source/BridgeComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,7 @@ open class BridgeComponent: BridgingComponent {
/// - Parameter message: The message to be replied with.
/// - Returns: `true` if the reply was successful, `false` if the bridge is not available.
public func reply(with message: Message) -> Bool {
guard let bridge = delegate.bridge else {
logger.warning("bridgeMessageFailedToReply: bridge is not available")
return false
}

bridge.reply(with: message)
return true
return delegate.reply(with: message)
}

@discardableResult
Expand Down
15 changes: 15 additions & 0 deletions Source/BridgeDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ public final class BridgeDelegate {
bridge = nil
}

@discardableResult
/// Replies to the web with a received message, optionally replacing its `event` or `jsonData`.
///
/// - Parameter message: The message to be replied with.
/// - Returns: `true` if the reply was successful, `false` if the bridge is not available.
public func reply(with message: Message) -> Bool {
guard let bridge else {
logger.warning("bridgeMessageFailedToReply: bridge is not available")
return false
}

bridge.reply(with: message)
return true
}

// MARK: - Destination lifecycle

public func onViewDidLoad() {
Expand Down
22 changes: 22 additions & 0 deletions Tests/BridgeDelegateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,28 @@ class BridgeDelegateTests: XCTestCase {
XCTAssertTrue(delegate.bridgeDidReceiveMessage(testMessage()))
}

// MARK: reply(with:)

func test_replyWithSucceedsWhenBridgeIsSet() {
let message = testMessage()
let success = delegate.reply(with: message)

XCTAssertTrue(success)
XCTAssertTrue(bridge.replyWithMessageWasCalled)
XCTAssertEqual(bridge.replyWithMessageArg, message)
}

func test_replyWithFailsWhenBridgeNotSet() {
delegate.bridge = nil

let message = testMessage()
let success = delegate.reply(with: message)

XCTAssertFalse(success)
XCTAssertFalse(bridge.replyWithMessageWasCalled)
XCTAssertNil(bridge.replyWithMessageArg)
}

private func testMessage() -> Message {
return Message(id: "1",
component: "two",
Expand Down