-
Notifications
You must be signed in to change notification settings - Fork 61
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
SignalManager: improve type safety and reduce boilerplate #1133
base: master
Are you sure you want to change the base?
SignalManager: improve type safety and reduce boilerplate #1133
Conversation
e381965
to
a2e3573
Compare
a2e3573
to
bfa8d0c
Compare
2720135
to
5b2699a
Compare
5b2699a
to
3441fdc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the proposal. It looks like a simplification. I had a first glance over the changes and have a general comment using Strings. Please have look.
* | ||
* @example | ||
* // Single argument event | ||
* signalManager().on('THEME_CHANGED', (theme: string) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that a String has to be passed manually instead of a String constant as before (Signals.THEME_CHANGED) is more error-prone, because it's not caught at build time. Think we should have something similar as before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current setup is actually type-safe! The SignalType = keyof Signals
type means TypeScript will only accept exact string matches of the events defined in our Signals
interface. You can't pass an arbitrary string - TypeScript will catch any typos or invalid events at compile time:
signalManager.on('THEME_CHANGED', ...) // ✅ Works fine
signalManager.on('INVALID_EVENT', ...) // ❌ Compile error
signalManager.on('theme_changed', ...) // ❌ Compile error
Plus, we get the bonus of TypeScript automatically inferring the correct parameter types for our listeners. So we're getting all the safety of constants with better ergonomics.
Here's a screengrab of how this looks in vscode:
Screencast.from.10-25-2024.09.55.57.AM.webm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the answers. I'll review it.
FYI @ngondalia. Please let us know if you have any concerns. I'll update the vscode-trace-extension accordingly once we have decided to merge this. |
@williamsyang-work I want to understand the impact to the vscode-trace-extension. I'm currently struggling to make it compile for the external-api (https://github.com/eclipse-cdt-cloud/vscode-trace-extension/blob/e1bfcee1493621b060889a0e79c993c0748931d5/vscode-trace-extension/src/external-api/external-api.ts#L78). I get the following error
Any suggestions? |
I think we could change the API offSignalManagerSignal(event: string | symbol, listener: (...args: unknown[]) => void): void {
signalManager().off(event, listener);
}, to the following offSignalManagerSignal(event: keyof Signals, listener: (...args: unknown[]) => void): void {
signalManager().off(event, listener);
}, This will break downstream projects. @ngondalia please let us know if that is ok with you. |
@bhufmann I haven't looked into the changes needed for vscode-trace-extension. I wanted to get some positive feedback on this change before investigating what is needed for that repo. If you think this is a good idea I can start investigating the changes required for this in vscode. I think that the change you suggested makes sense. |
This will require a bit of refactoring on our end, but i think it is a good improvement. I am ok with this. :) |
Here is a draft PR I made with changes to the |
3441fdc
to
aca8607
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good to me. I ran it in Theia and with the vscode extension update (eclipse-cdt-cloud/vscode-trace-extension#277) and it works.
BTW, the license error is taken care of and we can still proceed with merging this PR:
https://gitlab.eclipse.org/eclipsefdn/emo-team/iplab/-/issues/16973
@ngondalia would you be able to do another review for this one? If not, please let me know and then I'll ask another committer. |
Yeah, i can review it - Give me until the end of the day (I would like to try it out with both updates) |
;
There are some merge conflicts - otherwise LGTM |
aca8607
to
e7de326
Compare
@ngondalia |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@williamsyang-work there are linter errors. Please run yarn format:write
to fix them and then re-submit the changes.
Also it needs a rebase after recent merges.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I too was worried about not having an enum or constants for the signal names, but the auto-complete does give the list of possible signals.
signalManager().on(Signals.EXPERIMENT_SELECTED, this._onExperimentSelected); | ||
signalManager().on(Signals.EXPERIMENT_CLOSED, this._onExperimentClosed); | ||
signalManager().on("EXPERIMENT_SELECTED", this._onExperimentSelected); | ||
signalManager().on("EXPERIMENT_CLOSED", this._onExperimentClosed); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Single quotes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Sorry for the delay. @bhufmann
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem. I'll re-review.
e7de326
to
6d29d50
Compare
- Replace individual fire* methods with strongly-typed emit interface - Consolidate signal payload types into single Signals interface - Add type-safe event handler registration with overloaded on/off methods - Remove redundant method declarations in favor of TypeScript inference - Maintain full backward compatibility with existing event system The new implementation provides compile-time type checking for event payloads while significantly reducing code volume and maintenance burden. Signed-off-by: Will Yang <[email protected]>
6d29d50
to
1e8c9fc
Compare
Dismissing review. I verified that Patrick's comments are addressed. Patrick is currently not available for another review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks good to me. Thanks for the contribution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this approach. I am approving but make sure to merge only when ready.
Improve SignalManager Type Safety and Developer Experience
Overview
This PR refactors the SignalManager to provide better TypeScript support and improved developer experience. The changes focus on making the event system more type-safe while reducing boilerplate code.
Key Improvements
💡 Better Type Inference
🛡️ Compile-Time Type Safety
📝 Better IDE Support
🧹 Reduced Boilerplate
Implementation Details
Signals
interface