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

lib,events: replace the attr listener of the wrapped listener with symbol. #36558

Closed
wants to merge 1 commit into from
Closed

lib,events: replace the attr listener of the wrapped listener with symbol. #36558

wants to merge 1 commit into from

Conversation

peng-huang-ch
Copy link

Fixes: #36522

Replace the attr listener of the wrapped listener with a symbol.

Related Issues

Fixes: #36522

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added the events Issues and PRs related to the events subsystem / EventEmitter. label Dec 18, 2020
Copy link
Contributor

@yashLadha yashLadha left a comment

Choose a reason for hiding this comment

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

PTAL

lib/events.js Outdated
Comment on lines 164 to 168
kEventListener: {
value: kEventListener,
enumerable: false,
configurable: false,
writable: false,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this property needs to be attached to the ``EventEmitter` object?

Copy link
Author

@peng-huang-ch peng-huang-ch Dec 18, 2020

Choose a reason for hiding this comment

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

Seem EventTarget needs to use the attributes, should keep the attr same, maybe could use common define, where do you think I can define it.

lib/events.js Outdated
Comment on lines 426 to 427
const l = listener[kEventListener] ? listener[kEventListener] : listener;
target.emit('newListener', type, l);
Copy link
Contributor

Choose a reason for hiding this comment

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

l is not a very verbose name and doesn't dictates the meaning.

Copy link
Author

@peng-huang-ch peng-huang-ch Dec 18, 2020

Choose a reason for hiding this comment

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

Fixed. remove the useless temporary variables.

lib/events.js Outdated
Comment on lines 704 to 706
let handleListener = handler ? handler[kEventListener] : undefined;
while (handleListener !== undefined) {
ArrayPrototypePush(listeners, handleListener);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the need for removing null check?

Copy link
Author

Choose a reason for hiding this comment

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

Fixed, still use the optional chaining

@@ -24,6 +24,7 @@
require('../common');
const assert = require('assert');
const events = require('events');
const { kEventListener } = events;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this required, can it be restructured?

Copy link
Author

Choose a reason for hiding this comment

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

Test the wrapped listener has this attr, and the value is the origin listener.

@lpinca lpinca added the semver-major PRs that contain breaking changes and should be released in the next major version. label Dec 18, 2020
@lpinca
Copy link
Member

lpinca commented Dec 18, 2020

I've added the semver-major label as the property is used by user land modules.

@PoojaDurgad PoojaDurgad added the request-ci Add this label to start a Jenkins CI on a PR. label Dec 18, 2020
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Dec 18, 2020
@nodejs-github-bot
Copy link
Collaborator

@Trott
Copy link
Member

Trott commented Dec 24, 2020

@nodejs/events

@benjamingr benjamingr added the needs-benchmark-ci PR that need a benchmark CI run. label Dec 24, 2020
Copy link
Member

@jasnell jasnell left a comment

Choose a reason for hiding this comment

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

We should not remove the existing listener property without a proper deprecation cycle. It could be made an alias of the new symbol. Also, the symbol should be declared directly in events.js rather than in internal/util requiring a new import here. It would also likely be a good idea to use Symbol.for() (SymbolFor from primordials) to define it.

@peng-huang-ch
Copy link
Author

@jasnell I have some questions:

  1. If using SymbolFor the userland still could change the attribute, we should not allow do it.
  2. The EventTarget also needs to use the attributes, so I defined the symbol in the internal/util. if declare directly in the event.js, then should export the attribute. same with above 1, will allow the userland to change the warped listener.

@aduh95
Copy link
Contributor

aduh95 commented Sep 15, 2021

  1. If using SymbolFor the userland still could change the attribute, we should not allow do it.

I think the goal is to let it be accessible to users, so they can mutate its value, but you decrease the risk of a clash with a user-specific utility (I mean, if someone is setting fn[Symbol.for('nodejs.listener')], it's almost certain they meant to deal with this API only). Another way of doing this would be indeed to export the symbol, in which case there's no need to use SymbolFor.
If you think we should instead remove this feature, it would need to go through a full deprecation cycle first.

@peng-huang-ch
Copy link
Author

@aduh95 thanks for your reply.
I still think we shouldn't actually affect user-specific utilities. if two listeners have some relationship, it would be better to define it on the utility level.
If you disagree with it, maybe we should add some description for the event once to let the caller knows the impact on the registered listener.

@aduh95
Copy link
Contributor

aduh95 commented Sep 28, 2021

I don't disagree on the philosophical level, the issue is it's being used by user land modules, so removing it wouldn't be acceptable without going through a deprecation cycle first. Improving the documentation is of course always welcome.

@peng-huang-ch
Copy link
Author

Sure, I agree. but it may require the module developers to decide whether the modification is necessary and how to deal with it.

@peng-huang-ch peng-huang-ch closed this by deleting the head repository Feb 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
events Issues and PRs related to the events subsystem / EventEmitter. needs-benchmark-ci PR that need a benchmark CI run. semver-major PRs that contain breaking changes and should be released in the next major version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Remove the incorrect event listener.
9 participants