Skip to content

Commit

Permalink
using promiseFactory as base instead of Promise[]
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlee348 committed Nov 11, 2024
1 parent f5d6117 commit 7220a3c
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions packages/sessions/src/trackers/multiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,25 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf
) {}

async configOfImageHash(args: { imageHash: string }): Promise<commons.config.Config | undefined> {
const requests = this.trackers.map(async (t, i) => ({ res: await t.configOfImageHash(args), i }))
const requestFactory = this.trackers.map((t, i) => async () => ({ res: await t.configOfImageHash(args), i }))

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

if (this.isSerial) {
result1 = await serialResolve(
requests.map(p => () => p),
result1 = await serialResolve(requestFactory, undefined, val => {
if (val?.res === undefined) return false
return universal.genericCoderFor(val.res.version).config.isComplete(val.res)
})
} else {
// We try to find a complete configuration, we race so that we don't wait for all trackers to respond
result1 = await raceUntil(
requestFactory.map(p => p()),
undefined,
val => {
if (val?.res === undefined) return false
return universal.genericCoderFor(val.res.version).config.isComplete(val.res)
}
)
} else {
// We try to find a complete configuration, we race so that we don't wait for all trackers to respond
result1 = await raceUntil(requests, undefined, val => {
if (val?.res === undefined) return false
return universal.genericCoderFor(val.res.version).config.isComplete(val.res)
})
}

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

const results = await allSafe(requests, undefined)
const results = await allSafe(
requestFactory.map(p => p()),
undefined
)

for (const r of results) {
if (r?.res) await tmptracker.saveWalletConfig({ config: r.res })
Expand All @@ -114,16 +117,16 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf
wallet: string
}): Promise<{ imageHash: string; context: commons.context.WalletContext } | undefined> {
let imageHash: { imageHash: string; context: commons.context.WalletContext } | undefined
const requests = this.trackers.map(t => t.imageHashOfCounterfactualWallet(args))
const requestFactory = this.trackers.map(t => () => t.imageHashOfCounterfactualWallet(args))

if (this.isSerial) {
imageHash = await serialResolve(
requests.map(p => () => p),
imageHash = await serialResolve(requestFactory, undefined, result => Boolean(result))
} else {
imageHash = await raceUntil(
requestFactory.map(p => p()),
undefined,
result => Boolean(result)
)
} else {
imageHash = await raceUntil(requests, undefined, result => Boolean(result))
}

if (imageHash) {
Expand Down

0 comments on commit 7220a3c

Please sign in to comment.