Implement the data model for web renderers v0.9#606
Implement the data model for web renderers v0.9#606jacobsimionato wants to merge 11 commits intogoogle:mainfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a DataModel class for managing client-side state, complete with a comprehensive test suite. The implementation is robust, providing a reactive data store with path-based access and subscriptions. My review focuses on improving type safety, code clarity, and consistency. I've identified a significant type-safety issue in the Subscription interface that could lead to runtime errors, and I've provided a suggestion to fix it. I also found a minor redundancy in the notification logic and an inconsistency in handling undefined values that should be addressed or at least documented. Lastly, please note that the repository's contribution guidelines require the PR description to include the Pre-Review Checklist, which is currently missing.
|
I have addressed the feedback. Please take another look. |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces the v0.9 data model for web renderers, including the DataModel class implementation and a comprehensive test suite. The implementation is well-structured and the tests cover many cases.
I've found a critical issue in the set method's path traversal logic that could lead to incorrect behavior when a path contains a primitive value. I've provided a fix for this. I've also suggested an additional test case to cover this scenario and ensure the model behaves as expected.
The rest of the changes, including package updates and documentation, look good and are consistent with the new data model implementation.
| model.set('/foo', 'bar'); | ||
| assert.strictEqual(count, 0); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
This is a great set of tests! To make it even more robust, I recommend adding a test case to verify the behavior of set when a path segment contains a primitive value. This will ensure the fix for the issue I mentioned in data-model.ts is covered and prevent regressions.
Here's a suggested test case:
| }); | |
| it('overwrites primitive values in path when setting nested properties', () => { | |
| model.set('/user/name', 'not an object'); | |
| assert.strictEqual(model.get('/user/name'), 'not an object'); | |
| // Now, try to set a nested property. This should overwrite the primitive. | |
| model.set('/user/name/first', 'Alice'); | |
| assert.deepStrictEqual(model.get('/user/name'), { first: 'Alice' }); | |
| assert.strictEqual(model.get('/user/name/first'), 'Alice'); | |
| }); | |
| }); |
References
- The style guide requires that code changes should have tests. While tests are present, this comment suggests adding a test for a critical edge case that was missed, improving test coverage and robustness. (link)
There was a problem hiding this comment.
Please add a test case for this, but verify failure as explained above.
|
I have addressed the feedback. Please take another look. |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces the DataModel class for v0.9 of the web renderer, which provides a reactive data store with a subscription mechanism. The implementation is well-tested and includes comprehensive notification logic for subscribers. I've provided a couple of suggestions for improvement: one to prevent corrupting array data structures and another for enhancing performance in scenarios with many subscriptions. Overall, this is a solid implementation of a core feature.
|
I have addressed the feedback. Please take another look. |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces the DataModel for v0.9 of the web renderers, which is a core component for managing client-side state. The implementation is well-structured and comes with a comprehensive test suite, which is great to see. The related documentation and dependency updates are also included.
My review has identified a couple of areas for improvement. The most significant is that the path parsing logic in DataModel is not fully compliant with the JSON Pointer (RFC 6901) specification, which could lead to issues with certain property keys. I've also noted a potential performance optimization in the subscriber notification logic. Lastly, please remember to fill out the Pre-Review Checklist in the pull request description as per the repository's contribution guidelines.
| - **Scope instantiation:** For every item in the array, the client instantiates the template component. | ||
| - **Relative resolution:** Inside these instantiated components, any path that **does not** start with a forward slash `/` is treated as a **Relative Path**. | ||
| - A relative path `firstName` inside a template iterating over `/users` resolves to `/users/0/firstName` for the first item, `/users/1/firstName` for the second, etc. | ||
| - It is an error to use a non-numeric index on a path segment that refers to an array. |
There was a problem hiding this comment.
This is a good addition.
ditman
left a comment
There was a problem hiding this comment.
Small question about the creation of Subscription objects!
ditman
left a comment
There was a problem hiding this comment.
Some questions about the new DataModel object, I think the only really critical thing is the regexp to check if the last segment is digits only being repeated 3x, it should be a helper function.
Also, some questions about the internal data storage type.
| * - For objects: Setting a property to `undefined` removes the key from the object. | ||
| * - For arrays: Setting an index to `undefined` sets that index to `undefined` but preserves the array length (sparse array). | ||
| */ | ||
| set(path: string, value: any): void { |
There was a problem hiding this comment.
I think it'd be nice if this object was more like an "Observable Map", IMO this should return this, see Map.prototype.set(), for example: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set#return_value
There was a problem hiding this comment.
I like this suggestion, let's do this.
| export class DataModel { | ||
| private data: any = {}; |
There was a problem hiding this comment.
There's a lot of any in this class. can we say data must be a Record or Map or similar, instead of any?
There was a problem hiding this comment.
I think Record<string, unknown> seems to be the best fit, because the structure of the map is unknown.
| */ | ||
| export class DataModel { | ||
| private data: any = {}; | ||
| private readonly subscriptions: Map<string, Set<SubscriptionImpl<any>>> = new Map(); |
There was a problem hiding this comment.
| private readonly subscriptions: Map<string, Set<SubscriptionImpl<any>>> = new Map(); | |
| private readonly subscriptions: Map<string, Set<Subscription<any>>> = new Map(); |
We should only use the Impl to create new instances, but in general we should code against the interface, if we can!
There was a problem hiding this comment.
Here, we need a reference to the Impl because it exposes the 'setValue' method which data-model needs access to e.g. on line 213. But it only exposes these objects via the more narrow Subscription interface publicly.
|
|
||
| /** | ||
| * Updates the model at the specific path and notifies all relevant subscribers. | ||
| * If path is '/' or empty, replaces the entire root. |
There was a problem hiding this comment.
What happens if path is null or undefined? Does that become a runtime error?
There was a problem hiding this comment.
Path can't be null or undefined due to the magic of TS, right?
| current = current[segment]; | ||
| } | ||
|
|
||
| if (Array.isArray(current) && !/^\d+$/.test(lastSegment)) { |
There was a problem hiding this comment.
This regexp is repeated here and in line 107 and 95, please create a helper "isOnlyNumbers" function or similar!
There was a problem hiding this comment.
Sounds great
| * A standalone, observable data store representing the client-side state. | ||
| * It handles JSON Pointer path resolution and subscription management. | ||
| */ | ||
| export class DataModel { |
There was a problem hiding this comment.
Is there any way of retrieving the whole built data object?
There was a problem hiding this comment.
Yes! Call myDataModel.get('/').
| private parsePath(path: string): string[] { | ||
| return path.split('/').filter(p => p.length > 0); | ||
| } |
There was a problem hiding this comment.
Are there any restrictions on what the syntax for a path can be, other than "separated by /"?
There was a problem hiding this comment.
Hmmm I'm not sure. I don't think we were super clear in the spec. I think as time goes on, we'll likely be more specific about this in the spec and then can add corresponding validation here.
Refactor: Enhance DataModel robustness and type safety in web_core v0.9
This change refactors the DataModel in
@a2ui/web_core/v0_9to improve its robustness, type safety, and adherence to the A2UI protocol specification, particularly concerning data manipulation and path resolution.Key changes include:
Improved DataModel.set path handling:
setmethod to throw an error if an intermediate segment of a path points to a primitive value when attempting to set a nested property, preventing unexpected data corruption.setmethod to explicitly describe the behavior when setting values toundefinedfor both objects (key removal) and arrays (index nullification).Enhanced Type Safety:
Subscription<T>interface to correctly typevalueandonChangecallback parameters asT | undefined, reflecting potentialundefinedstates in the data model and improving consumer type safety.Test Coverage & Fixes:
data-model.test.tsto validate the error conditions for primitive path traversal and non-numeric array segment access.web_coreandlitby ensuring@types/nodeis correctly installed and by updatingweb_core/package.jsonto explicitly exportv0_9modules.anytype errors in test callbacks.specification/v0_9/json/standard_catalog.json.Documentation Updates:
specification/v0_9/docs/a2ui_protocol.mdto reflect the refined data model behavior forundefinedvalues and array index rules.