@@ -32,21 +32,6 @@ export function raceUntil<T>(promises: Promise<T>[], fallback: T, evalRes: (val:
32
32
} )
33
33
}
34
34
35
- export async function serialResolve < T > ( promises : Array < ( ) => Promise < T > > , fallback : T , evalRes : ( val : T ) => boolean ) : Promise < T > {
36
- for ( const p of promises ) {
37
- try {
38
- const val = await p ( )
39
- if ( evalRes ( val ) ) {
40
- return val
41
- }
42
- } catch {
43
- // Continue to next promise if this one fails
44
- continue
45
- }
46
- }
47
- return fallback
48
- }
49
-
50
35
export async function allSafe < T > ( promises : Promise < T > [ ] , fallback : T ) : Promise < T [ ] > {
51
36
return Promise . all ( promises . map ( promise => promise . catch ( ( ) => fallback ) ) )
52
37
}
@@ -58,25 +43,26 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf
58
43
) { }
59
44
60
45
async configOfImageHash ( args : { imageHash : string } ) : Promise < commons . config . Config | undefined > {
61
- const requestFactory = this . trackers . map ( ( t , i ) => async ( ) => ( { res : await t . configOfImageHash ( args ) , i } ) )
46
+ const requests = this . trackers . map ( async ( t , i ) => ( { res : await t . configOfImageHash ( args ) , i } ) )
62
47
63
48
let result1 : { res : commons . config . Config | undefined ; i : number } | undefined
64
49
65
50
if ( this . isSerial ) {
66
- result1 = await serialResolve ( requestFactory , undefined , val => {
51
+ for ( const request of requests ) {
52
+ const result = await request
53
+ if ( result . res ) {
54
+ if ( universal . genericCoderFor ( result . res . version ) . config . isComplete ( result . res ) ) {
55
+ result1 = result
56
+ break
57
+ }
58
+ }
59
+ }
60
+ } else {
61
+ // We try to find a complete configuration, we race so that we don't wait for all trackers to respond
62
+ result1 = await raceUntil ( requests , undefined , val => {
67
63
if ( val ?. res === undefined ) return false
68
64
return universal . genericCoderFor ( val . res . version ) . config . isComplete ( val . res )
69
65
} )
70
- } else {
71
- // We try to find a complete configuration, we race so that we don't wait for all trackers to respond
72
- result1 = await raceUntil (
73
- requestFactory . map ( p => p ( ) ) ,
74
- undefined ,
75
- val => {
76
- if ( val ?. res === undefined ) return false
77
- return universal . genericCoderFor ( val . res . version ) . config . isComplete ( val . res )
78
- }
79
- )
80
66
}
81
67
82
68
if ( result1 ?. res ) {
@@ -90,10 +76,7 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf
90
76
// but we try to combine all results anyway
91
77
const tmptracker = new LocalConfigTracker ( undefined as any ) // TODO: Fix this, provider not needed anyway
92
78
93
- const results = await allSafe (
94
- requestFactory . map ( p => p ( ) ) ,
95
- undefined
96
- )
79
+ const results = await allSafe ( requests , undefined )
97
80
98
81
for ( const r of results ) {
99
82
if ( r ?. res ) await tmptracker . saveWalletConfig ( { config : r . res } )
@@ -117,16 +100,18 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf
117
100
wallet : string
118
101
} ) : Promise < { imageHash : string ; context : commons . context . WalletContext } | undefined > {
119
102
let imageHash : { imageHash : string ; context : commons . context . WalletContext } | undefined
120
- const requestFactory = this . trackers . map ( t => ( ) => t . imageHashOfCounterfactualWallet ( args ) )
103
+ const requests = this . trackers . map ( t => t . imageHashOfCounterfactualWallet ( args ) )
121
104
122
105
if ( this . isSerial ) {
123
- imageHash = await serialResolve ( requestFactory , undefined , result => Boolean ( result ) )
106
+ for ( const request of requests ) {
107
+ const result = await request
108
+ if ( result ) {
109
+ imageHash = result
110
+ break
111
+ }
112
+ }
124
113
} else {
125
- imageHash = await raceUntil (
126
- requestFactory . map ( p => p ( ) ) ,
127
- undefined ,
128
- result => Boolean ( result )
129
- )
114
+ imageHash = await raceUntil ( requests , undefined , result => Boolean ( result ) )
130
115
}
131
116
132
117
if ( imageHash ) {
0 commit comments