From efd1840f35ba61e85942c4da836a02e961a6c651 Mon Sep 17 00:00:00 2001 From: CoreCode Date: Mon, 8 Jul 2019 20:51:16 +0200 Subject: [PATCH] Update SUUpdater.m --- Sparkle/SUUpdater.m | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index e1c7b1f8..9ba5ec15 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -37,6 +37,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification @interface SUUpdater () @property (strong) NSTimer *checkTimer; +@property (assign) BOOL shouldRescheduleOnWake; @property (strong) NSBundle *sparkleBundle; - (instancetype)initForBundle:(NSBundle *)bundle; @@ -59,6 +60,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification @synthesize delegate; @synthesize checkTimer; +@synthesize shouldRescheduleOnWake; @synthesize userAgentString = customUserAgentString; @synthesize httpHeaders; @synthesize driver; @@ -113,6 +115,9 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults if (self) { [self registerAsObserver]; } + + [NSWorkspace.sharedWorkspace.notificationCenter addObserver:self selector:@selector(receiveSleepNote) name:NSWorkspaceWillSleepNotification object:NULL]; + [NSWorkspace.sharedWorkspace.notificationCenter addObserver:self selector:@selector(receiveWakeNote) name:NSWorkspaceDidWakeNotification object:NULL]; id updater = [sharedUpdaters objectForKey:[NSValue valueWithNonretainedObject:bundle]]; if (updater) @@ -294,6 +299,24 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults self.checkTimer = [NSTimer scheduledTimerWithTimeInterval:delayUntilCheck target:self selector:@selector(checkForUpdatesInBackground) userInfo:nil repeats:NO]; // Timer is non-repeating, may have invalidated itself, so we had to retain it. } +- (void)receiveSleepNote +{ + if (self.checkTimer) + { + [self.checkTimer invalidate]; + self.checkTimer = nil; + self.shouldRescheduleOnWake = YES; + } + else + self.shouldRescheduleOnWake = NO; +} + +- (void)receiveWakeNote +{ + if (self.shouldRescheduleOnWake) // the reason for rescheduling the update-check timer is that NSTimer does behave as if the time the Mac spends asleep did not exist at all, which can significantly prolong the time between update checks + [self scheduleNextUpdateCheck]; +} + - (void)checkForUpdatesInBackground { BOOL automatic = [self automaticallyDownloadsUpdates];