TM-SGNL-iOS/SignalServiceKit/Threading/Threading.swift
TeleMessage developers dde0620daf initial commit
2025-05-03 12:28:28 -07:00

33 lines
985 B
Swift

//
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
// The block is executed immediately if called from the
// main thread; otherwise it is dispatched async to the
// main thread.
public func DispatchMainThreadSafe(_ block: @escaping @MainActor () -> Void) {
if Thread.isMainThread {
MainActor.assumeIsolated(block)
} else {
DispatchQueue.main.async(execute: block)
}
}
// The block is executed immediately if called from the
// main thread; otherwise it is dispatched sync to the
// main thread.
public func DispatchSyncMainThreadSafe(_ block: @escaping @MainActor () -> Void) {
if Thread.isMainThread {
MainActor.assumeIsolated(block)
} else {
DispatchQueue.main.sync(execute: block)
}
}
@objcMembers
public final class ThreadingObjcBridge: NSObject {
public static func dispatchMainThreadSafe(_ block: @escaping @MainActor () -> Void) {
DispatchMainThreadSafe(block)
}
}