Digital PCT265 defect CXTDT-579050: Change batching to the UI.

This commit is contained in:
Hedden, Kyle Matthew 2024-07-03 15:42:12 -04:00
parent d8d4b37d1d
commit 4cbf15a3a7

View File

@ -70,9 +70,47 @@ import Combine
observingForResponses = NotificationCenter.default.publisher(for: NSNotification.Name(rawValue: NotificationResponseLoaded)) observingForResponses = NotificationCenter.default.publisher(for: NSNotification.Name(rawValue: NotificationResponseLoaded))
.receive(on: self.pageUpdateQueue) // Background serial queue. .receive(on: self.pageUpdateQueue) // Background serial queue.
.map { [weak self] notification in .map { [weak self] notification in
guard let self = self else { return (nil, nil, nil) } self?.pullUpdates(from: notification) ?? (nil, nil, nil)
}
.filter { (pageUpdates: [String : Any]?, pageModel: PageModelProtocol?, moduleUpdates: [String : Any]?) in
// Skip any non-updates.
(pageUpdates != nil && pageModel != nil) || (moduleUpdates != nil && moduleUpdates!.count > 0)
}
// Merge all page and module updates into one update event.
//.print("[Update pipe] merging")
.scan((nil, nil, nil)) { accumulator, next in
// Always take the latest page and the latest modules with same key.
return (next.0, next.1, next.2?.mergingRight(accumulator.2 ?? [:]))
}
//.print("[Update pipe] into buffer")
// Hold onto the latest merged state until UI is ready for an update. Keep only the latest from scan.
.buffer(size: 1, prefetch: .byRequest, whenFull: .dropOldest)
//.print("[Update pipe] out of buffer")
// Delay allowing the previous model update to settle before triggering a re-render.
.flatMap(maxPublishers: .max(1)) { buffer in
Just(buffer).delay(for: .seconds(0.1), scheduler: RunLoop.main)
}
.sink { [weak self] (pageUpdates: [String : Any]?, pageModel: PageModelProtocol?, moduleUpdates: [String : Any]?) in
guard let self = self else { return }
if let pageUpdates, pageModel != nil {
self.loadObject?.pageJSON = pageUpdates
}
let mergedModuleUpdates = (loadObject?.modulesJSON ?? [:]).mergingLeft(moduleUpdates ?? [:])
self.loadObject?.modulesJSON = mergedModuleUpdates
self.debugLog("Applying async update page model \(pageModel.debugDescription) and modules \(mergedModuleUpdates.keys) to page.")
self.handleNewData(pageModel)
}
}
open func stopObservingForResponseJSONUpdates() {
guard let observingForResponses = observingForResponses else { return }
NotificationCenter.default.removeObserver(observingForResponses)
self.observingForResponses = nil
}
func pullUpdates(from notification: Notification) -> (pageUpdates: [String : Any]?, pageModel: PageModelProtocol?, moduleUpdates: [String : Any]?) {
// Get the page data. // Get the page data.
let pageUpdates = self.extractInterestedPageType(from: notification.userInfo?.optionalDictionaryForKey(KeyPageMap) ?? [:]) let pageUpdates = extractInterestedPageType(from: notification.userInfo?.optionalDictionaryForKey(KeyPageMap) ?? [:])
// Convert the page data into a new model. // Convert the page data into a new model.
var pageModel: PageModelProtocol? = nil var pageModel: PageModelProtocol? = nil
if let pageUpdates { if let pageUpdates {
@ -86,34 +124,11 @@ import Combine
} }
} }
// Get the module data. // Get the module data.
let moduleUpdates = self.extractInterestedModules(from: notification.userInfo?.optionalDictionaryForKey(KeyModuleMap) ?? [:]) let moduleUpdates = extractInterestedModules(from: notification.userInfo?.optionalDictionaryForKey(KeyModuleMap) ?? [:])
debugLog("Receiving page \(pageModel?.pageType ?? "none") & \(moduleUpdates?.keys.description ?? "none") modules from \((notification.userInfo?["MVMCoreLoadObject"] as? MVMCoreLoadObject)?.requestParameters?.url?.absoluteString ?? "")")
// Bundle the transformations. // Bundle the transformations.
return (pageUpdates, pageModel, moduleUpdates) return (pageUpdates, pageModel, moduleUpdates)
} }
.filter { (pageUpdates: [String : Any]?, pageModel: PageModelProtocol?, moduleUpdates: [String : Any]?) in
// Skip any non-updates.
(pageUpdates != nil && pageModel != nil) || (moduleUpdates != nil && moduleUpdates!.count > 0)
}
// Opportunity: Merge all module only updates into one event.
// Delay allowing the previous model update to settle before triggering a re-render.
.buffer(size: 100, prefetch: .byRequest, whenFull: .dropOldest)
.flatMap(maxPublishers: .max(1)) { Just($0).delay(for: .seconds(0.1), scheduler: RunLoop.main) }
.sink { [weak self] (pageUpdates: [String : Any]?, pageModel: PageModelProtocol?, moduleUpdates: [String : Any]?) in
guard let self = self else { return }
if let pageUpdates, let pageModel {
self.loadObject?.pageJSON = pageUpdates
}
var mergedModuleUpdates = (loadObject?.modulesJSON ?? [:]).mergingLeft(moduleUpdates ?? [:])
self.loadObject?.modulesJSON = mergedModuleUpdates
self.handleNewData(pageModel)
}
}
open func stopObservingForResponseJSONUpdates() {
guard let observingForResponses = observingForResponses else { return }
NotificationCenter.default.removeObserver(observingForResponses)
self.observingForResponses = nil
}
open func pagesToListenFor() -> [String]? { open func pagesToListenFor() -> [String]? {
guard let pageType = loadObject?.pageType else { return nil } guard let pageType = loadObject?.pageType else { return nil }