Repository navigation
Conversation
…reated
## Description
`syncNodeStateFromLexical` writes a node's state into the shared doc in two
loops — one for unknown keys (state whose `StateConfig` this build has not
registered) and one for known keys. They disagree about how to treat "there is
no previous state":
```js
const [prevUnknown, prevKnown] =
prevState && stateMap.doc
? prevState.getInternalState()
: [undefined, new Map()];
if (unknown) {
for (const [k, v] of Object.entries(unknown)) {
if (prevUnknown && v !== prevUnknown[k]) {
stateMap.set(k, v);
}
}
}
for (const [stateConfig, v] of known) {
if (prevKnown.get(stateConfig) !== v) {
stateMap.set(stateConfig.key, stateConfig.unparse(v));
}
}
```
The known loop deliberately falls back to an empty `Map`, so on the create path
every entry compares unequal and is written. The unknown loop falls back to
`undefined` and then short-circuits on it, so **nothing** is written.
`NodeState.getInternalState()` returns `[UnknownStateRecord | undefined, ...]`,
so `prevUnknown` is also `undefined` whenever the previous state carried only
known keys — not just on creation. Either way the guard means "nothing was
synced yet", which is exactly when the entries *should* be written.
The create path is the common one: `$createCollabNodeFromLexicalNode` calls
`syncPropertiesFromLexical(..., null, node)`, so `prevLexicalNode` is `null`.
The unknown keys of a newly created node therefore never reach the shared
document, and because later updates only write *changed* keys, a value dropped
at creation stays dropped forever. Peers never receive it.
This matters precisely for the case unknown state exists to serve — as
`NodeState.unknownState` documents, it is what stops "an old version of your
code" from erasing metadata written by a newer version. Under collab that
metadata is dropped instead of preserved.
Making the unknown loop use the same "no previous state means everything is
new" rule as the known loop fixes it.
## Test plan
New unit test `packages/lexical-yjs/src/__tests__/unit/NodeStateSyncUnknown.test.ts`.
It serializes a paragraph carrying unknown state through a real binding and
asserts the `__state` Y.Map contains it. A third case sets *known* state the
same way as a control — that one passes before and after, which is what pins
the asymmetry.
### Before
```
$ npx vitest run packages/lexical-yjs/src/__tests__/unit/NodeStateSyncUnknown.test.ts
× unknown state on a newly created node is written to the shared doc 10ms
× several unknown keys all reach the shared doc 1ms
AssertionError: expected undefined to be 42 // Object.is equality
AssertionError: expected undefined to be 1 // Object.is equality
Tests 2 failed | 1 passed (3)
```
### After
```
$ npx vitest run packages/lexical-yjs
Test Files 6 passed (6)
Tests 81 passed (81)
$ npx vitest run packages/lexical-react
Test Files 31 passed (31)
Tests 182 passed (182)
```
|
@LeSingh1 is attempting to deploy a commit to the Meta Open Source Team on Vercel. A member of the Team first needs to authorize it. |
|
Follow-up note while probing this area: I instrumented the real sync path to check whether this fix is complete for the create path, and it is. On One gap this PR does not cover, noted for a separate change: |
|
It would be better to consolidate the fix into this PR, a high velocity of lots of small PRs is not a considerate contribution strategy. Reviewing a 20 line PR does not take twice as long as reviewing a 10 line PR. |
Description
syncNodeStateFromLexicalwrites a node's state into the shared doc in twoloops — one for unknown keys (state whose
StateConfigthis build has notregistered) and one for known keys. They disagree about how to treat "there is
no previous state":
The known loop deliberately falls back to an empty
Map, so on the create pathevery entry compares unequal and is written. The unknown loop falls back to
undefinedand then short-circuits on it, so nothing is written.NodeState.getInternalState()returns[UnknownStateRecord | undefined, ...],so
prevUnknownis alsoundefinedwhenever the previous state carried onlyknown keys — not just on creation. Either way the guard means "nothing was
synced yet", which is exactly when the entries should be written.
The create path is the common one:
$createCollabNodeFromLexicalNodecallssyncPropertiesFromLexical(..., null, node), soprevLexicalNodeisnull.The unknown keys of a newly created node therefore never reach the shared
document, and because later updates only write changed keys, a value dropped
at creation stays dropped forever. Peers never receive it.
This matters precisely for the case unknown state exists to serve — as
NodeState.unknownStatedocuments, it is what stops "an old version of yourcode" from erasing metadata written by a newer version. Under collab that
metadata is dropped instead of preserved.
Making the unknown loop use the same "no previous state means everything is
new" rule as the known loop fixes it.
Test plan
New unit test
packages/lexical-yjs/src/__tests__/unit/NodeStateSyncUnknown.test.ts.It serializes a paragraph carrying unknown state through a real binding and
asserts the
__stateY.Map contains it. A third case sets known state thesame way as a control — that one passes before and after, which is what pins
the asymmetry.
Before
After