Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(store): improve selector decorator types (#1929) #2042

Merged
merged 7 commits into from
Apr 6, 2024

Conversation

ConnorUllmann
Copy link
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

The @Selector decorator does not enforce that the arguments of the decorated function match the return types of the selectors that it depends on.

Issue Number: #1929

What is the new behavior?

The @Selector decorator enforces that the arguments of the functions that it decorates match the return values of the selectors that it was passed.

Limitations:

  • The models of state classes are not inferred. Any type is allowed for these arguments (however StateToken works correctly).
  • The arguments list is checked against both the possibility of an injected state and the possibility of no injected state. This is regardless of the actual setting, and regardless of whether the decorator is used in a State class or in an independent class.

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Although this is not a breaking change in the sense that a codebase with correct typings on their selector functions will require no changes, the purpose of this change is to expose failures to do that typing correctly and prevent them in the future. As a result, I would expect any reasonably-sized codebase to require fixes to their selector implementations as a result of this change.

@nx-cloud
Copy link

nx-cloud bot commented Aug 12, 2023

☁️ Nx Cloud Report

CI is running/has finished running commands for commit d6be8b6. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch


✅ Successfully ran 3 targets

Sent with 💌 from NxCloud.

@ConnorUllmann ConnorUllmann force-pushed the typed-selector-decorator branch 2 times, most recently from e468148 to 71e1ad5 Compare August 13, 2023 06:46
@ConnorUllmann ConnorUllmann marked this pull request as ready for review August 13, 2023 07:25
Improves @selector decorator so that the return types of the
selectors passed to the decorator must match the types of the
arguments of the decorated function.

Closes ngxs#1929
@bundlemon
Copy link

bundlemon bot commented Aug 14, 2023

BundleMon

Unchanged files (3)
Status Path Size Limits
fesm2015/ngxs-store.js
94.13KB 125KB / +0.5%
fesm2015/ngxs-store-operators.js
6.23KB 15KB / +0.5%
fesm2015/ngxs-store-internals.js
3.57KB 20KB / +0.5%

Total files change -2B 0%

Groups updated (1)
Status Path Size Limits
@ngxs/store(esm2015)[gzip]
./esm2015/**/*.js
180.3KB (+435B +0.24%) +1%
Unchanged groups (2)
Status Path Size Limits
@ngxs/store(umd)[gzip]
./bundles/*.umd.js
36.44KB +1%
@ngxs/store(fesm2015)[gzip]
./fesm2015/*.js
24.94KB +1%

Final result: ✅

View report in BundleMon website ➡️


Current branch size history | Target branch size history

@bundlemon
Copy link

bundlemon bot commented Aug 14, 2023

BundleMon (NGXS Plugins)

Unchanged files (16)
Status Path Size Limits
Plugins(umd)[gzip]
storage-plugin/bundles/ngxs-storage-plugin.um
d.js
7.97KB +1%
Plugins(umd)[gzip]
router-plugin/bundles/ngxs-router-plugin.umd.
js
7.19KB +1%
Plugins(umd)[gzip]
hmr-plugin/bundles/ngxs-hmr-plugin.umd.js
6.89KB +1%
Plugins(umd)[gzip]
websocket-plugin/bundles/ngxs-websocket-plugi
n.umd.js
6.79KB +1%
Plugins(fesm2015)[gzip]
storage-plugin/fesm2015/ngxs-storage-plugin.j
s
3.7KB +1%
Plugins(umd)[gzip]
form-plugin/bundles/ngxs-form-plugin.umd.js
3.29KB +1%
Plugins(fesm2015)[gzip]
router-plugin/fesm2015/ngxs-router-plugin.js
2.92KB +1%
Plugins(umd)[gzip]
devtools-plugin/bundles/ngxs-devtools-plugin.
umd.js
2.75KB +1%
Plugins(fesm2015)[gzip]
hmr-plugin/fesm2015/ngxs-hmr-plugin.js
2.65KB +1%
Plugins(fesm2015)[gzip]
form-plugin/fesm2015/ngxs-form-plugin.js
2.57KB +1%
Plugins(umd)[gzip]
logger-plugin/bundles/ngxs-logger-plugin.umd.
js
2.53KB +1%
Plugins(fesm2015)[gzip]
websocket-plugin/fesm2015/ngxs-websocket-plug
in.js
2.52KB +1%
Plugins(fesm2015)[gzip]
devtools-plugin/fesm2015/ngxs-devtools-plugin
.js
2.17KB +1%
Plugins(fesm2015)[gzip]
logger-plugin/fesm2015/ngxs-logger-plugin.js
2.01KB +1%
Plugins(umd)[gzip]
router-plugin/bundles/ngxs-router-plugin-inte
rnals.umd.js
654B +1%
Plugins(fesm2015)[gzip]
router-plugin/fesm2015/ngxs-router-plugin-int
ernals.js
406B +1%

No change in files bundle size

Unchanged groups (3)
Status Path Size Limits
All Plugins(esm2015)[gzip]
./-plugin/esm2015/**/.js
109.06KB +1%
All Plugins(umd)[gzip]
./-plugin/bundles/.umd.js
38.05KB +1%
All Plugins(fesm2015)[gzip]
./-plugin/fesm2015/.js
18.93KB +1%

Final result: ✅

View report in BundleMon website ➡️


Current branch size history | Target branch size history

@bundlemon
Copy link

bundlemon bot commented Aug 14, 2023

BundleMon (Integration Projects)

Unchanged files (2)
Status Path Size Limits
Main bundles(Gzip)
hello-world-ng17/dist-integration/main.(hash)
.js
68.5KB +1%
Main bundles(Gzip)
hello-world-ng16/dist-integration/main.(hash)
.js
67.03KB +1%

No change in files bundle size

Final result: ✅

View report in BundleMon website ➡️


Current branch size history | Target branch size history

Copy link

nx-cloud bot commented Apr 5, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit a4eaf0b. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 4 targets

Sent with 💌 from NxCloud.

Copy link
Member

@markwhitfeld markwhitfeld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ConnorUllmann Thank you so much for this contribution!
My apologies for the long delay in reviewing this.
We would like to include your PR in NGXS v17, which is landing very soon.

Would it be possible to add some comments / descriptions to the types that you created which explain how they work / what they do / etc. so that it is easier for us to be able to understand and maintain this code into the future?

One other quick comment is that after this PR is merged, we would like to build on top of your changes to add a deprecation notice to any occurrence of the @Selector(...) decorator where the selector function is expecting the container state as the first parameter (except for the parameterless version). I am just trying to figure out where exactly that doc comment would be 😄 .

@ngxs ngxs deleted a comment from codeclimate bot Apr 5, 2024
@ConnorUllmann
Copy link
Contributor Author

ConnorUllmann commented Apr 5, 2024

@markwhitfeld Glad to hear it! I'm writing up the descriptions of those types now.

I had to make some modifications in order to allow the parameterless call to not be deprecated when injecting container state while deprecating the version of the call with parameters. Do the examples in the image below accurately cover what you're expecting to see deprecated?

I can include the deprecation myself in my update, if you like, otherwise I'll note where it will need to be put.

image

@markwhitfeld
Copy link
Member

Yes, I agree with what you have there.
If you would like to add the deprecation notice, it would be the same as the one found here:

* Read the deprecation notice at this link: https://ngxs.io/deprecations/inject-container-state-deprecation.md.

The deprecation notice should point to the page that will go live when the release goes out:
https://ngxs.io/deprecations/inject-container-state-deprecation.md
You can see the development version of this page here.

Copy link
Member

@markwhitfeld markwhitfeld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic work.
Can I ask you to name the T and U type placeholders according to what they are?
This should be the last puzzle piece to make this more understandable and therefore maintainable.

Copy link

codeclimate bot commented Apr 6, 2024

Code Climate has analyzed commit a4eaf0b and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 100.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 95.3% (0.0% change).

View more on Code Climate.

Copy link
Member

@markwhitfeld markwhitfeld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much. This is really super work!

@markwhitfeld markwhitfeld merged commit f52c613 into ngxs:master Apr 6, 2024
12 checks passed
@ConnorUllmann ConnorUllmann deleted the typed-selector-decorator branch April 7, 2024 23:07
@markwhitfeld markwhitfeld added this to the v.18.0.0 milestone Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants