|
20 | 20 | import org.jackhuang.hmcl.task.Task; |
21 | 21 |
|
22 | 22 | import java.util.Arrays; |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.List; |
23 | 25 |
|
24 | 26 | import static org.jackhuang.hmcl.util.logging.Logger.LOG; |
25 | 27 |
|
@@ -47,25 +49,58 @@ public Task<?> refreshAsync() { |
47 | 49 |
|
48 | 50 | private Task<?> refreshAsync(String gameVersion, int sourceIndex) { |
49 | 51 | VersionList<?> versionList = backends[sourceIndex]; |
50 | | - return versionList.refreshAsync(gameVersion) |
51 | | - .thenComposeAsync(() -> { |
| 52 | + Task<?> refreshTask = versionList.refreshAsync(gameVersion); |
| 53 | + |
| 54 | + return new Task<Object>() { |
| 55 | + private Task<?> nextTask = null; |
| 56 | + |
| 57 | + { |
| 58 | + setSignificance(TaskSignificance.MODERATE); |
| 59 | + setName("MultipleSourceVersionList.refreshAsync(" + sourceIndex + ")"); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public Collection<Task<?>> getDependents() { |
| 64 | + return List.of(refreshTask); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Collection<? extends Task<?>> getDependencies() { |
| 69 | + return nextTask != null ? List.of(nextTask) : List.of(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean isRelyingOnDependents() { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void execute() throws Exception { |
| 79 | + if (isDependentsSucceeded()) { |
52 | 80 | lock.writeLock().lock(); |
53 | 81 | try { |
54 | 82 | versions.putAll(gameVersion, versionList.getVersions(gameVersion)); |
55 | | - } catch (Exception e) { |
56 | | - if (sourceIndex == backends.length - 1) { |
57 | | - LOG.warning("Failed to fetch versions list from all sources", e); |
58 | | - throw e; |
59 | | - } else { |
60 | | - LOG.warning("Failed to fetch versions list and try to fetch from other source", e); |
61 | | - return refreshAsync(gameVersion, sourceIndex + 1); |
62 | | - } |
63 | 83 | } finally { |
64 | 84 | lock.writeLock().unlock(); |
65 | 85 | } |
66 | 86 |
|
67 | | - return null; |
68 | | - }); |
| 87 | + setResult(refreshTask.getResult()); |
| 88 | + } else { |
| 89 | + Exception exception = refreshTask.getException(); |
| 90 | + assert exception != null; |
| 91 | + |
| 92 | + if (sourceIndex == backends.length - 1) { |
| 93 | + LOG.warning("Failed to fetch versions list from all sources", exception); |
| 94 | + setSignificance(TaskSignificance.MINOR); |
| 95 | + throw exception; |
| 96 | + } else { |
| 97 | + LOG.warning("Failed to fetch versions list and try to fetch from other source", exception); |
| 98 | + nextTask = refreshAsync(gameVersion, sourceIndex + 1); |
| 99 | + nextTask.storeTo(this::setResult); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + }; |
69 | 104 | } |
70 | 105 |
|
71 | 106 | @Override |
|
0 commit comments