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

View File

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