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

[lexical-react] Refactor: Ensure disconnect is called after connection is established in useYjsCollaboration #6619

Merged
merged 4 commits into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/lexical-react/src/shared/useYjsCollaboration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ export function useYjsCollaboration(
): JSX.Element {
const isReloadingDoc = useRef(false);

const connect = useCallback(() => {
provider.connect();
}, [provider]);
const connect = useCallback(() => provider.connect(), [provider]);

const disconnect = useCallback(() => {
try {
Expand Down Expand Up @@ -152,11 +150,14 @@ export function useYjsCollaboration(
}
},
);
connect();

const connectionPromise = connect();

return () => {
if (isReloadingDoc.current === false) {
disconnect();
Promise.resolve(connectionPromise).then(() => {
disconnect();
});
Comment on lines +158 to +160
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is a timing difference between Promise.resolve(undefined).then(f) and f() because the former executes during the next microtask, but I think generally speaking that should be fine unless someone has some overly sensitive unit tests.

I guess if someone really did need the existing timing behavior they could use something like:

// undefined when connected
let connectingPromise: Promise<undefined> | undefined = connect()?.then(() => {
  connectingPromise = undefined;
});
connectingPromise?.then(() => disconnect()) ?? disconnect();

}

provider.off('sync', onSync);
Expand Down
Loading