iOS/Sources/Shared/Common/ProcessInfo+BackgroundTask.swift
Zac West 77c72785ff
Update a few more housekeeping things in the project (#1142)
- Combines all .entitlements into either: App-iOS, App-catalyst, WatchApp, Extension-iOS or Extension-catalyst.
- Cleans up and renames all the schemes to match target names
- Moves around several folders and deletes some old files.
- Converts Podfile to be hierarchical, rather than calling shared methods.
- Always runs MaterialDesignIcons script; aborts early if it's up-to-date.
- Updates all dependencies.
2020-10-03 16:05:19 -07:00

38 lines
1.2 KiB
Swift

import Foundation
import PromiseKit
public class ProcessInfoBackgroundTaskRunner: HomeAssistantBackgroundTaskRunner {
public func callAsFunction<PromiseValue>(
withName name: String,
wrapping: (TimeInterval?) -> Promise<PromiseValue>
) -> Promise<PromiseValue> {
ProcessInfo.processInfo.backgroundTask(withName: name, wrapping: wrapping)
}
}
private extension ProcessInfo {
func backgroundTask<PromiseValue>(
withName name: String,
wrapping: (TimeInterval?) -> Promise<PromiseValue>
) -> Promise<PromiseValue> {
let identifier = UUID()
let semaphore = DispatchSemaphore(value: 0)
return HomeAssistantBackgroundTask.execute(
withName: name,
beginBackgroundTask: { name, expirationHandler -> (UUID?, TimeInterval?) in
performExpiringActivity(withReason: name) { expire in
if expire {
expirationHandler()
} else {
semaphore.wait()
}
}
return (identifier, nil)
}, endBackgroundTask: { _ in
semaphore.signal()
}, wrapping: wrapping
)
}
}