Skip to content

Commit

Permalink
[lexical-react] Refactor: Ensure disconnect is called after connectio…
Browse files Browse the repository at this point in the history
…n is established in useYjsCollaboration (#6619)

Co-authored-by: Bob Ippolito <bob@redivi.com>
  • Loading branch information
smworld01 and etrepum committed Sep 11, 2024
1 parent 01e2ae4 commit f50f168
Showing 1 changed file with 6 additions and 5 deletions.
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();
});
}

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

0 comments on commit f50f168

Please sign in to comment.