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

doc: add snippet for AsyncResource and EE integration #33751

Closed

Conversation

puzpuzpuz
Copy link
Member

Continuation of #33736

Adds a snippet for AsyncResource + EventEmitter integration, based on asyncResource.runInAsyncScope.bind.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • documentation is changed or added
  • commit message follows commit guidelines

@puzpuzpuz puzpuzpuz added the async_hooks Issues and PRs related to the async hooks subsystem. label Jun 5, 2020
@nodejs-github-bot nodejs-github-bot added the doc Issues and PRs related to the documentations. label Jun 5, 2020
@puzpuzpuz puzpuzpuz force-pushed the doc/add-async-resource-bind-sample branch from b894b3e to 9fa4728 Compare June 5, 2020 14:02
@puzpuzpuz puzpuzpuz marked this pull request as ready for review June 5, 2020 14:02
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.

It may be worthwhile building this in the the EventEmitter API (and possibly the EventTarget API). We've talked before about adding a context type of option to events, which could be expressed as an additional argument to addListener/on/once ... e.g.

const asyncResource = new AsyncResource('request');

req.on('close', () => {}, asyncResource)

Under the covers it would effectively just do exactly what this example does, but in a way that is a bit more ergonomic for the user. For instance, in order to subsequently remove the event listener, the user has to track the bound function themselves (e.g. const fn = asyncResource.runInAsyncScope.bind(asyncResource, () => {}); ee.on('foo', fn'); ee.off('foo', fn); which is a bit cumbersome when we could handle it internally in a way similar to how we approach once handlers.

For now, however, this example is very valuable.

doc/api/async_hooks.md Outdated Show resolved Hide resolved
doc/api/async_hooks.md Outdated Show resolved Hide resolved
@puzpuzpuz puzpuzpuz force-pushed the doc/add-async-resource-bind-sample branch from 9fa4728 to 0648fff Compare June 5, 2020 17:14
test0.js Outdated Show resolved Hide resolved
@puzpuzpuz puzpuzpuz force-pushed the doc/add-async-resource-bind-sample branch from 0648fff to ed845b8 Compare June 5, 2020 17:23
@Qard
Copy link
Member

Qard commented Jun 6, 2020

For what it's worth, I still think having an actual method for this is friendlier, and it's simple enough that the maintenance burden is near zero.

bindToAsyncScope(fn, thisArg) {
  return this.runInAsyncScope.bind(this, fn, thisArg);
}

I'm +0 on this. If this is how we want to proceed, that's fine. 🤷

@puzpuzpuz
Copy link
Member Author

@Qard optimistically, this doc addition is a temp measure while EEs/streams used in the core are not integrated properly with async_hooks (see #33723). Once they're integrated (hopefully, this is going to happen), it's easy to remove this addition.

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

Copy link
Member

@Qard Qard left a comment

Choose a reason for hiding this comment

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

Yep, my perspective was just due to the other issue being closed before so it seemed like we decided against that route in favour of only documenting it. I would still like to work towards an actual fix in core. Since it seems the doc change is being considered more as a potentially temporary solution, I'll upgrade to 👍 .

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/31827/

puzpuzpuz added a commit that referenced this pull request Jun 11, 2020
PR-URL: #33751
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
@puzpuzpuz
Copy link
Member Author

Landed in db3d6b3

@puzpuzpuz puzpuzpuz closed this Jun 11, 2020
@puzpuzpuz puzpuzpuz deleted the doc/add-async-resource-bind-sample branch June 11, 2020 09:17
codebytere pushed a commit that referenced this pull request Jun 18, 2020
PR-URL: #33751
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
@codebytere codebytere mentioned this pull request Jun 28, 2020
codebytere pushed a commit that referenced this pull request Jun 30, 2020
PR-URL: #33751
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
codebytere pushed a commit that referenced this pull request Jul 10, 2020
PR-URL: #33751
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
@codebytere codebytere mentioned this pull request Jul 13, 2020
codebytere pushed a commit that referenced this pull request Jul 14, 2020
PR-URL: #33751
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
async_hooks Issues and PRs related to the async hooks subsystem. doc Issues and PRs related to the documentations.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants