feat: entity companion definition static thunk #210
Merged
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.
Why
We're seeing an increasing number of circular reference reports caused by the requirement to maintain the companion definition singularity/singleton property in the application, usually by defining it in a module-top-level property. This has historically caused issues due to some properties in the companion/configuration using top-level definitions of entity types or other classes.
In the past, we fixed this piece-by-piece, making the companion definition or configuration fields that could cause circular references functions so that circular references would be less likely. #89 #93
This PR expands upon this but should fix it once-and-for-all by maintaining the singleton requirement in the framework itself rather than pushing the responsibility to the application.
How
This PR does a few things sequentially (yet procedurally) to accomplish the goal:
getCompanionDefinition
->defineCompanionDefinition
- this signals to the application developer that this is a definition and that the framework will handle the singleton.EntityCompanionProvider
. This will serve as the instance of the entity companion for the entity and is responsible for making this a singleton. Note that this doesn't need to be a singleton since these are objects, but since this method is called so frequently this memoization is a nice speedup.EntityCompanionProvider
, for example:viewerContext.entityCompanionProvider.getCompanionForEntity(EntityClass).entityCompanionDefinition.entityConfiguration.tableName
EntityCompanionDefinition
andEntityConfiguration
to non-thunks due to the top-level being a thunk itself and accomplishing the lazy evaluation.EntityLoader.constructEntity
to make it type-safe and prevent needing to access companion definition in the entity instances themselves (which would be infinite loop). This requires changing the constructor signature forEntity
itself but that shouldn't be an issue since it is so rarely used outside of the framework itself.Test Plan
Run all tests.
Also
yarn link
this into a large codebase (Expo www) and ensure that this fixes circular issues and doesn't break anything.