feat: let serialize `BaseGraph` with Codecs by tbouffard · Pull Request #778 · maxGraph/maxGraph
packages/core/src/serialization/register-other-codecs.ts (2)
19-19: Added import for BaseGraphCodec.
The new import for BaseGraphCodec is correctly added alongside other codec imports.
81-81: BaseGraphCodec registration added.
The BaseGraphCodec is now registered in the registerCoreCodecs function, enabling serialization support for BaseGraph instances. The placement before GraphCodec registration is appropriate since BaseGraph is the parent class of Graph.
packages/core/src/serialization/codecs/_other-codecs.ts (2)
18-18: Added export for BaseGraphCodec.
The export statement for the new BaseGraphCodec is properly added.
21-21: Changed export style for GraphCodec.
The export style for GraphCodec has been changed from a wildcard export to a named export. This is a good practice that explicitly states what's being exported and aligns with the need to export the excludedFields constant separately.
packages/core/src/serialization/codecs/GraphCodec.ts (2)
22-31: Extracted excluded fields into a reusable constant.
Good refactoring to extract the excluded fields list into a separate exported constant. This follows the DRY principle and allows the list to be reused in the new BaseGraphCodec, ensuring consistency in what fields are excluded from serialization.
53-53: Updated constructor to use the extracted constant.
The constructor now correctly uses the excludedFields constant instead of an inline array.
packages/core/src/serialization/codecs/BaseGraphCodec.ts (3)
1-20: New BaseGraphCodec implementation with proper imports and documentation.
The new file includes appropriate copyright header, imports, and JSDoc documentation for the codec. Importing the excludedFields from GraphCodec demonstrates good code reuse.
21-36: Well-documented codec class.
The JSDoc documentation correctly lists all the transient fields that will be excluded from serialization, which helps developers understand what parts of BaseGraph instances won't be included when serialized.
37-42: BaseGraphCodec implementation.
The BaseGraphCodec class correctly extends ObjectCodec and is implemented similarly to the GraphCodec class. The constructor properly initializes with a new BaseGraph instance and reuses the excludedFields constant from GraphCodec.
packages/core/__tests__/serialization/codecs/all-graph-classes.test.ts (6)
17-17: LGTM: Updated imports to support parameterized testing.
The imports have been correctly updated to include describe from Jest globals, which is necessary for the parameterized test structure used later in the file.
23-24: LGTM: Added required imports for BaseGraph testing.
Properly imported BaseGraph and getDefaultPlugins to support the new codec functionality being tested.
52-82: Well-designed XML template function.
Good refactoring to extract the XML template into a reusable function that dynamically substitutes the graph type name. This eliminates code duplication and makes the tests more maintainable.
84-90: LGTM: Clear test parameterization for both graph types.
The parameterized test structure is well-designed, covering both Graph (using the existing factory function) and BaseGraph (with default plugins) implementations.
91-98: LGTM: Export test verifies correct serialization.
The export test properly verifies that both graph types serialize correctly, with appropriate property overrides to ensure the serialization process captures changes from defaults.
100-113: LGTM: Import test verifies correct deserialization.
The import test effectively verifies the deserialization process by:
- Checking default property values before import
- Performing the import operation
- Verifying that properties were correctly overridden with values from the XML
This provides good test coverage for the codec functionality.