Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
interface
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
LuckySwap
interface
Commits
600aeaaf
Unverified
Commit
600aeaaf
authored
Apr 06, 2022
by
Zach Pomerantz
Committed by
GitHub
Apr 06, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: polling memory leak (#3676)
* chore: clarify stale callback * fix: polling memory leak
parent
3bfbc74e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
7 additions
and
7 deletions
+7
-7
useClientSideSmartOrderRouterTrade.ts
src/lib/hooks/routing/useClientSideSmartOrderRouterTrade.ts
+1
-1
usePoll.ts
src/lib/hooks/usePoll.ts
+6
-6
No files found.
src/lib/hooks/routing/useClientSideSmartOrderRouterTrade.ts
View file @
600aeaaf
...
@@ -92,7 +92,7 @@ export default function useClientSideSmartOrderRouterTrade<TTradeType extends Tr
...
@@ -92,7 +92,7 @@ export default function useClientSideSmartOrderRouterTrade<TTradeType extends Tr
const
getIsValidBlock
=
useGetIsValidBlock
()
const
getIsValidBlock
=
useGetIsValidBlock
()
const
{
data
:
quoteResult
,
error
}
=
usePoll
(
getQuoteResult
,
JSON
.
stringify
(
queryArgs
),
{
const
{
data
:
quoteResult
,
error
}
=
usePoll
(
getQuoteResult
,
JSON
.
stringify
(
queryArgs
),
{
debounce
:
isDebouncing
,
debounce
:
isDebouncing
,
staleCallback
:
useCallback
(({
data
})
=>
!
getIsValidBlock
(
Number
(
data
?.
blockNumber
)
||
0
),
[
getIsValidBlock
]),
isStale
:
useCallback
(({
data
})
=>
!
getIsValidBlock
(
Number
(
data
?.
blockNumber
)
||
0
),
[
getIsValidBlock
]),
})
??
{
})
??
{
error
:
undefined
,
error
:
undefined
,
}
}
...
...
src/lib/hooks/usePoll.ts
View file @
600aeaaf
...
@@ -9,7 +9,7 @@ interface PollingOptions<T> {
...
@@ -9,7 +9,7 @@ interface PollingOptions<T> {
debounce
?:
boolean
debounce
?:
boolean
// If stale, any cached result will be returned, and a new fetch will be initiated.
// If stale, any cached result will be returned, and a new fetch will be initiated.
staleCallback
?:
(
value
:
T
)
=>
boolean
isStale
?:
(
value
:
T
)
=>
boolean
pollingInterval
?:
number
pollingInterval
?:
number
keepUnusedDataFor
?:
number
keepUnusedDataFor
?:
number
...
@@ -25,7 +25,7 @@ export default function usePoll<T>(
...
@@ -25,7 +25,7 @@ export default function usePoll<T>(
key
=
''
,
key
=
''
,
{
{
debounce
=
false
,
debounce
=
false
,
staleCallback
,
isStale
,
pollingInterval
=
DEFAULT_POLLING_INTERVAL
,
pollingInterval
=
DEFAULT_POLLING_INTERVAL
,
keepUnusedDataFor
=
DEFAULT_KEEP_UNUSED_DATA_FOR
,
keepUnusedDataFor
=
DEFAULT_KEEP_UNUSED_DATA_FOR
,
}:
PollingOptions
<
T
>
}:
PollingOptions
<
T
>
...
@@ -39,11 +39,10 @@ export default function usePoll<T>(
...
@@ -39,11 +39,10 @@ export default function usePoll<T>(
let
timeout
:
number
let
timeout
:
number
const
entry
=
cache
.
get
(
key
)
const
entry
=
cache
.
get
(
key
)
const
isStale
=
staleCallback
&&
entry
?.
result
!==
undefined
?
staleCallback
(
entry
.
result
)
:
false
if
(
entry
)
{
if
(
entry
)
{
// If there is not a pending fetch (and there should be), queue one.
// If there is not a pending fetch (and there should be), queue one.
if
(
entry
.
ttl
)
{
if
(
entry
.
ttl
)
{
if
(
isStale
)
{
if
(
isStale
&&
entry
?.
result
!==
undefined
?
isStale
(
entry
.
result
)
:
false
)
{
poll
()
// stale results should be refetched immediately
poll
()
// stale results should be refetched immediately
}
else
if
(
entry
.
ttl
&&
entry
.
ttl
+
keepUnusedDataFor
>
Date
.
now
())
{
}
else
if
(
entry
.
ttl
&&
entry
.
ttl
+
keepUnusedDataFor
>
Date
.
now
())
{
timeout
=
setTimeout
(
poll
,
Math
.
max
(
0
,
entry
.
ttl
-
Date
.
now
()))
timeout
=
setTimeout
(
poll
,
Math
.
max
(
0
,
entry
.
ttl
-
Date
.
now
()))
...
@@ -57,6 +56,7 @@ export default function usePoll<T>(
...
@@ -57,6 +56,7 @@ export default function usePoll<T>(
return
()
=>
{
return
()
=>
{
clearTimeout
(
timeout
)
clearTimeout
(
timeout
)
timeout
=
0
}
}
async
function
poll
(
ttl
=
Date
.
now
()
+
pollingInterval
)
{
async
function
poll
(
ttl
=
Date
.
now
()
+
pollingInterval
)
{
...
@@ -66,9 +66,9 @@ export default function usePoll<T>(
...
@@ -66,9 +66,9 @@ export default function usePoll<T>(
// Always set the result in the cache, but only set it as data if the key is still being queried.
// Always set the result in the cache, but only set it as data if the key is still being queried.
const
result
=
await
fetch
()
const
result
=
await
fetch
()
cache
.
set
(
key
,
{
ttl
,
result
})
cache
.
set
(
key
,
{
ttl
,
result
})
setData
((
data
)
=>
(
data
.
key
===
key
?
{
key
,
result
}
:
data
))
if
(
timeout
)
setData
((
data
)
=>
(
data
.
key
===
key
?
{
key
,
result
}
:
data
))
}
}
},
[
cache
,
debounce
,
fetch
,
keepUnusedDataFor
,
key
,
pollingInterval
,
staleCallback
])
},
[
cache
,
debounce
,
fetch
,
isStale
,
keepUnusedDataFor
,
key
,
pollingInterval
])
useEffect
(()
=>
{
useEffect
(()
=>
{
// Cleanup stale entries when a new key is used.
// Cleanup stale entries when a new key is used.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment