|
| 1 | +// |
| 2 | +// The MIT License (MIT) |
| 3 | +// |
| 4 | +// Copyright (c) 2018 DeclarativeHub/Bond |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in |
| 14 | +// all copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +// THE SOFTWARE. |
| 23 | +// |
| 24 | + |
| 25 | +import Foundation |
| 26 | +import ReactiveKit |
| 27 | +import Differ |
| 28 | + |
| 29 | +//extension ArrayBasedDiff where Index == IndexPath { |
| 30 | +// |
| 31 | +// public init(from diff: ExtendedDiff, rootIndex: IndexPath) { |
| 32 | +// self.init() |
| 33 | +// for element in diff.elements { |
| 34 | +// switch element { |
| 35 | +// case .insert(let at): |
| 36 | +// inserts.append(rootIndex.appending(at)) |
| 37 | +// case .delete(let at): |
| 38 | +// deletes.append(rootIndex.appending(at)) |
| 39 | +// case .move(let from, let to): |
| 40 | +// moves.append((from: rootIndex.appending(from), to: rootIndex.appending(to))) |
| 41 | +// } |
| 42 | +// } |
| 43 | +// } |
| 44 | +//} |
| 45 | +// |
| 46 | +//extension ArrayBasedTreeNode where ChildNode: ArrayBasedTreeNode { |
| 47 | +// |
| 48 | +// public func treeDiff(_ other: Self, rootIndex: IndexPath = [], areRootsEqual: @escaping (Value, Value) -> Bool, areChildrenEqual: @escaping (ChildNode.Value, ChildNode.Value) -> Bool) -> TreeChangeset<Self>.Diff { |
| 49 | +// |
| 50 | +// // Check roots |
| 51 | +// if rootIndex.isEmpty && !areRootsEqual(value, other.value) { |
| 52 | +// return TreeChangeset<Self>.Diff(updates: [rootIndex]) |
| 53 | +// } |
| 54 | +// |
| 55 | +// let isEqual: (ChildNode, ChildNode) -> Bool = { a, b in areChildrenEqual(a.value, b.value) } |
| 56 | +// let traces = children.outputDiffPathTraces(to: other.children, isEqual: isEqual) |
| 57 | +// let levelDiff = Differ.Diff(traces: traces) |
| 58 | +// let levelExtendedDiff = children.extendedDiff(from: levelDiff, other: other.children, isEqual: isEqual) |
| 59 | +// |
| 60 | +// var diff = TreeChangeset<Self>.Diff(from: levelExtendedDiff, rootIndex: rootIndex) |
| 61 | +// |
| 62 | +// let matchingLevelTraces = traces.filter { trace in |
| 63 | +// return trace.from.x + 1 == trace.to.x && trace.from.y + 1 == trace.to.y // Differ matchPoint |
| 64 | +// } |
| 65 | +// |
| 66 | +// for trace in matchingLevelTraces { |
| 67 | +// let sourceChild = children[trace.from.x] |
| 68 | +// let destinationChild = other.children[trace.from.y] |
| 69 | +// let diffRootIndex = rootIndex + [trace.from.y] |
| 70 | +// let childDiff = sourceChild.treeDiff( |
| 71 | +// destinationChild, |
| 72 | +// rootIndex: diffRootIndex, |
| 73 | +// areRootsEqual: areChildrenEqual, |
| 74 | +// areChildrenEqual: areChildrenEqual |
| 75 | +// ) |
| 76 | +// let childPatch = childDiff.generatePatch(to: sourceChild).map { $0.asAnyArrayBasedOperation } |
| 77 | +// diff = ArrayBasedDiff<IndexPath>(from: diff.generatePatch(to: self).map { $0.asAnyArrayBasedOperation } + childPatch) |
| 78 | +// } |
| 79 | +// |
| 80 | +// return diff |
| 81 | +// } |
| 82 | +//} |
| 83 | +// |
| 84 | +//extension ArrayBasedTreeNode where ChildNode: ArrayBasedTreeNode, ChildNode.Value: Equatable, Value: Equatable { |
| 85 | +// |
| 86 | +// /// Diff the receiver against the given tree. |
| 87 | +// public func treeDiff(_ other: Self) -> TreeChangeset<Self>.Diff { |
| 88 | +// return treeDiff(other, areRootsEqual: { $0 == $1 }, areChildrenEqual: { $0 == $1 }) |
| 89 | +// } |
| 90 | +//} |
| 91 | +// |
| 92 | +//extension ArrayBasedTreeNode where ChildNode: ArrayBasedTreeNode,ChildNode.Value: Equatable, Value == Void { |
| 93 | +// |
| 94 | +// /// Diff the receiver against the given tree. |
| 95 | +// public func treeDiff(_ other: Self) -> TreeChangeset<Self>.Diff { |
| 96 | +// return treeDiff(other, areRootsEqual: { _, _ in true }, areChildrenEqual: { $0 == $1 }) |
| 97 | +// } |
| 98 | +//} |
| 99 | +// |
| 100 | +//extension SignalProtocol where Element: ArrayBasedTreeNode, Element.ChildNode: ArrayBasedTreeNode { |
| 101 | +// |
| 102 | +// /// Diff each next element (tree) against the previous one and emit a diff event. |
| 103 | +// public func diff(areRootsEqual: @escaping (Element.Value, Element.Value) -> Bool, areChildrenEqual: @escaping (Element.ChildNode.Value, Element.ChildNode.Value) -> Bool) -> Signal<TreeChangeset<Element>, Error> { |
| 104 | +// return diff(generateDiff: { $0.treeDiff($1, areRootsEqual: areRootsEqual, areChildrenEqual: areChildrenEqual) }) |
| 105 | +// } |
| 106 | +//} |
| 107 | +// |
| 108 | +//extension SignalProtocol where Element: ArrayBasedTreeNode, Element.ChildNode: ArrayBasedTreeNode, Element.Value == Element.ChildNode.Value { |
| 109 | +// |
| 110 | +// /// Diff each next element (tree) against the previous one and emit a diff event. |
| 111 | +// public func diff(_ areEqual: @escaping (Element.Value, Element.Value) -> Bool) -> Signal<TreeChangeset<Element>, Error> { |
| 112 | +// return diff(generateDiff: { $0.treeDiff($1, areRootsEqual: areEqual, areChildrenEqual: areEqual) }) |
| 113 | +// } |
| 114 | +//} |
| 115 | +// |
| 116 | +//extension SignalProtocol where Element: ArrayBasedTreeNode, Element.ChildNode: ArrayBasedTreeNode, Element.ChildNode.Value: Equatable, Element.Value: Equatable { |
| 117 | +// |
| 118 | +// /// Diff each next element (tree) against the previous one and emit a diff event. |
| 119 | +// public func diff() -> Signal<TreeChangeset<Element>, Error> { |
| 120 | +// return diff(generateDiff: { $0.treeDiff($1) }) |
| 121 | +// } |
| 122 | +//} |
| 123 | +// |
| 124 | +//extension SignalProtocol where Element: ArrayBasedTreeNode, Element.ChildNode: ArrayBasedTreeNode, Element.ChildNode.Value: Equatable, Element.Value == Void { |
| 125 | +// |
| 126 | +// /// Diff each next element (tree) against the previous one and emit a diff event. |
| 127 | +// public func diff() -> Signal<TreeChangeset<Element>, Error> { |
| 128 | +// return diff(generateDiff: { $0.treeDiff($1) }) |
| 129 | +// } |
| 130 | +//} |
0 commit comments