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
c5977740
Commit
c5977740
authored
Nov 02, 2022
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rewrite to class
parent
dc8dce68
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
119 additions
and
9 deletions
+119
-9
useApiSocket.tsx
lib/hooks/useApiSocket.tsx
+2
-2
Socket.ts
lib/socket/Socket.ts
+103
-0
types.ts
lib/socket/types.ts
+6
-0
Home.tsx
ui/pages/Home.tsx
+8
-7
No files found.
lib/hooks/useApiSocket.tsx
View file @
c5977740
...
@@ -13,7 +13,7 @@ interface Params {
...
@@ -13,7 +13,7 @@ interface Params {
type
SocketData
=
[
null
,
null
,
string
,
string
,
Record
<
string
,
unknown
>
];
type
SocketData
=
[
null
,
null
,
string
,
string
,
Record
<
string
,
unknown
>
];
interface
SocketChannelSubscriber
{
interface
SocketChannelSubscriber
{
filters
?:
Array
<
string
>
;
filters
?:
Array
<
string
>
;
callback
:
(
payload
:
unknown
)
=>
void
;
onMessage
:
(
payload
:
unknown
)
=>
void
;
}
}
export
default
function
useApiSocket
({
onOpen
,
onError
,
onClose
}:
Params
)
{
export
default
function
useApiSocket
({
onOpen
,
onError
,
onClose
}:
Params
)
{
...
@@ -82,7 +82,7 @@ export default function useApiSocket({ onOpen, onError, onClose }: Params) {
...
@@ -82,7 +82,7 @@ export default function useApiSocket({ onOpen, onError, onClose }: Params) {
const
subscribers
=
channels
.
current
[
channelId
];
const
subscribers
=
channels
.
current
[
channelId
];
subscribers
subscribers
?.
filter
((
subscriber
)
=>
subscriber
.
filters
?
subscriber
.
filters
.
includes
(
filterId
)
:
true
)
?.
filter
((
subscriber
)
=>
subscriber
.
filters
?
subscriber
.
filters
.
includes
(
filterId
)
:
true
)
?.
forEach
((
subscriber
)
=>
subscriber
.
callback
(
payload
));
?.
forEach
((
subscriber
)
=>
subscriber
.
onMessage
(
payload
));
});
});
socket
.
current
.
addEventListener
(
'
error
'
,
(
event
)
=>
{
socket
.
current
.
addEventListener
(
'
error
'
,
(
event
)
=>
{
...
...
lib/socket/Socket.ts
0 → 100644
View file @
c5977740
import
type
{
SocketData
,
SocketChannelSubscriber
}
from
'
lib/socket/types
'
;
import
{
SECOND
}
from
'
lib/consts
'
;
interface
InitParams
{
onOpen
?:
(
event
:
Event
)
=>
void
;
onError
?:
(
event
:
Event
)
=>
void
;
onClose
?:
(
event
:
Event
)
=>
void
;
}
const
OPEN_STATE
=
1
;
class
Socket
{
private
socket
:
WebSocket
|
undefined
;
private
heartBeatIntervalId
:
number
|
undefined
;
private
onReadyEvents
:
Array
<
SocketData
>
=
[];
private
channels
:
Record
<
string
,
Array
<
SocketChannelSubscriber
>>
=
{};
init
({
onOpen
,
onError
,
onClose
}:
InitParams
)
{
if
(
this
.
socket
)
{
return
this
;
}
// todo_tom pass host and base path from config
this
.
socket
=
new
WebSocket
(
'
wss://blockscout.com/poa/core/socket/v2/websocket?vsn=2.0.0
'
);
this
.
socket
.
addEventListener
(
'
open
'
,
(
event
:
Event
)
=>
{
this
.
startHeartBeat
();
onOpen
?.(
event
);
this
.
onReadyEvents
.
forEach
((
data
)
=>
this
.
socket
?.
send
(
JSON
.
stringify
(
data
)));
this
.
onReadyEvents
=
[];
});
this
.
socket
.
addEventListener
(
'
message
'
,
(
event
)
=>
{
const
data
:
SocketData
=
JSON
.
parse
(
event
.
data
);
const
channelId
=
data
[
2
];
const
filterId
=
data
[
3
];
const
payload
=
data
[
4
];
const
subscribers
=
this
.
channels
[
channelId
];
subscribers
?.
filter
((
subscriber
)
=>
subscriber
.
filters
?
subscriber
.
filters
.
includes
(
filterId
)
:
true
)
?.
forEach
((
subscriber
)
=>
subscriber
.
onMessage
(
payload
));
});
this
.
socket
.
addEventListener
(
'
error
'
,
(
event
)
=>
{
onError
?.(
event
);
});
this
.
socket
.
addEventListener
(
'
close
'
,
(
event
)
=>
{
onClose
?.(
event
);
});
return
this
;
}
close
()
{
window
.
clearInterval
(
this
.
heartBeatIntervalId
);
this
.
socket
?.
close
();
this
.
socket
=
undefined
;
this
.
onReadyEvents
=
[];
this
.
channels
=
{};
}
joinRoom
(
id
:
string
,
subscriber
:
SocketChannelSubscriber
)
{
const
data
:
SocketData
=
[
null
,
null
,
id
,
'
phx_join
'
,
{}
];
if
(
this
.
socket
?.
readyState
===
OPEN_STATE
)
{
this
.
socket
?.
send
(
JSON
.
stringify
(
data
));
}
else
{
this
.
onReadyEvents
.
push
(
data
);
}
if
(
this
.
channels
[
id
])
{
this
.
channels
[
id
].
push
(
subscriber
);
}
else
{
this
.
channels
[
id
]
=
[
subscriber
];
}
}
leaveRoom
(
id
:
string
)
{
// todo_tom remove only specified subscriber
const
data
:
SocketData
=
[
null
,
null
,
id
,
'
phx_leave
'
,
{}
];
if
(
this
.
socket
?.
readyState
===
OPEN_STATE
)
{
this
.
socket
?.
send
(
JSON
.
stringify
(
data
));
}
else
{
this
.
onReadyEvents
.
push
(
data
);
}
this
.
channels
[
id
]
=
[];
}
private
startHeartBeat
()
{
this
.
heartBeatIntervalId
=
window
.
setInterval
(()
=>
{
const
data
:
SocketData
=
[
null
,
null
,
'
phoenix
'
,
'
heartbeat
'
,
{}
];
this
.
socket
?.
send
(
JSON
.
stringify
(
data
));
},
30
*
SECOND
);
}
}
export
default
Socket
;
lib/socket/types.ts
0 → 100644
View file @
c5977740
export
type
SocketData
=
[
null
,
null
,
string
,
string
,
Record
<
string
,
unknown
>
];
export
interface
SocketChannelSubscriber
{
filters
?:
Array
<
string
>
;
onMessage
:
(
payload
:
unknown
)
=>
void
;
}
ui/pages/Home.tsx
View file @
c5977740
...
@@ -5,8 +5,8 @@ import React from 'react';
...
@@ -5,8 +5,8 @@ import React from 'react';
import
appConfig
from
'
configs/app/config
'
;
import
appConfig
from
'
configs/app/config
'
;
import
*
as
cookies
from
'
lib/cookies
'
;
import
*
as
cookies
from
'
lib/cookies
'
;
import
useApiSocket
from
'
lib/hooks/useApiSocket
'
;
import
useToast
from
'
lib/hooks/useToast
'
;
import
useToast
from
'
lib/hooks/useToast
'
;
import
Socket
from
'
lib/socket/Socket
'
;
import
Page
from
'
ui/shared/Page/Page
'
;
import
Page
from
'
ui/shared/Page/Page
'
;
import
PageTitle
from
'
ui/shared/Page/PageTitle
'
;
import
PageTitle
from
'
ui/shared/Page/PageTitle
'
;
...
@@ -15,23 +15,24 @@ const Home = () => {
...
@@ -15,23 +15,24 @@ const Home = () => {
const
[
isFormVisible
,
setFormVisibility
]
=
React
.
useState
(
false
);
const
[
isFormVisible
,
setFormVisibility
]
=
React
.
useState
(
false
);
const
[
token
,
setToken
]
=
React
.
useState
(
''
);
const
[
token
,
setToken
]
=
React
.
useState
(
''
);
const
{
joinRoom
,
leaveRoom
}
=
useApiSocket
({});
React
.
useEffect
(()
=>
{
React
.
useEffect
(()
=>
{
joinRoom
(
'
blocks:0xdc4765d9dabf6c6c4908fe97e649ef1f05cb6252
'
,
{
const
socket
=
(
new
Socket
).
init
({});
socket
.
joinRoom
(
'
blocks:0xdc4765d9dabf6c6c4908fe97e649ef1f05cb6252
'
,
{
filters
:
[
'
new_block
'
],
filters
:
[
'
new_block
'
],
callback
:
()
=>
{},
onMessage
:
()
=>
{},
});
});
return
()
=>
{
return
()
=>
{
leaveRoom
(
'
blocks:0xdc4765d9dabf6c6c4908fe97e649ef1f05cb6252
'
);
socket
.
leaveRoom
(
'
blocks:0xdc4765d9dabf6c6c4908fe97e649ef1f05cb6252
'
);
socket
.
close
();
};
};
},
[
joinRoom
,
leaveRoom
]);
},
[]);
React
.
useEffect
(()
=>
{
React
.
useEffect
(()
=>
{
const
token
=
cookies
.
get
(
cookies
.
NAMES
.
API_TOKEN
);
const
token
=
cookies
.
get
(
cookies
.
NAMES
.
API_TOKEN
);
setFormVisibility
(
Boolean
(
!
token
&&
appConfig
.
isAccountSupported
));
setFormVisibility
(
Boolean
(
!
token
&&
appConfig
.
isAccountSupported
));
},
[
joinRoom
]);
},
[]);
const
checkSentry
=
React
.
useCallback
(()
=>
{
const
checkSentry
=
React
.
useCallback
(()
=>
{
Sentry
.
captureException
(
new
Error
(
'
Test error
'
),
{
extra
:
{
foo
:
'
bar
'
},
tags
:
{
source
:
'
test
'
}
});
Sentry
.
captureException
(
new
Error
(
'
Test error
'
),
{
extra
:
{
foo
:
'
bar
'
},
tags
:
{
source
:
'
test
'
}
});
...
...
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