Use "Symbol" for internal entity properties#135
Merged
Conversation
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.
GitHub
Changes
__typeand__primaryKey. Instead, it uses symbols:Symbol('type')Symbol('primaryKey')__nodeIdis removed from internal entity properties. It was a left-over from the previous implementation of relationships and is no longer used.InternalEntitytype. The entity is now the same internally and publicly, as the Symbols representing internal properties are non-enumerable.BroadcastChannel, so we need to preserve the transmitted entity’s internal properties in a manually attached regular property. The event listener then re-attaches the symbols to the received entity using theinheritInternalPropertiesfunction..toEqualassertion. The end-developer never sees nor operates with the internal symbols as they are non-enumerable.Databaseclass to restrictmodelNamevalues to the keys of the given dictionary.Motivation
This change originates from #132, where a recursive relationship resulted in an infinite loop. Upon investigation, the loop originated from the
removeInternalPropertiesfunction that stripped a given entity of its internal properties (those were stored as regular properties under reserved keys). That function eventually stumbled upon a relational property, read it, tried to clean it, then found the nested relation, then nested, etc (the relation is expected to be circular but the library must not hang on it).Diving into the matter further, I’ve tried storing the internal properties in a different way. My first try was to use
Object.defineProperties, which appends properties that are non-enumerable by default. This worked nicely until I realized that the properties appended that way are lost when you assign the object (entity) into another data structure:This corrupted each entity once it was stored in the
Database. UsingObject.definePropertieswas not an option for this use case.Afterward, I’ve recalled symbols and thought to give them a try. It turned out symbols are great for this particular case, because they are non-enumerable, but also persist with the object regardless of where it’s being stored. It was decided to use symbols for storing internal entity properties.
Consequences
BroadcastChannelin thesyncextension (database operations synchronization between open tabs), it loses its symbols, corrupting the entity. I’d have to add a manual plain property calledSERIALIZED_INTERNAL_PROPERTIESthat temporarily stores respective properties, and then re-define them on the entity in another client.