Skip to content

Commit

Permalink
Add notification when registerBridgeComponents is called
Browse files Browse the repository at this point in the history
  • Loading branch information
olivaresf committed Oct 21, 2024
1 parent 3ba9441 commit e0dda4d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions Source/Bridge/Bridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public final class Bridge: Bridgable {
public static func initialize(_ webView: WKWebView) {
if getBridgeFor(webView) == nil {
initialize(Bridge(webView: webView))
webView.customUserAgent = Hotwire.config.userAgent
}
}

Expand Down
2 changes: 2 additions & 0 deletions Source/Hotwire.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public enum Hotwire {
Bridge.initialize(webView)
return webView
}

NotificationCenter.default.postDidRegisterBridgeComponents()
}

static var bridgeComponentTypes = [BridgeComponent.Type]()
Expand Down
39 changes: 39 additions & 0 deletions Source/HotwireNotifications.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// HotwireNotifications.swift
// HotwireNative
//
// Created by Fernando Olivares on 20/10/24.
//

import Foundation
import UIKit

extension NSNotification.Name {
static let didRegisterBridgeComponents = NSNotification.Name("HotwireNativeDidRegisterBridgeComponents")
}

extension NotificationCenter {
func removeObservation(_ observationToken: NSObjectProtocol?) {
guard let observationToken else { return }
removeObserver(observationToken)
}

func removeObservations(_ observationTokens: [NSObjectProtocol?]) {
for observationToken in observationTokens.compactMap({ $0 }) {
removeObserver(observationToken)
}
}
}

extension NotificationCenter {

func postDidRegisterBridgeComponents() {
post(name: .didRegisterBridgeComponents, object: nil)
}

func observeDidRegisterBridgeComponents(reaction: @escaping () -> Void) -> NSObjectProtocol {
addObserver(forName: .didRegisterBridgeComponents, object: nil, queue: .main) { _ in
reaction()
}
}
}
10 changes: 10 additions & 0 deletions Source/Turbo/Navigator/Navigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ public class Navigator {
self.webkitUIDelegate = WKUIController(delegate: self)
session.webView.uiDelegate = webkitUIDelegate
modalSession.webView.uiDelegate = webkitUIDelegate

didRegisterBridgeComponentsObservationToken = NotificationCenter.default.observeDidRegisterBridgeComponents {
Bridge.initialize(session.webView)
Bridge.initialize(modalSession.webView)
}
}

deinit {
NotificationCenter.default.removeObservation(didRegisterBridgeComponentsObservationToken)
}

// MARK: Private
Expand All @@ -146,6 +155,7 @@ public class Navigator {
private let navigatorDelegate = DefaultNavigatorDelegate()
private var backgroundTerminatedWebViewSessions = [Session]()
private var appInBackground = false
private var didRegisterBridgeComponentsObservationToken: NSObjectProtocol?

private func controller(for proposal: VisitProposal) -> UIViewController? {
switch delegate.handle(proposal: proposal) {
Expand Down

0 comments on commit e0dda4d

Please sign in to comment.