Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
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
vicotor
frontend
Commits
0d997da5
Commit
0d997da5
authored
Nov 25, 2022
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gradual increment of new txs num
parent
c8bb1097
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
4 deletions
+53
-4
useGradualIncrement.tsx
lib/hooks/useGradualIncrement.tsx
+48
-0
useNewTxsSocket.tsx
lib/hooks/useNewTxsSocket.tsx
+5
-4
No files found.
lib/hooks/useGradualIncrement.tsx
0 → 100644
View file @
0d997da5
import
_clamp
from
'
lodash/clamp
'
;
import
React
from
'
react
'
;
const
MAX_DELAY
=
500
;
const
MIN_DELAY
=
50
;
export
default
function
useGradualIncrement
(
initialValue
:
number
):
[
number
,
(
inc
:
number
)
=>
void
]
{
const
[
num
,
setNum
]
=
React
.
useState
(
initialValue
);
const
queue
=
React
.
useRef
<
number
>
(
0
);
const
timeoutId
=
React
.
useRef
(
0
);
const
incrementDelayed
=
React
.
useCallback
(()
=>
{
if
(
queue
.
current
===
0
)
{
return
;
}
queue
.
current
--
;
setNum
(
prev
=>
prev
+
1
);
timeoutId
.
current
=
0
;
},
[]);
const
increment
=
React
.
useCallback
((
inc
:
number
)
=>
{
if
(
inc
<
1
)
{
return
;
}
queue
.
current
+=
inc
;
if
(
!
timeoutId
.
current
)
{
timeoutId
.
current
=
window
.
setTimeout
(
incrementDelayed
,
0
);
}
},
[
incrementDelayed
]);
React
.
useEffect
(()
=>
{
if
(
queue
.
current
>
0
&&
!
timeoutId
.
current
)
{
const
delay
=
_clamp
(
MAX_DELAY
/
queue
.
current
*
1.5
,
MIN_DELAY
,
MAX_DELAY
);
timeoutId
.
current
=
window
.
setTimeout
(
incrementDelayed
,
delay
);
}
},
[
incrementDelayed
,
num
]);
React
.
useEffect
(()
=>
{
return
()
=>
{
window
.
clearTimeout
(
timeoutId
.
current
);
};
},
[]);
return
[
num
,
increment
];
}
lib/hooks/useNewTxsSocket.tsx
View file @
0d997da5
...
@@ -2,6 +2,7 @@ import type { NextRouter } from 'next/router';
...
@@ -2,6 +2,7 @@ import type { NextRouter } from 'next/router';
import
{
useRouter
}
from
'
next/router
'
;
import
{
useRouter
}
from
'
next/router
'
;
import
React
from
'
react
'
;
import
React
from
'
react
'
;
import
useGradualIncrement
from
'
lib/hooks/useGradualIncrement
'
;
import
{
ROUTES
}
from
'
lib/link/routes
'
;
import
{
ROUTES
}
from
'
lib/link/routes
'
;
import
useSocketChannel
from
'
lib/socket/useSocketChannel
'
;
import
useSocketChannel
from
'
lib/socket/useSocketChannel
'
;
import
useSocketMessage
from
'
lib/socket/useSocketMessage
'
;
import
useSocketMessage
from
'
lib/socket/useSocketMessage
'
;
...
@@ -36,19 +37,19 @@ function assertIsNewPendingTxResponse(response: unknown): response is { pending_
...
@@ -36,19 +37,19 @@ function assertIsNewPendingTxResponse(response: unknown): response is { pending_
export
default
function
useNewTxsSocket
()
{
export
default
function
useNewTxsSocket
()
{
const
router
=
useRouter
();
const
router
=
useRouter
();
const
[
num
,
setNum
]
=
React
.
useState
(
0
);
const
[
num
,
setNum
]
=
useGradualIncrement
(
0
);
const
[
socketAlert
,
setSocketAlert
]
=
React
.
useState
(
''
);
const
[
socketAlert
,
setSocketAlert
]
=
React
.
useState
(
''
);
const
{
topic
,
event
}
=
getSocketParams
(
router
);
const
{
topic
,
event
}
=
getSocketParams
(
router
);
const
handleNewTxMessage
=
React
.
useCallback
((
response
:
{
transaction
:
number
}
|
{
pending_transaction
:
number
}
|
unknown
)
=>
{
const
handleNewTxMessage
=
React
.
useCallback
((
response
:
{
transaction
:
number
}
|
{
pending_transaction
:
number
}
|
unknown
)
=>
{
if
(
assertIsNewTxResponse
(
response
))
{
if
(
assertIsNewTxResponse
(
response
))
{
setNum
(
(
prev
)
=>
prev
+
response
.
transaction
);
setNum
(
response
.
transaction
);
}
}
if
(
assertIsNewPendingTxResponse
(
response
))
{
if
(
assertIsNewPendingTxResponse
(
response
))
{
setNum
(
(
prev
)
=>
prev
+
response
.
pending_transaction
);
setNum
(
response
.
pending_transaction
);
}
}
},
[]);
},
[
setNum
]);
const
handleSocketClose
=
React
.
useCallback
(()
=>
{
const
handleSocketClose
=
React
.
useCallback
(()
=>
{
setSocketAlert
(
'
Connection is lost. Please click here to load new transactions.
'
);
setSocketAlert
(
'
Connection is lost. Please click here to load new transactions.
'
);
...
...
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