Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

[docs] initial docs for syncing state #159

Merged
merged 1 commit into from
May 25, 2020

Conversation

drarmstr
Copy link
Contributor

resolves #143
resolves #149

@drarmstr drarmstr added the documentation Improvements or additions to documentation label May 25, 2020
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label May 25, 2020
@drarmstr drarmstr merged commit e377043 into facebookexperimental:docs May 25, 2020
```jsx
function CurrentUserIDSubscription() {
const [currentUserID, setCurrentUserID] = useRecoilState(currentUserIDState);
let knownServerCurrentUserID;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

knownServerCurrentUserID should be stored in React.useState so that changes to it will be able to trigger the useEffect

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated it with useRef; what I get for writing at 3am.. ;)

const setStatus = useSetRecoilState(friendStatusState(id));

useEffect(() => {
RemoteStateAPI.subscribeToFriendStatus(id, setStatus);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth making it explicitly clear that it should be taken into consideration that if this hook is used in multiple places, each instance will separately create a subscription?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I can add a note. So far, syncing remote mutable state via Recoil isn't a common use-case internally, so we can do some more iteration to provide a more robust example/approach for this use-case. It would be nice to incorporate more of a singleton pattern here. But, as also mentioned, lifetimes of components using a friend directly may not match lifetimes of selectors using a friend. The cleaner answer here may also be related to async query cancelation and memory management for atom/selector lifetime. If we have an API for that we could perhaps use it for this as well.

};
}, []);

// Subscribe atom changes to update server state

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it's worth making this a little bit more robust, e.g. by including timestamp comparisons, or version counters.
Right now I think there is a race condition if the Recoil state is updated at roughly the same time that the remote values changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also suffers from redundant setting of the local atom if the user sets it and the server mirrors back the change to all subscribers, though the value should be correct. Yeah, just wanted a simplified example, but I'm not very keen on it so far. Could definitely be improved.

@drarmstr
Copy link
Contributor Author

@acutmore - some minor tweaks in #173

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants