[FIX] Fix circular reference error when serializing Observer data#55
Merged
willeastcott merged 6 commits intomainfrom Jan 6, 2026
Merged
[FIX] Fix circular reference error when serializing Observer data#55willeastcott merged 6 commits intomainfrom
willeastcott merged 6 commits intomainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a circular reference error that occurred when serializing Observer data to JSON during ShareDB synchronization. The solution makes all internal properties non-enumerable across the Events, Observer, and ObserverList classes, preventing them from being included in JSON.stringify() operations.
Key changes:
- Converted internal property initialization from inline declarations to constructor-based
Object.definePropertycalls - Changed
_pathsWithDuplicatesfrom an object-based lookup to a Set for more idiomatic duplicate checking - Added comprehensive unit tests verifying non-enumerable properties and JSON serialization without circular references
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 10 comments.
| File | Description |
|---|---|
| test/observer.test.mjs | Adds three new test suites verifying that internal properties are non-enumerable and that objects with circular references can be safely serialized to JSON |
| src/observer.ts | Refactors property initialization to use Object.defineProperty for all internal properties, converts _pathsWithDuplicates from object to Set, and removes property initializers from declarations |
| src/observer-list.ts | Makes _indexed property non-enumerable using Object.defineProperty in the constructor |
| src/events.ts | Refactors all three internal properties (_events, _suspendEvents, _additionalEmitters) to be non-enumerable using a consistent initialization pattern |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
kpal81xd
approved these changes
Jan 6, 2026
Contributor
kpal81xd
left a comment
There was a problem hiding this comment.
Approving with a couple comments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes playcanvas/editor#1649
Problem
When editing JSON array attributes in the Editor, ShareDB would fail with:
This occurred because internal properties on
Events,Observer, andObserverListclasses were enumerable by default. WhenJSON.stringifywas called during ShareDB sync, these properties (which contain circular references like_parent,_additionalEmitters) were included in serialization.Solution
Made all internal properties non-enumerable using
Object.defineProperty:_events,_suspendEvents,_additionalEmitters_destroyed,_path,_keys,_data,_pathsWithDuplicates,_parent,_parentPath,_parentField,_parentKey,_latestFn,_silent_indexedNon-enumerable properties are excluded from
Object.keys(),for...inloops, andJSON.stringify().Additional Improvements
!) for strict TypeScript compatibility_parentand_latestFnto include| nullevents.test.mjs,observer.test.mjs,observer-list.test.mjstest/directory to lint scriptTesting
Added unit tests verifying:
Object.keys()JSON.stringify()pathsWithDuplicatesoption works correctly with Set implementation