added useWsEvents hooks

This commit is contained in:
SrGooglo 2023-07-05 19:02:18 +00:00
parent a5ffd235fd
commit c225658d15

View 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
}