Skip to content

Commit

Permalink
Converting the API to be in the Swift 3 style (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
plivesey authored Sep 20, 2016
1 parent 57b24c4 commit e2f7d36
Show file tree
Hide file tree
Showing 33 changed files with 994 additions and 2,074 deletions.
34 changes: 17 additions & 17 deletions ConsistencyManager/ConsistencyManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ import Foundation
The two important APIs that you will mainly use in this class are:
`listenForUpdates(listener: ConsistencyManagerListener)`
`updateWithNewModel(model: ConsistencyManagerModel, context: Any? = nil)`
`addListener(listener: ConsistencyManagerListener)`
`updateModel(model: ConsistencyManagerModel, context: Any? = nil)`
These APIs allow you to start listening for updates on a model and register new updates.
Anytime you change a model locally, you should call updateWithNewModel to propegate these changes.
Anytime you change a model locally, you should call updateModel to propegate these changes.
Additionally you have the following APIs to use if you choose to
have your listener temporarily pause (and later resume) listening to updates:
`pauseListeningForUpdates(listener: ConsistencyManagerListener)`
`resumeListeningForUpdates(listener: ConsistencyManagerListener)`
`pauseListener(listener: ConsistencyManagerListener)`
`resumeListener(listener: ConsistencyManagerListener)`
#### Removing Listeners
Expand Down Expand Up @@ -140,23 +140,23 @@ open class ConsistencyManager {
Note that calling this method on a paused listener will not unpause it.
- parameter listener: The consistency manager listener that is listening to a model
*/
open func listenForUpdates(_ listener: ConsistencyManagerListener) {
open func addListener(_ listener: ConsistencyManagerListener) {
let model = listener.currentModel()
if let model = model {
listenForUpdates(listener, onModel: model)
addListener(listener, to: model)
}
// Else they are listening to nothing. Let's not remove them though, since we are on a different thread, so timing issues could cause bugs.
}

/**
Call this method if you want to listen to a specific model. Usually, this is unnecssary and you should just use listenForUpdates(listener).
Call this method if you want to listen to a specific model. Usually, this is unnecssary and you should just use `addListener(listener)`.
This is necessary if you manually update a model and change only part of it.
Note that calling this method on a paused listener will not unpause it.
For a performance optimization, you may only want to add yourself as a listener for this new change (and not the whole model again).
- parameter listener: the consistency manager
- parameter onModel: the model you want to listen to with this listener
- parameter model: the model you want to listen to with this listener
*/
open func listenForUpdates(_ listener: ConsistencyManagerListener, onModel model: ConsistencyManagerModel) {
open func addListener(_ listener: ConsistencyManagerListener, to model: ConsistencyManagerModel) {
dispatchTask { _ in
self.addListener(listener, recursivelyToChildModels: model)
}
Expand Down Expand Up @@ -198,7 +198,7 @@ open class ConsistencyManager {
// MARK: Pausing and Resuming Listening to Updates

/**
Temporarily ignore any updates on the current model. Use removeListener(listener: ConsistencyManagerListener) instead if you
Temporarily ignore any updates on the current model. Use `removeListener(listener: ConsistencyManagerListener)` instead if you
know that you will not ever need to resume listening to updates.
Once you start listening again, you will get all the changes that you missed via the modelUpdated delegate method
with the most updated model at that point, and you will get the most recent context (only) as well.
Expand All @@ -211,8 +211,8 @@ open class ConsistencyManager {
This should only be called on the main thread.
- parameter listener: The consistency manager listener that is currently listening to a model
*/
open func pauseListeningForUpdates(_ listener: ConsistencyManagerListener) {
if !isPaused(listener) {
open func pauseListener(_ listener: ConsistencyManagerListener) {
if !isListenerPaused(listener) {
let pausedListener = PausedListener(listener: listener, updatedModel: listener.currentModel(), mostRecentContext: nil, modelUpdates: ModelUpdates(changedModelIds: [], deletedModelIds: []))
pausedListeners.append(pausedListener)
}
Expand All @@ -226,9 +226,9 @@ open class ConsistencyManager {
This should only be called on the main thread.
- parameter listener: The consistency manager listener that is currently not listening
(i.e. has most recently called the pauseListeningForUpdates method) to a model
(i.e. has most recently called the pauseListener method) to a model
*/
open func resumeListeningForUpdates(_ listener: ConsistencyManagerListener) {
open func resumeListener(_ listener: ConsistencyManagerListener) {
guard let index = pausedListeners.index(where: { listener === $0.listener }) else {
return
}
Expand Down Expand Up @@ -295,7 +295,7 @@ open class ConsistencyManager {
- parameter listener: The listener to query the paused state of.
*/
open func isPaused(_ listener: ConsistencyManagerListener) -> Bool {
open func isListenerPaused(_ listener: ConsistencyManagerListener) -> Bool {
return pausedListeners.contains { listener === $0.listener }
}

Expand All @@ -308,7 +308,7 @@ open class ConsistencyManager {
- parameter model: the model with which you want to update the consistency manager
- parameter context: any context parameter, to be passed on to each listener in the delegate method
*/
open func updateWithNewModel(_ model: ConsistencyManagerModel, context: Any? = nil) {
open func updateModel(_ model: ConsistencyManagerModel, context: Any? = nil) {
dispatchTask { cancelled in
let tuple = self.childrenAndListenersForModel(model)
let optionalModelUpdates = CollectionHelpers.optionalValueDictionaryFromDictionary(tuple.modelUpdates)
Expand Down
14 changes: 7 additions & 7 deletions ConsistencyManager/DataStructures/BatchListener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Foundation
### SETUP
You should NOT call listenForUpdates on any of the listeners that you pass into this class. Instead, you should call it directly on the instance of this class.
You should NOT call addListener on any of the listeners that you pass into this class. Instead, you should call it directly on the instance of this class.
This causes the instance of this class to listen to each of the models of the listeners.
Any time you manually change a model on one of the listeners, you need to call listenerHasUpdatedModel.
*/
Expand All @@ -35,23 +35,23 @@ open class BatchListener: ConsistencyManagerListener {
/// Listening to all models occurs immediately upon initialization of the BatchListener object
public init(listeners: [ConsistencyManagerListener], consistencyManager: ConsistencyManager) {
self.listeners = listeners
listenForUpdates(consistencyManager)
addListener(consistencyManager)
}

/**
Instead of calling listenForUpdates on each of the child listeners, you should call this method.
Instead of calling addListener on each of the child listeners, you should call this method.
You should also call it whenever you manually change any of the sublisteners.
*/
open func listenForUpdates(_ consistencyManager: ConsistencyManager) {
consistencyManager.listenForUpdates(self)
open func addListener(_ consistencyManager: ConsistencyManager) {
consistencyManager.addListener(self)
}

/**
Whenever you manually change a model on a listener, you must call this method to let the batch listener know.
*/
open func listenerHasUpdatedModel(_ listener: ConsistencyManagerListener, consistencyManager: ConsistencyManager) {
if let model = listener.currentModel() {
consistencyManager.listenForUpdates(self, onModel: model)
consistencyManager.addListener(self, to: model)
}
// else the model nil, so we don't have to listen to anything new.
}
Expand All @@ -63,7 +63,7 @@ open class BatchListener: ConsistencyManagerListener {
- parameter consistencyManager: The consistency manager you are using to listen to these changes.
*/
open func listenerHasUpdatedModel(_ model: ConsistencyManagerModel, consistencyManager: ConsistencyManager) {
consistencyManager.listenForUpdates(self, onModel: model)
consistencyManager.addListener(self, to: model)
}

// MARK: Consistency Manager Implementation
Expand Down
6 changes: 3 additions & 3 deletions ConsistencyManagerTests/BatchListenerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class BatchListenerTest: ConsistencyManagerTestCase {
XCTAssertEqual(context as? String, "context")
}

updateWithNewModel(updateModel, consistencyManager: consistencyManager, context: "context")
updateNewModel(updateModel, consistencyManager: consistencyManager, context: "context")

XCTAssertEqual(calledUpdateClosure, 1)
XCTAssertEqual(calledListenerUpdateClosure, 1)
Expand Down Expand Up @@ -97,7 +97,7 @@ class BatchListenerTest: ConsistencyManagerTestCase {
XCTFail()
}

updateWithNewModel(updateModel, consistencyManager: consistencyManager, context: "context")
updateNewModel(updateModel, consistencyManager: consistencyManager, context: "context")

XCTAssertEqual(calledUpdateClosure, 1)
XCTAssertEqual(calledListenerUpdateClosure, 1)
Expand Down Expand Up @@ -155,7 +155,7 @@ class BatchListenerTest: ConsistencyManagerTestCase {
XCTAssertTrue(updates.changedModelIds.contains("1"))
}

updateWithNewModel(updateModel, consistencyManager: consistencyManager, context: "context")
updateNewModel(updateModel, consistencyManager: consistencyManager, context: "context")

XCTAssertEqual(calledUpdateClosure, 1)
XCTAssertEqual(calledListenerUpdateClosure, 1)
Expand Down
2 changes: 1 addition & 1 deletion ConsistencyManagerTests/BatchUpdateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class BatchUpdateTests: ConsistencyManagerTestCase {
let updateModel1 = TestModel(id: "2", data: -2, children: [], requiredModel: TestRequiredModel(id: "21", data: -1))
let updateModel2 = TestModel(id: "4", data: -4, children: [], requiredModel: TestRequiredModel(id: "21", data: -1))
let batchModel = BatchUpdateModel(models: [updateModel1, updateModel2])
updateWithNewModel(batchModel, consistencyManager: consistencyManager)
updateNewModel(batchModel, consistencyManager: consistencyManager)

// Both models should have been updated, but we should only have gotten one update
if let testModel = listener.model as? TestModel {
Expand Down
6 changes: 3 additions & 3 deletions ConsistencyManagerTests/ClearAndCancelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ClearAndCancelTests: ConsistencyManagerTestCase {

addListener(listener, toConsistencyManager: consistencyManager)
addListener(pausedListener, toConsistencyManager: consistencyManager)
consistencyManager.pauseListeningForUpdates(pausedListener)
consistencyManager.pauseListener(pausedListener)

let expectation = self.expectation(description: "Wait for clear to complete")
consistencyManager.clearListenersAndCancelAllTasks {
Expand All @@ -47,7 +47,7 @@ class ClearAndCancelTests: ConsistencyManagerTestCase {
}

let updateModel = TestRequiredModel(id: "0", data: 1)
consistencyManager.updateWithNewModel(updateModel)
consistencyManager.updateModel(updateModel)

let expectation = self.expectation(description: "Wait for clear to complete")
consistencyManager.clearListenersAndCancelAllTasks {
Expand Down Expand Up @@ -87,7 +87,7 @@ class ClearAndCancelTests: ConsistencyManagerTestCase {
}

let updateModel = TestRequiredModel(id: "0", data: 1)
consistencyManager.updateWithNewModel(updateModel)
consistencyManager.updateModel(updateModel)

waitForExpectations(timeout: 10, handler: nil)

Expand Down
2 changes: 1 addition & 1 deletion ConsistencyManagerTests/ContextTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ContextTests: ConsistencyManagerTestCase {
XCTAssertEqual(context as? Int, 4)
}

updateWithNewModel(updateModel, consistencyManager: consistencyManager, context: 4)
updateNewModel(updateModel, consistencyManager: consistencyManager, context: 4)

XCTAssertTrue(contextClosureCalled)
}
Expand Down
2 changes: 1 addition & 1 deletion ConsistencyManagerTests/ErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ErrorTests: ConsistencyManagerTestCase, ConsistencyManagerDelegate {
let listener = TestListener(model: model)
addListener(listener, toConsistencyManager: consistencyManager)

updateWithNewModel(TestRequiredModel(id: "1", data: 1), consistencyManager: consistencyManager)
updateNewModel(TestRequiredModel(id: "1", data: 1), consistencyManager: consistencyManager)

// Make sure the error got called
if let error = error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ConsistencyManagerTestCase: XCTestCase {
}

func addListener(_ listener: ConsistencyManagerListener, toConsistencyManager consistencyManager: ConsistencyManager) {
consistencyManager.listenForUpdates(listener)
consistencyManager.addListener(listener)

waitOnDispatchQueue(consistencyManager)
}
Expand All @@ -53,11 +53,11 @@ class ConsistencyManagerTestCase: XCTestCase {
waitOnDispatchQueue(consistencyManager)
}

func updateWithNewModel(_ model: ConsistencyManagerModel, consistencyManager: ConsistencyManager, context: Any? = nil) {
consistencyManager.updateWithNewModel(model, context: context)
func updateNewModel(_ model: ConsistencyManagerModel, consistencyManager: ConsistencyManager, context: Any? = nil, timeout: TimeInterval = 10) {
consistencyManager.updateModel(model, context: context)

// First we need to wait for the consistency manager to finish on its queue
waitOnDispatchQueue(consistencyManager)
waitOnDispatchQueue(consistencyManager, timeout: timeout)

// Now, we need to wait for the main queue to do the actual updates
flushMainQueueOperations()
Expand Down Expand Up @@ -89,13 +89,13 @@ class ConsistencyManagerTestCase: XCTestCase {
flushMainQueueOperations()
}

func pauseListeningForUpdates(_ listener: ConsistencyManagerListener, consistencyManager: ConsistencyManager) {
func pauseListener(_ listener: ConsistencyManagerListener, consistencyManager: ConsistencyManager) {
// This is synchronous so no wait is necessary here. This is just for readability and consistency with resume.
consistencyManager.pauseListeningForUpdates(listener)
consistencyManager.pauseListener(listener)
}

func resumeListeningForUpdates(_ listener: ConsistencyManagerListener, consistencyManager: ConsistencyManager) {
consistencyManager.resumeListeningForUpdates(listener)
func resumeListener(_ listener: ConsistencyManagerListener, consistencyManager: ConsistencyManager) {
consistencyManager.resumeListener(listener)

// First we need to wait for the consistency manager to finish on its queue
waitOnDispatchQueue(consistencyManager)
Expand All @@ -104,15 +104,15 @@ class ConsistencyManagerTestCase: XCTestCase {
flushMainQueueOperations()
}

func waitOnDispatchQueue(_ consistencyManager: ConsistencyManager) {
func waitOnDispatchQueue(_ consistencyManager: ConsistencyManager, timeout: TimeInterval = 10) {
let expectation = self.expectation(description: "Wait for consistency manager to update internal state")

let operation = BlockOperation() {
expectation.fulfill()
}
consistencyManager.queue.addOperation(operation)

waitForExpectations(timeout: 10) { error in
waitForExpectations(timeout: timeout) { error in
XCTAssertNil(error)
}
}
Expand Down
18 changes: 9 additions & 9 deletions ConsistencyManagerTests/ModelUpdatesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ModelUpdatesTests: ConsistencyManagerTestCase {
}
}

updateWithNewModel(updateModel, consistencyManager: consistencyManager)
updateNewModel(updateModel, consistencyManager: consistencyManager)

XCTAssertTrue(calledUpdateClosure)
}
Expand All @@ -63,7 +63,7 @@ class ModelUpdatesTests: ConsistencyManagerTestCase {
XCTAssertEqual(updates.deletedModelIds.count, 0)
}

updateWithNewModel(updateModel, consistencyManager: consistencyManager)
updateNewModel(updateModel, consistencyManager: consistencyManager)

XCTAssertTrue(calledUpdateClosure)
}
Expand All @@ -89,7 +89,7 @@ class ModelUpdatesTests: ConsistencyManagerTestCase {
XCTAssertEqual(updates.deletedModelIds.count, 0)
}

updateWithNewModel(updateModel, consistencyManager: consistencyManager)
updateNewModel(updateModel, consistencyManager: consistencyManager)

XCTAssertTrue(calledUpdateClosure)
}
Expand Down Expand Up @@ -117,7 +117,7 @@ class ModelUpdatesTests: ConsistencyManagerTestCase {
XCTAssertEqual(updates.deletedModelIds.count, 0)
}

updateWithNewModel(updateModel, consistencyManager: consistencyManager)
updateNewModel(updateModel, consistencyManager: consistencyManager)

XCTAssertTrue(calledUpdateClosure)
}
Expand Down Expand Up @@ -148,7 +148,7 @@ class ModelUpdatesTests: ConsistencyManagerTestCase {
XCTAssertEqual(updates.deletedModelIds.count, 0)
}

updateWithNewModel(updateModel, consistencyManager: consistencyManager)
updateNewModel(updateModel, consistencyManager: consistencyManager)

XCTAssertTrue(calledUpdateClosure)
}
Expand Down Expand Up @@ -179,7 +179,7 @@ class ModelUpdatesTests: ConsistencyManagerTestCase {
XCTAssertEqual(updates.deletedModelIds.count, 0)
}

updateWithNewModel(updateModel, consistencyManager: consistencyManager)
updateNewModel(updateModel, consistencyManager: consistencyManager)

XCTAssertTrue(calledUpdateClosure)
}
Expand Down Expand Up @@ -213,7 +213,7 @@ class ModelUpdatesTests: ConsistencyManagerTestCase {
XCTAssertEqual(updates.deletedModelIds.count, 0)
}

updateWithNewModel(updateModel, consistencyManager: consistencyManager)
updateNewModel(updateModel, consistencyManager: consistencyManager)

XCTAssertTrue(calledUpdateClosure)
}
Expand All @@ -232,7 +232,7 @@ class ModelUpdatesTests: ConsistencyManagerTestCase {
let initialSubchildUpdate = TestModel(id: "7", data: 0, children: [], requiredModel: TestRequiredModel(id: "8", data: 0))
let initialUpdateModel = TestModel(id: "2", data: 0, children: [initialSubchildUpdate], requiredModel: TestRequiredModel(id: "3", data: 0))
// Now, let's do an update so it changes
updateWithNewModel(initialUpdateModel, consistencyManager: consistencyManager)
updateNewModel(initialUpdateModel, consistencyManager: consistencyManager)

// Now, let's change the new model, but with it contained in a subtree
// Here were updating both 2 and 7. We expect both of these to register as updates
Expand All @@ -252,7 +252,7 @@ class ModelUpdatesTests: ConsistencyManagerTestCase {
XCTAssertEqual(updates.deletedModelIds.count, 0)
}

updateWithNewModel(updateModel, consistencyManager: consistencyManager)
updateNewModel(updateModel, consistencyManager: consistencyManager)

XCTAssertTrue(calledUpdateClosure)
}
Expand Down
Loading

0 comments on commit e2f7d36

Please sign in to comment.