Fix first time carousel replacement.

This commit is contained in:
Hedden, Kyle Matthew 2023-12-18 17:19:52 -05:00
parent 0ecfef39cd
commit df32a51395
2 changed files with 19 additions and 9 deletions

View File

@ -10,6 +10,7 @@ import Foundation
public protocol ParentModelProtocol: MoleculeTreeTraversalProtocol {
/// Returns the direct children of this component. (Does not recurse.)
var children: [MoleculeModelProtocol] { get }
/// Method for replacing surface level children. (Does not recurse.)
@ -73,22 +74,26 @@ public extension ParentMoleculeModelProtocol {
}
func depthFirstTraverse(options: TreeTraversalOptions, depth: Int, onVisit: (Int, MoleculeModelProtocol, inout Bool)->Void) {
var stop = false
var shouldStop = false
if (options == .parentFirst) {
onVisit(depth, self, &stop)
guard !stop else { return }
onVisit(depth, self, &shouldStop)
guard !shouldStop else { return }
}
let stopIntercept: (Int, MoleculeModelProtocol, inout Bool)->Void = { depth, model, stop in
onVisit(depth, model, &shouldStop)
stop = shouldStop
}
for child in children {
if let additionalParent = child as? ParentMoleculeModelProtocol {
// Safety net to make sure the ParentMoleculeModelProtocol's method extension is called over the base MoleculeModelProtocol.
additionalParent.depthFirstTraverse(options: options, depth: depth + 1, onVisit: onVisit)
additionalParent.depthFirstTraverse(options: options, depth: depth + 1, onVisit: stopIntercept)
} else {
child.depthFirstTraverse(options: options, depth: depth + 1, onVisit: onVisit)
child.depthFirstTraverse(options: options, depth: depth + 1, onVisit: stopIntercept)
}
guard !stop else { return }
guard !shouldStop else { return }
}
if (options == .childFirst) {
onVisit(depth, self, &stop)
onVisit(depth, self, &shouldStop)
}
// if options == .leafOnly don't call on self.
}

View File

@ -61,7 +61,9 @@ public class ReplaceableMoleculeBehavior: PageMoleculeTransformationBehavior {
return nil
}
}
delegateObject?.moleculeDelegate?.replaceMoleculeData(moleculeModels)
if moleculeModels.count > 0 {
delegateObject?.moleculeDelegate?.replaceMoleculeData(moleculeModels)
}
}
private func listenForModuleUpdates() {
@ -75,6 +77,7 @@ public class ReplaceableMoleculeBehavior: PageMoleculeTransformationBehavior {
private func stopListeningForModuleUpdates() {
guard let observingForResponses = observingForResponses else { return }
NotificationCenter.default.removeObserver(observingForResponses)
self.observingForResponses = nil
}
@objc func responseJSONUpdated(notification: Notification) {
@ -92,7 +95,9 @@ public class ReplaceableMoleculeBehavior: PageMoleculeTransformationBehavior {
return nil
}
}
delegateObject?.moleculeDelegate?.replaceMoleculeData(modules)
if modules.count > 0 {
delegateObject?.moleculeDelegate?.replaceMoleculeData(modules)
}
}
private func convertToModel(moduleJSON: [String: Any]) throws -> MoleculeModelProtocol {