You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
These new hooks are intended to bring about a new Context System.
Essentially, each namespace (dependency array) may have a context, not just state. It can be used to listen to client events connected to the same key. Since we'd have all clients in the same context, that would also allow us to broadcast arbitrary payloads.
Context System
The existing useSyncState would be modified to return a third variable:
import{useSyncState}from'@robojs/sync'exportfunctionMultiplayerGame(){const[gameState,setGameState,syncContext]=useSyncState(initialGameState,['gameRoom'])const{ clients, clientId, isHost, broadcast }=syncContext// Use the context to implement custom logicuseEffect(()=>{if(isHost){// Perform host-specific initialization}},[isHost])consthandlePlayerAction=(action)=>{// Update the game state locallysetGameState((prev)=>({ ...prev, ...action}))// Broadcast the action to other clientsbroadcast(action)}return(<div><GameBoardstate={gameState}onAction={handlePlayerAction}/><div>Connected Players: {clients.length}</div></div>)}
useSyncBroadcast
This new hook would allow receiving and sending direct broadcasts to other clients in the same dependency array, or both.
It returns a context object and accepts a function as the first argument. The callback receives the payload and context as arguments. Think of it as a useEffect without the boilerplate.
The text was updated successfully, but these errors were encountered:
Pkmmte
changed the title
@robojs/sync - useSyncContext and useSyncBroadcast hooks
@robojs/sync - useSyncBroadcast and useSyncContext hooks
Oct 21, 2024
I’d love to work on this issue. Could you please assign it to me? As I’m new to this repository, I’d also appreciate any guidance to help me get started.
Thank you for your interest in contributing to Robo! This issue was initially tagged as a good first issue, but we then found it involves some deeper integrations that might be challenging as an initial task. In particular due to the way our server and core framework interact with this plugin's websocket connections.
I recommend looking at issue #331 instead, which could be a great place to start. It's more tailored to new contributors and should help you get acquainted with our codebase. I’ve added more details on the exact file to modify and am here (and on Discord) to provide any guidance you need to get started.
Points: 5
These new hooks are intended to bring about a new Context System.
Essentially, each namespace (dependency array) may have a context, not just state. It can be used to listen to client events connected to the same key. Since we'd have all clients in the same context, that would also allow us to broadcast arbitrary payloads.
Context System
The existing
useSyncState
would be modified to return a third variable:The
statusContext
object would include the following fields:Proposed type signature:
Example usage:
useSyncBroadcast
This new hook would allow receiving and sending direct broadcasts to other clients in the same dependency array, or both.
It returns a context object and accepts a function as the first argument. The callback receives the payload and context as arguments. Think of it as a
useEffect
without the boilerplate.Result object includes:
useSyncContext
Can be used to get the context of a dependency array easily.
It can optionally accept an object as an optional first item for convenience callbacks:
The text was updated successfully, but these errors were encountered: