Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor react hooks #259

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

greg-schrammel
Copy link

@greg-schrammel greg-schrammel commented Aug 16, 2023

currently every time you call usePartySocket() it creates a new PartySocket() new connection

The idea is to have a PartySocketProvider that defines which connection it's children are listening to, or a global singleton that holds all open connections

here's my contrived example to illustrate

function MyGame() {
  return (
  <PartySocketProvider host="localhost:1999/position" room="game1">
    <Player id={1} />
    <Player id={2} />
    <Player id={3} />
    
     <PartySocketProvider host="localhost:1999/chat" room="game1">
       <Chat />
     </PartySocketProvider>
     
  </PartySocketProvider>)
}

function Player() {
  const [position, setPosition] = useState()
  
  usePartySocket({
    // you could pass options here for a "provider-less" mode
    // host: 'localhost:1999/position',
    // room: 'game1',
    onMessage: (msg) => {
      const { player, position } = JSON.parse(msg.data)
      if (player === id) setPosition(position)
    }
  })

  return ...
}

with this pr changes each <Player /> rerenders independently when it's position change, while using a single connection

@changeset-bot
Copy link

changeset-bot bot commented Aug 16, 2023

⚠️ No Changeset found

Latest commit: e4dc3b1

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@CLAassistant
Copy link

CLAassistant commented Aug 16, 2023

CLA assistant check
All committers have signed the CLA.

@greg-schrammel greg-schrammel marked this pull request as draft August 16, 2023 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants