diff --git a/react-native-gutenberg-bridge/ios/Gutenberg.swift b/react-native-gutenberg-bridge/ios/Gutenberg.swift index 8b4c509804..7a3fa9a0fc 100644 --- a/react-native-gutenberg-bridge/ios/Gutenberg.swift +++ b/react-native-gutenberg-bridge/ios/Gutenberg.swift @@ -126,7 +126,7 @@ public class Gutenberg: NSObject { if let serverID = serverID { data["mediaServerId"] = serverID } - bridgeModule.sendEventIfNeeded(.mediaUpload, body: data) + sendEvent(.mediaUpload, body: data) } public func appendMedia(id: Int32, url: URL, type: MediaType) { @@ -135,7 +135,7 @@ public class Gutenberg: NSObject { "mediaUrl" : url.absoluteString, "mediaType": type.rawValue, ] - bridgeModule.sendEventIfNeeded(.mediaAppend, body: data) + sendEvent(.mediaAppend, body: data) } public func setFocusOnTitle() { diff --git a/react-native-gutenberg-bridge/ios/RNReactNativeGutenbergBridge.swift b/react-native-gutenberg-bridge/ios/RNReactNativeGutenbergBridge.swift index e639ccaf11..aead5bf2cb 100644 --- a/react-native-gutenberg-bridge/ios/RNReactNativeGutenbergBridge.swift +++ b/react-native-gutenberg-bridge/ios/RNReactNativeGutenbergBridge.swift @@ -1,10 +1,24 @@ +struct GutenbergEvent { + let name: String + let body: Any? +} + @objc (RNReactNativeGutenbergBridge) public class RNReactNativeGutenbergBridge: RCTEventEmitter { weak var delegate: GutenbergBridgeDelegate? weak var dataSource: GutenbergBridgeDataSource? private var isJSLoading = true private var hasObservers = false + private var queuedEvents = [GutenbergEvent]() + public override init() { + super.init() + NotificationCenter.default.addObserver(forName: .RCTContentDidAppear, object: nil, queue: nil) { (_) in + DispatchQueue.main.async { + self.connectionEstablished() + } + } + } // MARK: - Messaging methods @objc @@ -88,7 +102,9 @@ public class RNReactNativeGutenbergBridge: RCTEventEmitter { @objc func mediaUploadSync() { DispatchQueue.main.async { - self.delegate?.gutenbergDidRequestMediaUploadSync() + if self.hasObservers { + self.delegate?.gutenbergDidRequestMediaUploadSync() + } } } @@ -189,18 +205,28 @@ public class RNReactNativeGutenbergBridge: RCTEventEmitter { } } - private func shouldLog(with level: Int) -> Bool { - return level >= RCTGetLogThreshold().rawValue + public func connectionEstablished() { + guard !hasObservers else { return } // We have an established connection no need to continue. + hasObservers = true + while (self.queuedEvents.count > 0) { + let event = self.queuedEvents.removeFirst() + super.sendEvent(withName: event.name, body: event.body) // execute this on super as we want to avoid logic in self. + } } - override public func startObserving() { - super.startObserving() - hasObservers = true + public override func sendEvent(withName name: String, body: Any?) { + DispatchQueue.main.async { + if self.hasObservers && self.queuedEvents.count == 0 { + super.sendEvent(withName: name, body: body) + } else { + let event = GutenbergEvent(name: name, body: body) + self.queuedEvents.append(event) + } + } } - override public func stopObserving() { - super.stopObserving() - hasObservers = false + private func shouldLog(with level: Int) -> Bool { + return level >= RCTGetLogThreshold().rawValue } @objc