2019-10-10 00:15:22 +02:00
|
|
|
//
|
|
|
|
// FeedlyCompoundOperation.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 10/10/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2019-10-10 12:24:45 +02:00
|
|
|
/// An operation with a queue of its own.
|
2019-10-10 00:15:22 +02:00
|
|
|
final class FeedlyCompoundOperation: FeedlyOperation {
|
|
|
|
private let operationQueue = OperationQueue()
|
2019-10-16 13:12:37 +02:00
|
|
|
private var finishOperation: BlockOperation?
|
2019-10-10 00:15:22 +02:00
|
|
|
|
|
|
|
init(operations: [Operation]) {
|
|
|
|
assert(!operations.isEmpty)
|
2019-10-16 13:12:37 +02:00
|
|
|
operationQueue.isSuspended = true
|
|
|
|
finishOperation = nil
|
|
|
|
super.init()
|
|
|
|
|
|
|
|
let finish = BlockOperation {
|
|
|
|
self.didFinish()
|
|
|
|
}
|
|
|
|
|
|
|
|
finishOperation = finish
|
|
|
|
|
|
|
|
for operation in operations {
|
|
|
|
finish.addDependency(operation)
|
|
|
|
}
|
|
|
|
|
|
|
|
var initialOperations = operations
|
|
|
|
initialOperations.append(finish)
|
|
|
|
|
|
|
|
operationQueue.addOperations(initialOperations, waitUntilFinished: false)
|
2019-10-10 00:15:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
convenience init(operationsBlock: () -> ([Operation])) {
|
|
|
|
let operations = operationsBlock()
|
|
|
|
self.init(operations: operations)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func main() {
|
2019-10-16 13:12:37 +02:00
|
|
|
guard !isCancelled else {
|
|
|
|
didFinish()
|
|
|
|
return
|
2019-10-10 00:15:22 +02:00
|
|
|
}
|
2019-10-16 13:12:37 +02:00
|
|
|
operationQueue.isSuspended = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func addAnotherOperation(_ operation: Operation) {
|
|
|
|
guard !isCancelled else { return }
|
|
|
|
finishOperation?.addDependency(operation)
|
|
|
|
operationQueue.addOperation(operation)
|
2019-10-10 00:15:22 +02:00
|
|
|
}
|
2019-10-15 09:31:24 +02:00
|
|
|
|
|
|
|
override func cancel() {
|
|
|
|
operationQueue.cancelAllOperations()
|
|
|
|
super.cancel()
|
|
|
|
}
|
2019-10-10 00:15:22 +02:00
|
|
|
}
|