diff --git a/MVMCoreUI/Atomic/Protocols/ModelProtocols/ParentMoleculeModelProtocol.swift b/MVMCoreUI/Atomic/Protocols/ModelProtocols/ParentMoleculeModelProtocol.swift index a9f4f8ac..ef53b177 100644 --- a/MVMCoreUI/Atomic/Protocols/ModelProtocols/ParentMoleculeModelProtocol.swift +++ b/MVMCoreUI/Atomic/Protocols/ModelProtocols/ParentMoleculeModelProtocol.swift @@ -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. } diff --git a/MVMCoreUI/Behaviors/ReplaceableMoleculeBehaviorModel.swift b/MVMCoreUI/Behaviors/ReplaceableMoleculeBehaviorModel.swift index 0b4c0516..24e3b532 100644 --- a/MVMCoreUI/Behaviors/ReplaceableMoleculeBehaviorModel.swift +++ b/MVMCoreUI/Behaviors/ReplaceableMoleculeBehaviorModel.swift @@ -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 {