mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-12 20:14:15 +00:00
added useWsEvents
hooks
This commit is contained in:
parent
a5ffd235fd
commit
c225658d15
34
packages/app/src/hooks/useWsEvents/index.js
Normal file
34
packages/app/src/hooks/useWsEvents/index.js
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import React from "react"
|
||||||
|
|
||||||
|
export default (
|
||||||
|
wsEvents,
|
||||||
|
{
|
||||||
|
socketName
|
||||||
|
} = {}
|
||||||
|
) => {
|
||||||
|
function registerEvents() {
|
||||||
|
for (const [eventName, eventHandler] of Object.entries(wsEvents)) {
|
||||||
|
app.cores.api.listenEvent(eventName, eventHandler, socketName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function unregisterEvents() {
|
||||||
|
for (const [eventName, eventHandler] of Object.entries(wsEvents)) {
|
||||||
|
app.cores.api.unlistenEvent(eventName, eventHandler, socketName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (typeof wsEvents === "function") {
|
||||||
|
wsEvents = [wsEvents]
|
||||||
|
}
|
||||||
|
|
||||||
|
registerEvents()
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
unregisterEvents()
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return wsEvents
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user