importFoundationimport IOKit.psimport IOKit.pwr_mgt/// While a source is importing and the Mac is on mains power, hold a/// `PreventUserIdleSystemSleep` assertion so the run finishes (copied from/// CottonHost's `StayAwake` and `IOKitPowerAssertion`). The display still/// sleeps; the lid still sleeps the Mac; on battery nothing is held.@MainActorfinalclassStayAwake {staticlet reason = "Cotton is importing into your database"privatevar identifier = IOPMAssertionID(0)privatevar holding = falsefunc update(importing: Bool) {let wanted = importing && Self.isOnMainsPowerif wanted, !holding {var created = IOPMAssertionID(0)let result = IOPMAssertionCreateWithName( kIOPMAssertPreventUserIdleSystemSleep as CFString, IOPMAssertionLevel(kIOPMAssertionLevelOn),Self.reason as CFString, &created )if result == kIOReturnSuccess { identifier = created holding = true } } elseif !wanted, holding { IOPMAssertionRelease(identifier) holding = false } }/// A desktop with no battery to ask about counts as mains.staticvar isOnMainsPower: Bool {guardlet snapshot = IOPSCopyPowerSourcesInfo()?.takeRetainedValue(),let kind = IOPSGetProvidingPowerSourceType(snapshot)?.takeUnretainedValue() asString?else { returntrue }return kind != kIOPMBatteryPowerKey }}