Skip to content

Commit 5f6d278

Browse files
committed
removed factory and used simple for loop for isSerial
1 parent 7220a3c commit 5f6d278

File tree

1 file changed

+23
-38
lines changed

1 file changed

+23
-38
lines changed

packages/sessions/src/trackers/multiple.ts

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,6 @@ export function raceUntil<T>(promises: Promise<T>[], fallback: T, evalRes: (val:
3232
})
3333
}
3434

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-
5035
export async function allSafe<T>(promises: Promise<T>[], fallback: T): Promise<T[]> {
5136
return Promise.all(promises.map(promise => promise.catch(() => fallback)))
5237
}
@@ -58,25 +43,26 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf
5843
) {}
5944

6045
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 }))
6247

6348
let result1: { res: commons.config.Config | undefined; i: number } | undefined
6449

6550
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 => {
6763
if (val?.res === undefined) return false
6864
return universal.genericCoderFor(val.res.version).config.isComplete(val.res)
6965
})
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-
)
8066
}
8167

8268
if (result1?.res) {
@@ -90,10 +76,7 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf
9076
// but we try to combine all results anyway
9177
const tmptracker = new LocalConfigTracker(undefined as any) // TODO: Fix this, provider not needed anyway
9278

93-
const results = await allSafe(
94-
requestFactory.map(p => p()),
95-
undefined
96-
)
79+
const results = await allSafe(requests, undefined)
9780

9881
for (const r of results) {
9982
if (r?.res) await tmptracker.saveWalletConfig({ config: r.res })
@@ -117,16 +100,18 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf
117100
wallet: string
118101
}): Promise<{ imageHash: string; context: commons.context.WalletContext } | undefined> {
119102
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))
121104

122105
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+
}
124113
} 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))
130115
}
131116

132117
if (imageHash) {

0 commit comments

Comments
 (0)