Handle all download events from AutoUpdater::CheckForUpdates

This commit is contained in:
probablycorey 2014-02-01 15:55:57 -08:00
parent a8913f5ea0
commit 5da1e9ed10

View file

@ -43,29 +43,6 @@ void AutoUpdater::SetFeedURL(const std::string& feed) {
if (!delegate) if (!delegate)
return; return;
__block SQRLUpdate* update = nil;
// Subscribe to events.
[g_updater.updates subscribeNext:^(SQRLDownloadedUpdate* downloadedUpdate) {
update = downloadedUpdate.update;
} completed:^() {
if (update) {
// There is a new update that has been downloaded.
delegate->OnUpdateDownloaded(
base::SysNSStringToUTF8(update.releaseNotes),
base::SysNSStringToUTF8(update.releaseName),
base::Time::FromDoubleT(update.releaseDate.timeIntervalSince1970),
base::SysNSStringToUTF8(update.updateURL.absoluteString),
base::Bind(RelaunchToInstallUpdate));
}
else {
// When the completed event is sent with no update, then we know there
// is no update available.
delegate->OnUpdateNotAvailable();
}
update = nil;
}];
[[g_updater rac_valuesForKeyPath:@"state" observer:g_updater] [[g_updater rac_valuesForKeyPath:@"state" observer:g_updater]
subscribeNext:^(NSNumber *stateNumber) { subscribeNext:^(NSNumber *stateNumber) {
int state = [stateNumber integerValue]; int state = [stateNumber integerValue];
@ -81,14 +58,34 @@ void AutoUpdater::SetFeedURL(const std::string& feed) {
// static // static
void AutoUpdater::CheckForUpdates() { void AutoUpdater::CheckForUpdates() {
RACSignal* signal = [g_updater.checkForUpdatesCommand execute:nil];
AutoUpdaterDelegate* delegate = GetDelegate(); AutoUpdaterDelegate* delegate = GetDelegate();
if (!delegate) if (!delegate)
return; return;
[signal subscribeError:^(NSError* error) { [[[[g_updater.checkForUpdatesCommand
// Something wrong happened. execute:nil]
// Send a `nil` after everything...
concat:[RACSignal return:nil]]
// But only take the first value. If an update is sent, we'll get that.
// Otherwise, we'll get our inserted `nil` value.
take:1]
subscribeNext:^(SQRLDownloadedUpdate *downloadedUpdate) {
if (downloadedUpdate) {
SQRLUpdate* update = downloadedUpdate.update;
// There is a new update that has been downloaded.
delegate->OnUpdateDownloaded(
base::SysNSStringToUTF8(update.releaseNotes),
base::SysNSStringToUTF8(update.releaseName),
base::Time::FromDoubleT(update.releaseDate.timeIntervalSince1970),
base::SysNSStringToUTF8(update.updateURL.absoluteString),
base::Bind(RelaunchToInstallUpdate));
}
else {
// When the completed event is sent with no update, then we know there
// is no update available.
delegate->OnUpdateNotAvailable();
}
} error:^(NSError *error) {
delegate->OnError(base::SysNSStringToUTF8(error.localizedDescription)); delegate->OnError(base::SysNSStringToUTF8(error.localizedDescription));
}]; }];
} }