mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-10 15:23:27 +01:00
[AltStoreCore] Adds Managed.perform() to match AsyncManaged
This commit is contained in:
@@ -32,41 +32,66 @@ public struct Managed<ManagedObject>
|
||||
self.wrappedValue = wrappedValue
|
||||
self.managedObjectContext = self.managedObject?.managedObjectContext
|
||||
}
|
||||
|
||||
public subscript<T>(dynamicMember keyPath: KeyPath<ManagedObject, T>) -> T
|
||||
}
|
||||
|
||||
/// Run on managedObjectContext's queue.
|
||||
public extension Managed
|
||||
{
|
||||
// Non-throwing
|
||||
func perform<T>(_ closure: @escaping (ManagedObject) -> T) -> T
|
||||
{
|
||||
var result: T!
|
||||
|
||||
if let context = self.managedObjectContext
|
||||
{
|
||||
context.performAndWait {
|
||||
result = self.wrappedValue[keyPath: keyPath]
|
||||
result = closure(self.wrappedValue)
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = self.wrappedValue[keyPath: keyPath]
|
||||
result = closure(self.wrappedValue)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Optionals
|
||||
public subscript<Wrapped, T>(dynamicMember keyPath: KeyPath<Wrapped, T>) -> T? where ManagedObject == Optional<Wrapped>
|
||||
// Throwing
|
||||
func perform<T>(_ closure: @escaping (ManagedObject) throws -> T) throws -> T
|
||||
{
|
||||
var result: T?
|
||||
var result: Result<T, Error>!
|
||||
|
||||
if let context = self.managedObjectContext
|
||||
{
|
||||
context.performAndWait {
|
||||
result = self.wrappedValue?[keyPath: keyPath] as? T
|
||||
result = Result { try closure(self.wrappedValue) }
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = self.wrappedValue?[keyPath: keyPath] as? T
|
||||
result = Result { try closure(self.wrappedValue) }
|
||||
}
|
||||
|
||||
let value = try result.get()
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
/// @dynamicMemberLookup
|
||||
public extension Managed
|
||||
{
|
||||
subscript<T>(dynamicMember keyPath: KeyPath<ManagedObject, T>) -> T
|
||||
{
|
||||
let result = self.perform { $0[keyPath: keyPath] }
|
||||
return result
|
||||
}
|
||||
|
||||
// Optionals
|
||||
subscript<Wrapped, T>(dynamicMember keyPath: KeyPath<Wrapped, T>) -> T? where ManagedObject == Optional<Wrapped>
|
||||
{
|
||||
guard let wrappedValue else { return nil }
|
||||
|
||||
let result = self.perform { _ in wrappedValue[keyPath: keyPath] }
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user