Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed May 6, 2024
1 parent ba27aa1 commit 93d0d59
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 47 deletions.
38 changes: 19 additions & 19 deletions packages/sushi/src/router/data-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export type DataFetcherOptions = {
/** Determines if memoizer should be used or not */
memoize?: boolean
/** Determines a timeout (in ms) for fetching pools for a token pair */
fetchPoolsTimeout?: number,
fetchPoolsTimeout?: number
}

// TODO: Should be a mode on the config for DataFetcher
Expand Down Expand Up @@ -238,20 +238,20 @@ export class DataFetcher {
try {
options?.fetchPoolsTimeout
? await promiseTimeout(
provider.fetchPoolsForToken(
provider.fetchPoolsForToken(
currency0.wrapped,
currency1.wrapped,
excludePools,
options,
),
options.fetchPoolsTimeout,
)
: await provider.fetchPoolsForToken(
currency0.wrapped,
currency1.wrapped,
excludePools,
options,
),
options.fetchPoolsTimeout
)
: await provider.fetchPoolsForToken(
currency0.wrapped,
currency1.wrapped,
excludePools,
options,
)
)
} catch {
/**/
}
Expand All @@ -262,24 +262,24 @@ export class DataFetcher {
currency0.wrapped.sortsBefore(currency1.wrapped)
? [currency0.wrapped, currency1.wrapped]
: [currency1.wrapped, currency0.wrapped]
try {
options?.fetchPoolsTimeout
? await promiseTimeout(
try {
options?.fetchPoolsTimeout
? await promiseTimeout(
Promise.allSettled(
this.providers.map((p) =>
p.fetchPoolsForToken(token0, token1, excludePools, options),
),
),
options.fetchPoolsTimeout
options.fetchPoolsTimeout,
)
: await Promise.allSettled(
: await Promise.allSettled(
this.providers.map((p) =>
p.fetchPoolsForToken(token0, token1, excludePools, options),
),
)
} catch {
/**/
}
} catch {
/**/
}
}
}

Expand Down
27 changes: 12 additions & 15 deletions packages/sushi/src/router/timeout.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
import { describe, expect, it } from 'vitest'
import { describe, it } from 'vitest'
import { promiseTimeout } from './timeout.js'

const sleep = async(ms: number, msg = "") => {
let _timeoutReference;
return new Promise(
resolve => _timeoutReference = setTimeout(() => resolve(msg), ms),
).finally(
() => clearTimeout(_timeoutReference)
);
};
const sleep = async (ms: number, msg = '') => {
let _timeoutReference
return new Promise((resolve) => {
_timeoutReference = setTimeout(() => resolve(msg), ms)
return _timeoutReference
}).finally(() => clearTimeout(_timeoutReference))
}

describe('Promise Timeout', async () => {
it('should timeout', async () => {
const mainPromise = sleep(1000);
const mainPromise = sleep(1000)
await promiseTimeout(mainPromise, 500)
.then(() => {
throw "expected to reject, but resolved"
throw 'expected to reject, but resolved'
})
.catch(() => {
/**/
})

})
it('should NOT timeout', async () => {
const mainPromise = sleep(1000);
const mainPromise = sleep(1000)
await promiseTimeout(mainPromise, 1500)
.then(() => {
/**/
})
.catch(() => {
throw "expected to resolve, but rejected"
throw 'expected to resolve, but rejected'
})

})
})
23 changes: 13 additions & 10 deletions packages/sushi/src/router/timeout.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
export async function promiseTimeout (promise: Promise<any>, time: number, exception?: any) {
let timer: any;
export async function promiseTimeout(
promise: Promise<any>,
time: number,
exception?: any,
) {
let timer: any
return Promise.race([
promise,
new Promise(
(_res, _rej) => timer = setTimeout(_rej, time, exception)
)
]).finally(
() => clearTimeout(timer)
);
};
promise,
new Promise((_res, _rej) => {
timer = setTimeout(_rej, time, exception)
return timer
}),
]).finally(() => clearTimeout(timer))
}
8 changes: 5 additions & 3 deletions protocols/route-processor/test/DataFetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ async function testDF(
) {
if (!t0 || !t1) return
const start = performance.now()
await dataFetcher.fetchPoolsForToken(t0, t1, undefined, { fetchPoolsTimeout: 10000 })
await dataFetcher.fetchPoolsForToken(t0, t1, undefined, {
fetchPoolsTimeout: 10000,
})
const pools = dataFetcher.getCurrentPoolCodeMap(t0, t1)
const time = Math.round(performance.now() - start)
console.log(
Expand Down Expand Up @@ -52,9 +54,9 @@ async function runTest() {
chName,
dataFetcher,
SUSHI[chainId as keyof typeof SUSHI],
USDT[chainId as keyof typeof USDT],
FRAX[chainId as keyof typeof FRAX],
'SUSHI',
'USDT',
'FRAX',
)
await testDF(
chName,
Expand Down

0 comments on commit 93d0d59

Please sign in to comment.