-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
[wip] shift to optimistic updates #122
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,13 +41,13 @@ function Lobby() { | |
const location = useLocation() | ||
const [hideShare, setHideShare] = React.useState(false) | ||
const [updateGameSettings] = useUpdateGameSettingsMutation() | ||
const [cardPlayStyle, setCardPlayStyle] = React.useState( | ||
GameCardPlayStyleEnum.PlayersSubmitWords | ||
) | ||
// const [cardPlayStyle, setCardPlayStyle] = React.useState( | ||
// GameCardPlayStyleEnum.PlayersSubmitWords | ||
// ) | ||
|
||
React.useEffect(() => { | ||
setCardPlayStyle(currentGame.card_play_style) | ||
}, [currentGame.card_play_style]) | ||
// React.useEffect(() => { | ||
// setCardPlayStyle(currentGame.card_play_style) | ||
// }, [currentGame.card_play_style]) | ||
Comment on lines
+44
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stop using local state |
||
|
||
const [wordList, setWordList] = React.useState("") | ||
const debouncedSetWordList = React.useRef( | ||
|
@@ -94,16 +94,22 @@ function Lobby() { | |
<> | ||
<div className={classes.section}> | ||
<SettingsSection | ||
cardPlayStyle={cardPlayStyle} | ||
cardPlayStyle={currentGame.card_play_style} | ||
setCardPlayStyle={(value) => { | ||
setCardPlayStyle(value as GameCardPlayStyleEnum) | ||
// setCardPlayStyle(value as GameCardPlayStyleEnum) | ||
updateGameSettings({ | ||
variables: { | ||
id: currentGame.id, | ||
input: { | ||
card_play_style: value as GameCardPlayStyleEnum, | ||
}, | ||
}, | ||
optimisticResponse: { | ||
update_games_by_pk: { | ||
...currentGame, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if two fragments would help / minimize the scope of these setting cache updates, i.e.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most definitely 👍 - there's no use of fragments across this whole codebase yet! Bout damn time. |
||
card_play_style: value as GameCardPlayStyleEnum, | ||
}, | ||
}, | ||
}) | ||
}} | ||
debouncedSetWordList={debouncedSetWordList} | ||
|
@@ -129,7 +135,7 @@ function Lobby() { | |
</Grid> | ||
<WaitingRoom | ||
wordList={wordList} | ||
cardPlayStyle={cardPlayStyle} | ||
cardPlayStyle={currentGame.card_play_style} | ||
></WaitingRoom> | ||
</Grid> | ||
</Grid> | ||
|
@@ -139,7 +145,9 @@ function Lobby() { | |
<> | ||
<Divider variant="middle"></Divider> | ||
<div className={classes.section}> | ||
<SettingsSection cardPlayStyle={cardPlayStyle}></SettingsSection> | ||
<SettingsSection | ||
cardPlayStyle={currentGame.card_play_style} | ||
></SettingsSection> | ||
</div> | ||
</> | ||
)} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
subscribe this way, then can use optimistic response instead of local states.
related to: apollographql/apollo-client#5267
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some interesting behavior here - some flashes on refresh, and two web sockets get opened.
starting to think sub might be overkill for this whole thing, we could prob do with 1 sec polling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I ran into this same thing recently and landed on the same GitHub issue / solution; it worked well.
You're resubscribing a bunch in the current state and missing an
updateQuery
which might help:The subscription and query are the same in this context, so
updateQuery
is dead simple, but it gives you the flexibility to deviate from the query (i.e. only make some things realtime, subscribe to a handful of smaller subscriptions, etc).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea - I was meaning to check if updateQuery defaults to exactly that if not passed in - but will add it explicitly to see the effect.
I also thought direction of flexibility was 👍 e.g... we are subscribing for everything once game is started. But practically... the most settings, players and cards cannot change after the game has started, so based on the game state we could actually run different subscriptions (so long as we close out the previous gracefully).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like it doesn't on glance, but I'm not sure how it would have been working without it now if that's the case 🤔
Yeah, love that.