Skip to content

Commit

Permalink
Merge pull request #2366 from wordpress-mobile/issue/2325-bridge
Browse files Browse the repository at this point in the history
Queue Events for better media uploads
  • Loading branch information
Chip authored Jun 18, 2020
2 parents a6e1740 + 8f13252 commit 74f2bb2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
4 changes: 2 additions & 2 deletions react-native-gutenberg-bridge/ios/Gutenberg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -88,7 +102,9 @@ public class RNReactNativeGutenbergBridge: RCTEventEmitter {
@objc
func mediaUploadSync() {
DispatchQueue.main.async {
self.delegate?.gutenbergDidRequestMediaUploadSync()
if self.hasObservers {
self.delegate?.gutenbergDidRequestMediaUploadSync()
}
}
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 74f2bb2

Please sign in to comment.