feat(reactive-on): extend usages of the reactive on utility - #688
feat(reactive-on): extend usages of the reactive on utility#688JeanMeche wants to merge 1 commit into
on utility#688Conversation
|
View your CI Pipeline Execution ↗ for commit bd62c38
☁️ Nx Cloud last updated this comment at |
There was a problem hiding this comment.
Code Review
This pull request updates the on utility to support computed and afterRenderEffect by returning computation results and making the cleanup function optional. It also adds relevant tests. Feedback focuses on restoring JSDoc documentation and maintaining type safety by using unknown instead of any for internal variables.
| /** | ||
| * Makes dependencies of a computation explicit. | ||
| * | ||
| * // is equivalent to: | ||
| * effect(() => { | ||
| * const v = a(); | ||
| * untracked(() => console.log(v, b())); | ||
| * }); | ||
| * ``` | ||
| * Works with `effect`, `computed`, and `afterRenderEffect`. | ||
| */ |
There was a problem hiding this comment.
The previous JSDoc included a helpful example and an explanation of how on works (by using untracked internally). Removing these details makes the utility harder to understand for new users. It would be beneficial to keep the explanation and update the example to include the new supported use cases like computed.
/**
* Makes dependencies of a computation explicit.
*
* Works with `effect`, `computed`, and `afterRenderEffect`.
*
* @example
* ```typescript
* effect(on(a, (v) => console.log(v, b())));
*
* // is equivalent to:
* effect(() => {
* const v = a();
* untracked(() => console.log(v, b()));
* });
*
* // also works with computed:
* const doubled = computed(on(count, (val) => val * 2));
* ```
*/| let prevInput: any; | ||
| let prevValue: any; |
There was a problem hiding this comment.
Changing these variables from unknown to any reduces type safety within the implementation. Since fn is called with these values and its implementation signature accepts any, unknown is perfectly compatible and safer. It's recommended to stick with unknown as it was before.
let prevInput: unknown;
let prevValue: unknown;| return (onCleanup: EffectCleanupRegisterFn) => { | ||
| let input: unknown; | ||
| return (onCleanup?: EffectCleanupRegisterFn) => { | ||
| let input: any; |
73666c7 to
53f7062
Compare
nartc
left a comment
There was a problem hiding this comment.
gemini suggestion seems legit. JSDoc comment with examples is helpful.
bd62c38 to
ada6a4c
Compare
You can now use it with `computer` & `afterRenderEffect`
| const log: number[] = []; | ||
|
|
||
| effect( | ||
| on({ a, b }, ({ a: valA, b: valB }) => { |
There was a problem hiding this comment.
Thinking more about it, I don't think we should implicitly invoke the signals.
You can now use it with
computer&afterRenderEffect