-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Queue changes while reorder events are settling; so that mutative upd…
…ates after the edit do not cause crashes within UICollectionView; it does not account for mutations while a reorder is settling.
- Loading branch information
Showing
8 changed files
with
259 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// | ||
// ListChangesQueue.swift | ||
// ListableUI | ||
// | ||
// Created by Kyle Van Essen on 7/19/21. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
/// Used to queue updates into a list view. | ||
/// Note: This type is only safe to use from the main thread. | ||
final class ListChangesQueue { | ||
|
||
/// Adds a synchronous block to the queue, marked as done once the block exits. | ||
func add(_ block : @escaping () -> ()) { | ||
self.waiting.append(.init(block)) | ||
self.runIfNeeded() | ||
} | ||
|
||
/// Set by consumers to enable and disable queueing during a reorder event. | ||
var isQueuingForReorderEvent : Bool = false { | ||
didSet { | ||
self.runIfNeeded() | ||
} | ||
} | ||
|
||
/// Prevents processing other events in the queue. | ||
/// | ||
/// Note: Right now this just checks `isQueuingForReorderEvent`, but may check more props in the future. | ||
var isPaused : Bool { | ||
self.isQueuingForReorderEvent | ||
} | ||
|
||
/// Operations waiting to execute. | ||
private(set) var waiting : [Operation] = [] | ||
|
||
/// Invoked to continue processing queue events. | ||
private func runIfNeeded() { | ||
precondition(Thread.isMainThread) | ||
|
||
/// Nothing to do if we're currently paused! | ||
guard self.isPaused == false else { | ||
return | ||
} | ||
|
||
while let next = self.waiting.popFirst() { | ||
autoreleasepool { | ||
guard case .new(let new) = next.state else { | ||
fatalError("State of enqueued operation was wrong") | ||
} | ||
|
||
/// Ok, we have a runnable operation; let's run it. | ||
|
||
next.state = .done | ||
|
||
new.body() | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
extension ListChangesQueue { | ||
|
||
final class Operation { | ||
|
||
fileprivate(set) var state : State | ||
|
||
init(_ body : @escaping () -> ()) { | ||
self.state = .new(.init(body: body)) | ||
} | ||
|
||
enum State { | ||
case new(New) | ||
case done | ||
|
||
struct New { | ||
let body : () -> () | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
fileprivate extension Array { | ||
|
||
mutating func popFirst() -> Element? { | ||
guard self.isEmpty == false else { | ||
return nil | ||
} | ||
|
||
let first = self[0] | ||
|
||
self.remove(at: 0) | ||
|
||
return first | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// ListChangesQueueTests.swift | ||
// ListableUI-Unit-Tests | ||
// | ||
// Created by Kyle Van Essen on 7/24/21. | ||
// | ||
|
||
import XCTest | ||
@testable import ListableUI | ||
|
||
|
||
class ListChangesQueueTests : XCTestCase { | ||
|
||
func test_queue() { | ||
|
||
let queue = ListChangesQueue() | ||
|
||
XCTAssertFalse(queue.isPaused) | ||
XCTAssertFalse(queue.isQueuingForReorderEvent) | ||
|
||
var calls : [Int] = [] | ||
|
||
queue.add { | ||
calls += [1] | ||
} | ||
|
||
XCTAssertEqual(queue.waiting.count, 0) | ||
XCTAssertEqual(calls, [1]) | ||
|
||
queue.isQueuingForReorderEvent = true | ||
|
||
XCTAssertTrue(queue.isPaused) | ||
XCTAssertTrue(queue.isQueuingForReorderEvent) | ||
|
||
queue.add { | ||
calls += [2] | ||
} | ||
|
||
queue.add { | ||
calls += [3] | ||
} | ||
|
||
XCTAssertEqual(queue.waiting.count, 2) | ||
XCTAssertEqual(calls, [1]) | ||
|
||
queue.isQueuingForReorderEvent = false | ||
|
||
XCTAssertFalse(queue.isPaused) | ||
XCTAssertFalse(queue.isQueuingForReorderEvent) | ||
|
||
XCTAssertEqual(calls, [1, 2, 3]) | ||
} | ||
} |