Skip to content

set up listener for provider doc reload after disconnection/reconnection - #1409

Merged
btezzxxt merged 2 commits into
mainfrom
collab-rebind-when-provider-reload-doc
Mar 3, 2022
Merged

btezzxxt merged 2 commits into
mainfrom
collab-rebind-when-provider-reload-doc

Conversation

@btezzxxt

@btezzxxt btezzxxt commented Mar 2, 2022

Copy link
Copy Markdown
Contributor

when provider disconnects / reconnects, it pulls latest server copy into a new yjs doc, which means our collab plugin will need to renew our bindings. Adding a new listener listening to 'reload' even from provider. Then recreate the binding from new doc, clear the root in editorState, finally reload latest updates.

@vercel

vercel Bot commented Mar 2, 2022 •

Copy link
Copy Markdown

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployments, click below or on the icon next to each commit.

lexical – ./packages/lexical-website

🔍 Inspect: https://vercel.com/fbopensource/lexical/euH5WCvsSRbToX6urriJG9skdDzF
✅ Preview: https://lexical-git-collab-rebind-when-provider-reload-doc-fbopensource.vercel.app

lexical-playground – ./packages/lexical-playground

🔍 Inspect: https://vercel.com/fbopensource/lexical-playground/J4rrNsBsqii4EK2gZYPpF1Z17LkS
✅ Preview: Failed

[Deployment for e2cbcd4 failed]

@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 Mar 2, 2022
@trueadm

trueadm commented Mar 2, 2022 •

Copy link
Copy Markdown
Collaborator

This shouldn’t need changes to the package-lock?

@btezzxxt

btezzxxt commented Mar 3, 2022

Copy link
Copy Markdown
Contributor Author

yeah, I've removed the change to package-lock.json

@btezzxxt
btezzxxt merged commit 3a6461c into main Mar 3, 2022
@trueadm
trueadm deleted the collab-rebind-when-provider-reload-doc branch April 6, 2022 15:21
acywatson pushed a commit that referenced this pull request Apr 9, 2022
…ion (#1409)

* set up listener for provider doc reload after disconnection/reconnection

* fixing package-lock.json
etrepum pushed a commit to etrepum/lexical that referenced this pull request Aug 29, 2026
… replaced

## Description

meta#9051 makes `CollaborationPlugin` build a new `Provider` when `providerFactory`,
`id` or `yjsDocMap` change. The `Binding` did not follow. It was guarded by an
`isBindingInitialized` ref, and because React runs an effect's cleanup before
every re-run, a change to any of that effect's dependencies destroyed the
`Binding` and then took the early return -- leaving a destroyed `Binding` in
state. That fires on the very case meta#9051 set out to fix: swapping
`providerFactory` changes `provider`, which is one of those dependencies.

Changing `id` was worse: the new document was created and the `Provider` swapped,
but nothing was ever bound to it, so edits reached neither document.

The two effects are now one. Provider, document and `Binding` are built together
and set in a single commit, so the components below always see a consistent set
-- staged across renders, `useProvider()` connects once for the new `Provider`
and again for the new `Binding`.

The guard survives, because `providerFactory` must not be called twice under
StrictMode, but the inputs it compares now match the dependency list exactly, so
there is no dependency that can tear the session down and then be skipped.
`excludedProperties` and `rootName` are in neither list: they are read once when
the `Binding` is built, exactly as before, so a caller passing a fresh object
each render keeps its connection rather than churning it.

No public API changes: `CollaborationPluginProps` is untouched, and
`YjsCollaborationCursors` is module-private. The `doc` state is now write-only
and kept solely for the legacy 'reload' callback (meta#1409); rebinding on reload is
a pre-existing gap this does not close.

## Test plan

### Before

The new test, against meta#9051 as submitted -- nothing is ever bound to the second
document, so `root` does not exist on it:

```
$ npx vitest run --project unit \
    packages/lexical-react/src/__tests__/unit/LexicalCollaborationPlugin.test.tsx
 × the binding follows the id onto the new document
AssertionError: expected undefined to be defined
 Tests  1 failed | 2 passed (3)
```

Against `main` (before meta#9051), the id change is ignored outright:

```
 × provider is replaced when providerFactory changes
 × the binding follows the id onto the new document
AssertionError: expected "vi.fn()" to be called 1 times, but got 0 times
 Tests  2 failed | 1 passed (3)
```

### After

```
$ npx vitest run --project unit \
    packages/lexical-react/src/__tests__/unit/LexicalCollaborationPlugin.test.tsx
 Test Files  1 passed (1)
      Tests  3 passed (3)

$ pnpm run test-unit
 Test Files  293 passed (293)
      Tests  5513 passed | 1 skipped (5514)

$ pnpm run test-browser
 Test Files  21 passed (21)
      Tests  142 passed | 1 skipped (143)

$ pnpm run ci-check
No errors!
```

`provider is replaced when providerFactory changes` still asserts exactly one
`connect()` on the replacement provider, which is what the single-commit update
preserves. The collab E2E suite needs a websocket server and was not run here.

Co-Authored-By: Claude Opus <[email protected]>

This branch had an error being deployed

1 failed and 1 active deployments
Preview – lexical-playground — e2cbcd42 Deployed Mar 2, 2022 by vercel[bot]
Preview – lexical — e2cbcd42 Deployed Mar 2, 2022 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants