mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-28 08:17:16 +00:00
142 lines
4.1 KiB
Swift
142 lines
4.1 KiB
Swift
//
|
||
// MainThreadOperation.swift
|
||
// RSCore
|
||
//
|
||
// Created by Brent Simmons on 1/10/20.
|
||
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
|
||
//
|
||
|
||
import Foundation
|
||
import os
|
||
|
||
/// Code to be run by MainThreadOperationQueue on @MainActor.
|
||
///
|
||
/// Override `run()` with the code to be run — this is the only
|
||
/// thing that needs to be overridden.
|
||
///
|
||
/// When finished, it must call `didComplete()`.
|
||
///
|
||
/// It should check `isCanceled` at appropriate times,
|
||
/// and do its best stop when canceled.
|
||
/// However, it should not leave data in an inconsistent state.
|
||
///
|
||
/// The completion block will be called when finished,
|
||
/// regardless of cancellation status. The completion block
|
||
/// takes the operation as a parameter so that it can check
|
||
/// cancellation status if it needs to.
|
||
@MainActor open class MainThreadOperation: Hashable, @unchecked Sendable {
|
||
public let id: Int
|
||
private static var incrementingID = 0
|
||
|
||
private struct State: Sendable {
|
||
var isCanceled = false
|
||
}
|
||
private let state = OSAllocatedUnfairLock(initialState: State())
|
||
|
||
// Check this at appropriate times in case the operation has been canceled.
|
||
nonisolated public var isCanceled: Bool {
|
||
get { state.withLock { $0.isCanceled } }
|
||
set { state.withLock { $0.isCanceled = newValue } }
|
||
}
|
||
|
||
public let name: String?
|
||
|
||
public typealias MainThreadOperationCompletionBlock = @MainActor (MainThreadOperation) -> Void
|
||
public var completionBlock: MainThreadOperationCompletionBlock?
|
||
|
||
public weak var operationQueue: MainThreadOperationQueue?
|
||
|
||
var dependencies = Set<MainThreadOperation>()
|
||
|
||
/// Create a new MainThreadOperation.
|
||
///
|
||
/// This doesn’t add the operation to the queue.
|
||
/// Call `MainThreadOperationQueue.add` to add it.
|
||
///
|
||
/// - Parameters:
|
||
/// - name: Name of the operation — used for debugging.
|
||
/// - completionBlock: Called on the main thread once the operation has completed.
|
||
/// Called even if canceled, even if `run` was never called.
|
||
public init(name: String? = nil, completionBlock: MainThreadOperationCompletionBlock? = nil) {
|
||
self.id = Self.incrementingID
|
||
Self.incrementingID += 1
|
||
self.name = name
|
||
self.completionBlock = completionBlock
|
||
}
|
||
|
||
/// Do the thing this operation does. This method must be subclassed.
|
||
///
|
||
/// This code runs on the main thread. You can run code off the main
|
||
/// thread via the standard ways.
|
||
open func run() {
|
||
preconditionFailure("MainThreadOperation.run must be overridden.")
|
||
}
|
||
|
||
/// Cancel this operation.
|
||
///
|
||
/// Any operations dependent on this operation
|
||
/// will also be canceled automatically.
|
||
public func cancel() {
|
||
isCanceled = true
|
||
dependencies.removeAll()
|
||
Task { @MainActor in
|
||
didComplete()
|
||
}
|
||
}
|
||
|
||
/// Make this operation dependent on an other operation.
|
||
///
|
||
/// Do this before adding to the queue, since it might get run
|
||
/// before adding the dependency if you add to the queue first.
|
||
///
|
||
/// The other operation must complete before
|
||
/// this operation gets run. If the other operation is canceled,
|
||
/// this operation will automatically be canceled.
|
||
/// Note: an operation can have multiple dependencies.
|
||
public func addDependency(_ parentOperation: MainThreadOperation) {
|
||
dependencies.insert(parentOperation)
|
||
}
|
||
|
||
func removeDependency(_ parentOperation: MainThreadOperation) {
|
||
dependencies.remove(parentOperation)
|
||
}
|
||
|
||
func hasDependency(_ parentOperation: MainThreadOperation) -> Bool {
|
||
dependencies.contains(parentOperation)
|
||
}
|
||
|
||
/// Call when completed. This will trigger calling completionBlock.
|
||
nonisolated public func didComplete() {
|
||
if Thread.isMainThread {
|
||
MainActor.assumeIsolated {
|
||
operationQueue?.operationDidComplete(self)
|
||
}
|
||
} else {
|
||
Task { @MainActor in
|
||
didComplete()
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Override to be notified when the operation is complete.
|
||
open func noteDidComplete() {
|
||
}
|
||
|
||
func callCompletionBlock() {
|
||
completionBlock?(self)
|
||
completionBlock = nil
|
||
}
|
||
|
||
// MARK: - Hashable
|
||
|
||
nonisolated public func hash(into hasher: inout Hasher) {
|
||
hasher.combine(id)
|
||
}
|
||
|
||
// MARK: - Equatable
|
||
|
||
nonisolated static public func ==(lhs: MainThreadOperation, rhs: MainThreadOperation) -> Bool {
|
||
lhs.id == rhs.id
|
||
}
|
||
}
|