diff --git a/Core/Sources/Core/Blocks.swift b/Core/Sources/Core/Blocks.swift index 1a99f3a12..a46dd57b7 100644 --- a/Core/Sources/Core/Blocks.swift +++ b/Core/Sources/Core/Blocks.swift @@ -11,12 +11,6 @@ import Foundation public typealias VoidBlock = () -> Void public typealias VoidCompletionBlock = VoidBlock -/// Call a VoidCompletionBlock on the main thread. -/// - Parameter block: The block to call. -public func callVoidCompletionBlock(_ block: @escaping VoidCompletionBlock) { - DispatchQueue.main.async(execute: block) -} - public typealias VoidResult = Result public typealias VoidResultCompletionBlock = (VoidResult) -> Void diff --git a/Core/Sources/Core/MainThreadBlockOperation.swift b/Core/Sources/Core/MainThreadBlockOperation.swift deleted file mode 100644 index 73c52d8f7..000000000 --- a/Core/Sources/Core/MainThreadBlockOperation.swift +++ /dev/null @@ -1,33 +0,0 @@ -// -// MainThreadBlockOperation.swift -// RSCore -// -// Created by Brent Simmons on 1/16/20. -// Copyright © 2020 Ranchero Software, LLC. All rights reserved. -// - -import Foundation - -/// Run a block of code as an operation. -/// -/// This also serves as a simple example implementation of MainThreadOperation. -public final class MainThreadBlockOperation: MainThreadOperation { - - // MainThreadOperation - public var isCanceled = false - public var id: Int? - public var operationDelegate: MainThreadOperationDelegate? - public var name: String? - public var completionBlock: MainThreadOperation.MainThreadOperationCompletionBlock? - - private let block: VoidBlock - - public init(block: @escaping VoidBlock) { - self.block = block - } - - public func run() { - block() - informOperationDelegateOfCompletion() - } -} diff --git a/Core/Sources/Core/ManagedResourceFile.swift b/Core/Sources/Core/ManagedResourceFile.swift deleted file mode 100644 index ea1652abe..000000000 --- a/Core/Sources/Core/ManagedResourceFile.swift +++ /dev/null @@ -1,127 +0,0 @@ -// -// ManagedResourceFile.swift -// RSCore -// -// Created by Maurice Parker on 9/13/19. -// Copyright © 2020 Ranchero Software, LLC. All rights reserved. -// - -import Foundation - -public final class ManagedResourceFile: NSObject, NSFilePresenter { - - private var isDirty = false { - didSet { - queueSaveToDiskIfNeeded() - } - } - - private var isLoading = false - private let fileURL: URL - private let operationQueue: OperationQueue - private var saveQueue: CoalescingQueue - - private let loadCallback: () -> Void - private let saveCallback: () -> Void - - public var saveInterval: TimeInterval = 5.0 { - didSet { - saveQueue.performCallsImmediately() - saveQueue = CoalescingQueue(name: "ManagedResourceFile Save Queue", interval: saveInterval) - } - } - - public var presentedItemURL: URL? { - return fileURL - } - - public var presentedItemOperationQueue: OperationQueue { - return operationQueue - } - - public init(fileURL: URL, load: @escaping () -> Void, save: @escaping () -> Void) { - - self.fileURL = fileURL - self.loadCallback = load - self.saveCallback = save - - saveQueue = CoalescingQueue(name: "ManagedResourceFile Save Queue", interval: saveInterval) - operationQueue = OperationQueue() - operationQueue.qualityOfService = .userInteractive - operationQueue.maxConcurrentOperationCount = 1 - - super.init() - - NSFileCoordinator.addFilePresenter(self) - } - - public func presentedItemDidChange() { - guard !isDirty else { return } - DispatchQueue.main.async { - self.load() - } - } - - public func savePresentedItemChanges(completionHandler: @escaping (Error?) -> Void) { - saveIfNecessary() - completionHandler(nil) - } - - public func relinquishPresentedItem(toReader reader: @escaping ((() -> Void)?) -> Void) { - saveQueue.isPaused = true - reader() { - self.saveQueue.isPaused = false - } - } - - public func relinquishPresentedItem(toWriter writer: @escaping ((() -> Void)?) -> Void) { - saveQueue.isPaused = true - writer() { - self.saveQueue.isPaused = false - } - } - - public func markAsDirty() { - if !isLoading { - isDirty = true - } - } - - public func queueSaveToDiskIfNeeded() { - saveQueue.add(self, #selector(saveToDiskIfNeeded)) - } - - public func load() { - isLoading = true - loadCallback() - isLoading = false - } - - public func saveIfNecessary() { - saveQueue.performCallsImmediately() - } - - public func resume() { - NSFileCoordinator.addFilePresenter(self) - } - - public func suspend() { - NSFileCoordinator.removeFilePresenter(self) - } - - deinit { - NSFileCoordinator.removeFilePresenter(self) - } - -} - -private extension ManagedResourceFile { - - @objc func saveToDiskIfNeeded() { - if isDirty { - isDirty = false - saveCallback() - } - } - -}