Skip to content

Commit

Permalink
Fix retry mechanism on API loading not working
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-nyan committed May 24, 2024
1 parent 0cb0675 commit d329d4f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
5 changes: 3 additions & 2 deletions CollapseLauncher/Classes/Extension/TaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ internal static class TaskExtensions
}

if (lastException is not null
&& lastException is TaskCanceledException
&& !fromToken.IsCancellationRequested)
throw new TimeoutException($"The operation has timed out with inner exception!", lastException);
throw lastException is TaskCanceledException ?
new TimeoutException($"The operation has timed out with inner exception!", lastException) :
lastException;

throw new TimeoutException($"The operation has timed out!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal interface ILauncherApi
string? GameRegionTranslation { get; }
RegionResourceProp? LauncherGameResource { get; }
LauncherGameNews? LauncherGameNews { get; }
Task LoadAsync(OnLoadAction? beforeLoadRoutine = null, OnLoadAction? afterLoadRoutine = null,
Task<bool> LoadAsync(OnLoadAction? beforeLoadRoutine = null, OnLoadAction? afterLoadRoutine = null,
ActionOnTimeOutRetry? onTimeoutRoutine = null, ErrorLoadRoutineDelegate? errorLoadRoutine = null,
CancellationToken token = default);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,27 @@ protected LauncherApiBase(PresetConfig presetConfig, string gameName, string gam
GameRegion = gameRegion;
}

public async Task LoadAsync(OnLoadAction? beforeLoadRoutine, OnLoadAction? afterLoadRoutine,
ActionOnTimeOutRetry? onTimeoutRoutine,
ErrorLoadRoutineDelegate? errorLoadRoutine, CancellationToken token)
public async Task<bool> LoadAsync(OnLoadAction? beforeLoadRoutine, OnLoadAction? afterLoadRoutine,
ActionOnTimeOutRetry? onTimeoutRoutine, ErrorLoadRoutineDelegate? errorLoadRoutine,
CancellationToken token)
{
beforeLoadRoutine?.Invoke(token);

try
{
IsLoadingCompleted = false;
await LoadAsyncInner(onTimeoutRoutine, token);
afterLoadRoutine?.Invoke(token);

return true;
}
catch (Exception ex)
{
errorLoadRoutine?.Invoke(ex);
return false;
}
finally
{
afterLoadRoutine?.Invoke(token);

IsLoadingCompleted = true;
}
}
Expand Down
16 changes: 9 additions & 7 deletions CollapseLauncher/Classes/RegionManagement/RegionManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,20 @@ void AfterLoadRoutine(CancellationToken token)
LoadingMessageHelper.HideLoadingFrame();

IsLoadRegionComplete = true;

LogWriteLine($"Game: {regionToChangeName} has been completely initialized!", LogType.Scheme, true);
FinalizeLoadRegion(gameName, gameRegion);
ChangeBackgroundImageAsRegionAsync();
}

void OnErrorRoutine(Exception ex)
{
LoadingMessageHelper.HideActionButton();
LoadingMessageHelper.HideLoadingFrame();

LogWriteLine($"Error has occurred while loading: {regionToChangeName}!\r\n{ex}", LogType.Scheme, true);
ErrorSender.SendExceptionWithoutPage(ex, ErrorType.Connection);
MainFrameChanger.ChangeWindowFrame(typeof(DisconnectedPage));
}

async void CancelLoadEvent(object sender, RoutedEventArgs args)
Expand Down Expand Up @@ -114,13 +122,7 @@ void ActionOnTimeOutRetry(int retryAttemptCount, int retryAttemptTotal, int time
LoadingMessageHelper.ShowActionButton(Lang._Misc.Cancel, "", CancelLoadEvent);
}

await preset.GameLauncherApi.LoadAsync(BeforeLoadRoutine, AfterLoadRoutine, ActionOnTimeOutRetry, OnErrorRoutine, tokenSource.Token);

LogWriteLine($"Game: {regionToChangeName} has been completely initialized!", LogType.Scheme, true);
FinalizeLoadRegion(gameName, gameRegion);
ChangeBackgroundImageAsRegionAsync();

return true;
return await preset.GameLauncherApi.LoadAsync(BeforeLoadRoutine, AfterLoadRoutine, ActionOnTimeOutRetry, OnErrorRoutine, tokenSource.Token);
}

public void ClearMainPageState()
Expand Down

0 comments on commit d329d4f

Please sign in to comment.