Skip to content

Commit

Permalink
Update and remove should be non-optional to avoid escaping closures
Browse files Browse the repository at this point in the history
  • Loading branch information
terwanerik committed Sep 19, 2023
1 parent cde5289 commit 57f9519
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
4 changes: 2 additions & 2 deletions MMMArrayChanges.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Pod::Spec.new do |s|

s.name = "MMMArrayChanges"
s.version = "1.1.1"
s.version = "2.0.0"
s.summary = "Helps finding (UITableView-compatible) differences between two arrays possibly of different types"
s.description = s.summary
s.homepage = "https://github.com/mediamonks/MMMArrayChanges"
Expand All @@ -33,7 +33,7 @@ Pod::Spec.new do |s|
end

s.test_spec 'TestsSwift' do |test_spec|
test_spec.source_files = 'Tests/*.swift'
test_spec.source_files = 'Tests/**/*.swift'
end

s.default_subspec = 'ObjC', 'Swift'
Expand Down
16 changes: 7 additions & 9 deletions Sources/MMMArrayChanges/Array+diffUpdate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ extension Array {
elementId: (_ element: Element) -> ElementId,
sourceArray: [SourceElement], sourceElementId: (_ sourceElement: SourceElement) -> ElementId,
transform: (_ sourceElement: SourceElement) -> Element,
update: ((_ element: Element, _ sourceElement: SourceElement) -> Bool)? = nil,
remove: ((_ element: Element) -> Void)? = nil
update: ((_ element: Element, _ sourceElement: SourceElement) -> Bool),
remove: ((_ element: Element) -> Void)
) -> Bool {
// We just use the compact logic here, since that will behave the same with a
// non-optional transform closure.
Expand All @@ -88,8 +88,8 @@ extension Array {
elementId: (_ element: Element) -> ElementId,
sourceArray: [SourceElement], sourceElementId: (_ sourceElement: SourceElement) -> ElementId,
transform: (_ sourceElement: SourceElement) -> Element?,
update: ((_ element: Element, _ sourceElement: SourceElement) -> Bool)? = nil,
remove: ((_ element: Element) -> Void)? = nil
update: ((_ element: Element, _ sourceElement: SourceElement) -> Bool),
remove: ((_ element: Element) -> Void)
) -> Bool {

var changed = false
Expand All @@ -103,7 +103,7 @@ extension Array {
// According to our index the current array already has a matching element, so just keep it...
elementById.removeValue(forKey: id)
// ...possibly updating.
if update?(element, sourceElement) ?? false {
if update(element, sourceElement) {
// The update closure indicated that a change in the existing element should be counted
// alongside with removals, additions and moves.
changed = true
Expand Down Expand Up @@ -142,10 +142,8 @@ extension Array {
self = result

// IDs left in the index correspond to elements missing in the new array, so they have to me marked as gone.
if let remove = remove {
elementById.forEach { (_, element) in
remove(element)
}
elementById.forEach { (_, element) in
remove(element)
}
}

Expand Down
20 changes: 10 additions & 10 deletions Sources/MMMArrayChanges/MMMArrayChanges.swift
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ public class MMMArrayChanges: CustomStringConvertible, Equatable {

- Parameters:

- update: Optional closure that's called for every element in the array that was not added to update its contents.
- update: Closure that's called for every element in the array that was not added to update its contents.

- remove: Optional closure that's called for every removed element of the array.
- remove: Closure that's called for every removed element of the array.
Note that it should not try removing the corresponing element, it's only for your own book-keeping.

- transform: A closure that should be able to creat a new element of the array from the corresponding element
Expand All @@ -319,8 +319,8 @@ public class MMMArrayChanges: CustomStringConvertible, Equatable {
public static func byUpdatingArray<Element, SourceElement, ElementId: Hashable>(
_ array: inout [Element], elementId: (Element) -> ElementId,
sourceArray: [SourceElement], sourceElementId: (SourceElement) -> ElementId,
update: ((_ element: Element, _ oldIndex: Int, _ sourceElement: SourceElement, _ newIndex: Int) -> Bool)? = nil,
remove: ((_ element: Element, _ oldIndex: Int) -> Void)? = nil,
update: ((_ element: Element, _ oldIndex: Int, _ sourceElement: SourceElement, _ newIndex: Int) -> Bool),
remove: ((_ element: Element, _ oldIndex: Int) -> Void),
transform: (_ newElement: SourceElement, _ newIndex: Int) -> Element
) -> MMMArrayChanges {

Expand All @@ -342,7 +342,7 @@ public class MMMArrayChanges: CustomStringConvertible, Equatable {
// But let's check for item updates.
var updates: [Update] = []
for i in 0..<array.count {
if update?(array[i], i, sourceArray[i], i) ?? false {
if update(array[i], i, sourceArray[i], i) {
updates.append(.init(i, i))
}
}
Expand Down Expand Up @@ -404,7 +404,7 @@ public class MMMArrayChanges: CustomStringConvertible, Equatable {
if oldId == newId {

// The item is at its target position already, let's only check if the contents has updated.
if update?(oldItem, oldIndex, newItem, newIndex) ?? false {
if update(oldItem, oldIndex, newItem, newIndex) {
updates.append(.init(oldIndex, newIndex))
}

Expand Down Expand Up @@ -434,7 +434,7 @@ public class MMMArrayChanges: CustomStringConvertible, Equatable {
intermediate.insert(t, at: intermediateTargetIndex)

// And finally check if the item has content changes as well.
if update?(array[oldNewIndex], oldNewIndex, newItem, newIndex) ?? false {
if update(array[oldNewIndex], oldNewIndex, newItem, newIndex) {
// Yes, record an update, too.
updates.append(.init(oldNewIndex, newIndex))
}
Expand All @@ -446,7 +446,7 @@ public class MMMArrayChanges: CustomStringConvertible, Equatable {
for r in removals {
let item = array[r.index]
array.remove(at: r.index)
remove?(item, r.index)
remove(item, r.index)
}

for m in moves {
Expand All @@ -471,8 +471,8 @@ public class MMMArrayChanges: CustomStringConvertible, Equatable {
public static func byUpdatingArrayOfObjects<Element: AnyObject>(
_ array: inout [Element],
sourceArray: [Element],
update: ((_ element: Element, _ oldIndex: Int, _ sourceElement: Element, _ newIndex: Int) -> Bool)? = nil,
remove: ((_ element: Element, _ oldIndex: Int) -> Void)? = nil
update: ((_ element: Element, _ oldIndex: Int, _ sourceElement: Element, _ newIndex: Int) -> Bool),
remove: ((_ element: Element, _ oldIndex: Int) -> Void)
) -> MMMArrayChanges {
let result = byUpdatingArray(
&array,
Expand Down

0 comments on commit 57f9519

Please sign in to comment.