Skip to content

Commit

Permalink
0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alemar11 committed Feb 17, 2018
1 parent 9fd7af3 commit 32581af
Showing 1 changed file with 28 additions and 32 deletions.
60 changes: 28 additions & 32 deletions Sources/AdvancedOperationQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Foundation
protocol AdvancedOperationQueueDelegate: class {
func operationQueue(operationQueue: AdvancedOperationQueue, willAddOperation operation: Operation)
func operationQueue(operationQueue: AdvancedOperationQueue, didAddOperation operation: Operation)

func operationQueue(operationQueue: AdvancedOperationQueue, operationWillExecute operation: Operation)
func operationQueue(operationQueue: AdvancedOperationQueue, operationDidFinish operation: Operation, withErrors errors: [Error])
func operationQueue(operationQueue: AdvancedOperationQueue, operationDidCancel operation: Operation, withErrors errors: [Error])
Expand All @@ -37,65 +37,61 @@ extension AdvancedOperationQueueDelegate {
func operationQueue(operationQueue: AdvancedOperationQueue, operationWillExecute operation: Operation) {}
}

/// `AdvancedOperationQueue` is an `NSOperationQueue` subclass that implements a large number of "extra features" related to the `Operation` class:

/// - Notifying a delegate of all operation completion
/// - TODO
/// - TODO
/// `AdvancedOperationQueue` is an `OperationQueue` subclass that implements a large number of "extra features" related to the `Operation` class.
class AdvancedOperationQueue: OperationQueue {

weak var delegate: AdvancedOperationQueueDelegate?

override func addOperation(_ operation: Operation) {

if let operation = operation as? AdvancedOperation { /// AdvancedOperation

let observer = BlockObserver(willExecute: { [weak self] (operation) in
guard let `self` = self else { return }
self.delegate?.operationQueue(operationQueue: self, operationWillExecute: operation)

}, didCancel: { [weak self] (operation, errors) in
guard let `self` = self else { return }
self.delegate?.operationQueue(operationQueue: self, operationDidCancel: operation, withErrors: errors)

}, didFinish: { [weak self] (operation, errors) in
guard let `self` = self else { return }
self.delegate?.operationQueue(operationQueue: self, operationDidFinish: operation, withErrors: errors)

}, didCancel: { [weak self] (operation, errors) in
guard let `self` = self else { return }
self.delegate?.operationQueue(operationQueue: self, operationDidCancel: operation, withErrors: errors)
}, didFinish: { [weak self] (operation, errors) in
guard let `self` = self else { return }
self.delegate?.operationQueue(operationQueue: self, operationDidFinish: operation, withErrors: errors)
})

operation.addObserver(observer: observer)

} else { /// Operation

// For regular `Operation`s, we'll manually call out to the queue's delegate we don't want
// to just capture "operation" because that would lead to the operation strongly referencing itself and that's the pure definition of a memory leak.
operation.addCompletionBlock(asEndingBlock: false) { [weak self, weak operation] in
guard let queue = self, let operation = operation else { return }

queue.delegate?.operationQueue(operationQueue: queue, operationDidFinish: operation, withErrors: [])
}
}

delegate?.operationQueue(operationQueue: self, willAddOperation: operation)
super.addOperation(operation) // FIXME: This causes an infinite loop on Linux
delegate?.operationQueue(operationQueue: self, didAddOperation: operation)

}

override func addOperations(_ operations: [Operation], waitUntilFinished wait: Bool) {

for operation in operations {
addOperation(operation)
}

if wait {
//TODO: use this method?
//waitUntilAllOperationsAreFinished()
for operation in operations {
operation.waitUntilFinished()
}
for operation in operations {
operation.waitUntilFinished()
}
}
}

}

0 comments on commit 32581af

Please sign in to comment.