NetNewsWire/Frameworks/Account/Feedly/Operations/FeedlyCompoundOperation.swift

62 lines
1.4 KiB
Swift
Raw Normal View History

//
// 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.
final class FeedlyCompoundOperation: FeedlyOperation, FeedlyCheckpointOperationDelegate {
private let operationQueue = OperationQueue()
private let finishOperation = FeedlyCheckpointOperation()
init(operations: [Operation]) {
assert(!operations.isEmpty)
operationQueue.isSuspended = true
super.init()
finishOperation.checkpointDelegate = self
for operation in operations {
finishOperation.addDependency(operation)
}
var initialOperations = operations
initialOperations.append(finishOperation)
operationQueue.addOperations(initialOperations, waitUntilFinished: false)
}
convenience init(operationsBlock: () -> ([Operation])) {
let operations = operationsBlock()
self.init(operations: operations)
}
override func main() {
guard !isCancelled else {
didFinish()
return
}
operationQueue.isSuspended = false
}
func feedlyCheckpointOperationDidReachCheckpoint(_ operation: FeedlyCheckpointOperation) {
didFinish()
}
func addAnotherOperation(_ operation: Operation) {
guard !isCancelled else { return }
finishOperation.addDependency(operation)
operationQueue.addOperation(operation)
}
override func cancel() {
operationQueue.cancelAllOperations()
super.cancel()
}
}