feat: add event listeners for player, sound, and user events in SocketProvider
This commit is contained in:
@@ -2,7 +2,7 @@ import React, { createContext, useContext, useEffect, useState, useCallback } fr
|
||||
import { io, Socket } from 'socket.io-client'
|
||||
import { toast } from 'sonner'
|
||||
import { useAuth } from './AuthContext'
|
||||
import { authEvents, AUTH_EVENTS } from '../lib/events'
|
||||
import { authEvents, AUTH_EVENTS, soundEvents, SOUND_EVENTS, userEvents, USER_EVENTS, playerEvents, PLAYER_EVENTS } from '../lib/events'
|
||||
|
||||
interface SocketContextType {
|
||||
socket: Socket | null
|
||||
@@ -64,6 +64,21 @@ export function SocketProvider({ children }: SocketProviderProps) {
|
||||
})
|
||||
})
|
||||
|
||||
// Listen for player events and emit them locally
|
||||
newSocket.on('player_state', (data) => {
|
||||
playerEvents.emit(PLAYER_EVENTS.PLAYER_STATE, data)
|
||||
})
|
||||
|
||||
// Listen for sound events and emit them locally
|
||||
newSocket.on('sound_played', (data) => {
|
||||
soundEvents.emit(SOUND_EVENTS.SOUND_PLAYED, data)
|
||||
})
|
||||
|
||||
// Listen for user events and emit them locally
|
||||
newSocket.on('user_credits_changed', (data) => {
|
||||
userEvents.emit(USER_EVENTS.USER_CREDITS_CHANGED, data)
|
||||
})
|
||||
|
||||
return newSocket
|
||||
}, [user])
|
||||
|
||||
@@ -92,6 +107,7 @@ export function SocketProvider({ children }: SocketProviderProps) {
|
||||
}
|
||||
}, [handleTokenRefresh])
|
||||
|
||||
|
||||
// Initial socket connection
|
||||
useEffect(() => {
|
||||
if (loading) return
|
||||
|
||||
Reference in New Issue
Block a user