// Copyright © 2019 650 Industries. All rights reserved. #import #import NS_ASSUME_NONNULL_BEGIN @interface EXUpdatesSelectionPolicyNewest () @property (nonatomic, strong) NSArray *runtimeVersions; @end @implementation EXUpdatesSelectionPolicyNewest - (instancetype)initWithRuntimeVersions:(NSArray *)runtimeVersions { if (self = [super init]) { _runtimeVersions = runtimeVersions; } return self; } - (instancetype)initWithRuntimeVersion:(NSString *)runtimeVersion { return [self initWithRuntimeVersions:@[runtimeVersion]]; } - (nullable EXUpdatesUpdate *)launchableUpdateWithUpdates:(NSArray *)updates { EXUpdatesUpdate *runnableUpdate; NSDate *runnableUpdateCommitTime; for (EXUpdatesUpdate *update in updates) { if (![_runtimeVersions containsObject:update.runtimeVersion]) { continue; } NSDate *commitTime = update.commitTime; if (!runnableUpdateCommitTime || [runnableUpdateCommitTime compare:commitTime] == NSOrderedAscending) { runnableUpdate = update; runnableUpdateCommitTime = commitTime; } } return runnableUpdate; } - (NSArray *)updatesToDeleteWithLaunchedUpdate:(EXUpdatesUpdate *)launchedUpdate updates:(NSArray *)updates { if (!launchedUpdate) { return @[]; } NSMutableArray *updatesToDelete = [NSMutableArray new]; // keep the launched update and one other, the next newest, to be safe and make rollbacks faster EXUpdatesUpdate *nextNewestUpdate; for (EXUpdatesUpdate *update in updates) { if ([launchedUpdate.commitTime compare:update.commitTime] == NSOrderedDescending) { [updatesToDelete addObject:update]; if (!nextNewestUpdate || [update.commitTime compare:nextNewestUpdate.commitTime] == NSOrderedDescending) { nextNewestUpdate = update; } } } if (nextNewestUpdate) { [updatesToDelete removeObject:nextNewestUpdate]; } return updatesToDelete; } - (BOOL)shouldLoadNewUpdate:(nullable EXUpdatesUpdate *)newUpdate withLaunchedUpdate:(nullable EXUpdatesUpdate *)launchedUpdate { if (!newUpdate) { return false; } if (!launchedUpdate) { return true; } return [launchedUpdate.commitTime compare:newUpdate.commitTime] == NSOrderedAscending; } @end NS_ASSUME_NONNULL_END