-
-
Notifications
You must be signed in to change notification settings - Fork 9
โจ feat: reset random key to avoid ID conflicts during state parsing #103
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
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Reviewer's guide (collapsed on small PRs)Reviewer's GuideEnsures that when JSON state is parsed into the editor, the global random key generator is reset based on existing node IDs to prevent future ID collisions. Sequence diagram for JSON state parsing with random key resetsequenceDiagram
participant JSONDataSource
participant Editor
participant EditorState
participant ParseSerializedNodeImpl
participant RandomKeyGenerator
JSONDataSource->>Editor: loadJSONState(dataObj)
Editor->>EditorState: withJsonState(state)
EditorState->>JSONDataSource: callback(state)
JSONDataSource->>ParseSerializedNodeImpl: $parseSerializedNodeImpl(dataObj.root, editor, true, state)
ParseSerializedNodeImpl-->>JSONDataSource: rootNode
JSONDataSource->>EditorState: access _nodeMap
JSONDataSource->>EditorState: iterate _nodeMap keys (exclude root)
JSONDataSource->>JSONDataSource: compute maxId from numeric keys
JSONDataSource->>RandomKeyGenerator: resetRandomKey(maxId + 1)
JSONDataSource->>EditorState: _nodeMap.set(rootNode.getKey(), rootNode)
Updated class diagram for JSONDataSource and random key managementclassDiagram
class DataSource
class JSONDataSource
class Editor
class EditorState {
Map~string, EditorNode~ _nodeMap
}
class EditorNode {
string getKey()
}
class RandomKeyGenerator {
resetRandomKey(startId number)
}
JSONDataSource --|> DataSource
JSONDataSource ..> Editor
JSONDataSource ..> EditorState
JSONDataSource ..> RandomKeyGenerator
Editor o--> EditorState
EditorState o--> EditorNode
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
๐ @ilimei |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey - I've found 1 issue, and left some high level feedback:
- The iteration over
state._nodeMap.keys()assumeskeys()returns something with.forEach, which is not true for standardMapiterators; consider usingfor (const key of state._nodeMap.keys())orArray.from(state._nodeMap.keys()).forEach(...)to avoid runtime errors. - When computing
maxId,Number(key)can yieldNaNfor non-numeric keys (beyond'root'), which will breakMath.max; it would be safer to skip keys that don't parse to a finite number before taking the max.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The iteration over `state._nodeMap.keys()` assumes `keys()` returns something with `.forEach`, which is not true for standard `Map` iterators; consider using `for (const key of state._nodeMap.keys())` or `Array.from(state._nodeMap.keys()).forEach(...)` to avoid runtime errors.
- When computing `maxId`, `Number(key)` can yield `NaN` for non-numeric keys (beyond `'root'`), which will break `Math.max`; it would be safer to skip keys that don't parse to a finite number before taking the max.
## Individual Comments
### Comment 1
<location> `src/plugins/common/data-source/json-data-source.ts:78` </location>
<code_context>
try {
const root = $parseSerializedNodeImpl(dataObj.root, editor, true, state);
+ let maxId = -1;
+ state._nodeMap.keys().forEach((key) => {
+ if (key === 'root') return;
+ maxId = Math.max(maxId, Number(key));
</code_context>
<issue_to_address>
**issue (bug_risk):** Using `forEach` directly on `state._nodeMap.keys()` is likely incorrect because `Map.prototype.keys()` returns an iterator, not an array.
In JS/TS, `Map#keys()` returns an iterator, which doesnโt have `forEach`, so this will throw at runtime. Use a `for...of` loop over `state._nodeMap.keys()` or wrap with `Array.from(...)` if you specifically need `forEach` behavior.
</issue_to_address>Help me be more useful! Please click ๐ or ๐ on each comment and I'll use the feedback to improve your reviews.
commit: |
|
โค๏ธ Great PR @ilimei โค๏ธ |
## [Version 3.9.0](v3.8.0...v3.9.0) <sup>Released on **2026-01-08**</sup> #### โจ Features - **misc**: Reset random key to avoid ID conflicts during state parsing. <br/> <details> <summary><kbd>Improvements and Fixes</kbd></summary> #### What's improved * **misc**: Reset random key to avoid ID conflicts during state parsing, closes [#103](#103) ([7119713](7119713)) </details> <div align="right"> [](#readme-top) </div>
|
๐ This PR is included in version 3.9.0 ๐ The release is available on: Your semantic-release bot ๐ฆ๐ |
๐ป ๅๆด็ฑปๅ | Change Type
๐ ๅๆด่ฏดๆ | Description of Change
๐ ่กฅๅ ไฟกๆฏ | Additional Information