feat: add event listeners for player, sound, and user events in SocketProvider

This commit is contained in:
JSC
2025-07-31 21:34:08 +02:00
parent 7b4dd57f1f
commit d2891f4f2b
2 changed files with 35 additions and 1 deletions

View File

@@ -2,7 +2,7 @@ import React, { createContext, useContext, useEffect, useState, useCallback } fr
import { io, Socket } from 'socket.io-client' import { io, Socket } from 'socket.io-client'
import { toast } from 'sonner' import { toast } from 'sonner'
import { useAuth } from './AuthContext' 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 { interface SocketContextType {
socket: Socket | null 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 return newSocket
}, [user]) }, [user])
@@ -92,6 +107,7 @@ export function SocketProvider({ children }: SocketProviderProps) {
} }
}, [handleTokenRefresh]) }, [handleTokenRefresh])
// Initial socket connection // Initial socket connection
useEffect(() => { useEffect(() => {
if (loading) return if (loading) return

View File

@@ -33,6 +33,9 @@ class EventEmitter {
} }
export const authEvents = new EventEmitter() export const authEvents = new EventEmitter()
export const playerEvents = new EventEmitter()
export const soundEvents = new EventEmitter()
export const userEvents = new EventEmitter()
// Auth event types // Auth event types
export const AUTH_EVENTS = { export const AUTH_EVENTS = {
@@ -41,3 +44,18 @@ export const AUTH_EVENTS = {
LOGIN_SUCCESS: 'login_success', LOGIN_SUCCESS: 'login_success',
LOGOUT: 'logout', LOGOUT: 'logout',
} as const } as const
// Player event types
export const PLAYER_EVENTS = {
PLAYER_STATE: 'player_state',
} as const
// Sound event types
export const SOUND_EVENTS = {
SOUND_PLAYED: 'sound_played',
} as const
// User event types
export const USER_EVENTS = {
USER_CREDITS_CHANGED: 'user_credits_changed',
} as const