Skip to content

Commit

Permalink
Use Task
Browse files Browse the repository at this point in the history
  • Loading branch information
zmian committed Feb 12, 2024
1 parent e002b08 commit 1a3fb0d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Sources/Xcore/Cocoa/Extensions/AVFoundation+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension AVPlayerItem {
/// `nil`, this method looks in the main bundle of the current application.
/// - Returns: An instance of AVPlayerItem.
public convenience init?(filename: String, bundle: Bundle? = nil) {
guard let url = (bundle ?? Bundle.main).url(filename: filename) else {
guard let url = (bundle ?? .main).url(filename: filename) else {
return nil
}

Expand Down Expand Up @@ -78,7 +78,7 @@ extension AVAsset {
/// `nil`, this method looks in the main bundle of the current application.
/// - Returns: An instance of AVAsset.
public convenience init?(filename: String, bundle: Bundle? = nil) {
guard let url = (bundle ?? Bundle.main).url(filename: filename) else {
guard let url = (bundle ?? .main).url(filename: filename) else {
return nil
}

Expand Down
14 changes: 7 additions & 7 deletions Sources/Xcore/Cocoa/Extensions/NSError+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

import Foundation

extension NSError {
/// Returns an error with random domain and code.
public static func random() -> Self {
.init(domain: .random(), code: .random(), userInfo: nil)
}
}

extension NSError {
/// Appends the given dictionary to the existing `userInfo` dictionary replacing
/// any existing values with newer ones.
Expand All @@ -20,10 +27,3 @@ extension NSError {
)
}
}

extension NSError {
/// Returns an error with random domain and code.
public static func random() -> Self {
.init(domain: .random(), code: .random(), userInfo: nil)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extension NotificationCenter {
/// delivery.
/// - Returns: An asynchronous sequence of notifications from the center.
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
public static func async(_ name: Notification.Name, object: AnyObject? = nil) -> NotificationCenter.Notifications {
public static func async(_ name: Notification.Name, object: AnyObject? = nil) -> Notifications {
shared.notifications(named: name, object: object)
}
}
Expand Down Expand Up @@ -98,12 +98,10 @@ extension NotificationCenter {
userInfo: [AnyHashable: Any]? = nil,
delayInterval: TimeInterval = 0
) {
guard delayInterval > 0 else {
shared.post(name: name, object: object, userInfo: userInfo)
return
}

Timer.after(delayInterval) {
Task {
if delayInterval > 0 {
try await Task.sleep(for: .seconds(delayInterval))
}
shared.post(name: name, object: object, userInfo: userInfo)
}
}
Expand Down Expand Up @@ -137,6 +135,6 @@ extension NotificationCenter {
}

private static var shared: NotificationCenter {
NotificationCenter.default
.default
}
}
7 changes: 7 additions & 0 deletions Sources/Xcore/Cocoa/Extensions/UIImage+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ extension UIImage {
case png
/// Encoding format in JPEG format.
case jpeg(quality: Double)
/// Encoding format in HEIC format.
case heic
}

/// Returns a data object that contains the specified image in a given format.
Expand All @@ -26,6 +28,11 @@ extension UIImage {
return pngData()
case let .jpeg(quality):
return jpegData(compressionQuality: quality)
case .heic:
if #available(iOS 17.0, *) {
return heicData()
}
return nil
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ extension WKWebsiteDataStore {
}

public func remove(_ type: DataType, _ completion: (() -> Void)? = nil) {
fetchDataRecords(ofTypes: type.dataTypes) { [weak self] records in
self?.removeData(ofTypes: type.dataTypes, for: records) {
completion?()
}
Task {
let records = await dataRecords(ofTypes: type.dataTypes)
await removeData(ofTypes: type.dataTypes, for: records)
completion?()
}
}
}
Expand Down

0 comments on commit 1a3fb0d

Please sign in to comment.