robertbira

Italian Translation Report: Node.js [Part 18 - 1074 words]

Aug 1st, 2018
79
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. The module provides an API to register callbacks tracking the lifetime of asynchronous resources created inside a Node.js application.
  2. It can be accessed using:
  3. Terminology
  4. An asynchronous resource represents an object with an associated callback.
  5. This callback may be called multiple times, for example, the event in, or just a single time like in
  6. A resource can also be closed before the callback is called.
  7. does not explicitly distinguish between these different cases but will represent them as the abstract concept that is a resource.
  8. Public API
  9. Overview
  10. Following is a simple overview of the public API.
  11. Return the ID of the current execution context.
  12. Return the ID of the handle responsible for triggering the callback of the current execution scope to call.
  13. Create a new instance.
  14. All of these callbacks are optional.
  15. Allow callbacks of this instance to call.
  16. This is not an implicit action after running the constructor, and must be explicitly run to begin executing callbacks.
  17. Disable listening for new asynchronous events.
  18. The following are the callbacks that can be passed to
  19. init is called during object construction.
  20. The resource may not have completed construction when this callback runs, therefore all fields of the resource referenced by may not have been populated.
  21. before is called just before the resource's callback is called.
  22. It can be called 0-N times for handles (e.g. TCPWrap), and will be called exactly 1 time for requests (e.g. FSReqWrap).
  23. after is called just after the resource's callback has finished.
  24. destroy is called when an instance is destroyed.
  25. promiseResolve is called only for promise resources, when the function passed to the constructor is invoked (either directly or through other means of resolving a promise).
  26. The to register
  27. Returns: Instance used for disabling and enabling hooks
  28. Registers functions to be called for different lifetime events of each async operation.
  29. The callbacks are called for the respective asynchronous event during a resource's lifetime.
  30. For example, if only resource cleanup needs to be tracked, then only the callback needs to be passed.
  31. The specifics of all functions that can be passed to is in the section.
  32. Note that the callbacks will be inherited via the prototype chain:
  33. Error Handling
  34. If any callbacks throw, the application will print the stack trace and exit.
  35. The exit path does follow that of an uncaught exception, but all listeners are removed, thus forcing the process to exit.
  36. The callbacks will still be called unless the application is run with, in which case a stack trace will be printed and the application exits, leaving a core file.
  37. The reason for this error handling behavior is that these callbacks are running at potentially volatile points in an object's lifetime, for example during class construction and destruction. Because of this, it is deemed necessary to bring down the process quickly in order to prevent an unintentional abort in the future. This is subject to change in the future if a comprehensive analysis is performed to ensure an exception can follow the normal control flow without unintentional side effects.
  38. Printing in callbacks
  39. Because printing to the console is an asynchronous operation, will cause the callbacks to be called.
  40. Using or similar asynchronous operations inside an callback function will thus cause an infinite recursion.
  41. An easy solution to this when debugging is to use a synchronous logging operation such as
  42. This will print to because is the file descriptor for stdout and will not invoke recursively because it is synchronous.
  43. use a function like this one when debugging inside an AsyncHooks callback
  44. If an asynchronous operation is needed for logging, it is possible to keep track of what caused the asynchronous operation using the information provided by itself. The logging should then be skipped when it was the logging itself that caused callback to call. By doing this the otherwise infinite recursion is broken.
  45. Returns: A reference to
  46. Enable the callbacks for a given instance. If no callbacks are provided enabling is a noop.
  47. The instance is disabled by default. If the instance should be enabled immediately after creation, the following pattern can be used.
  48. Disable the callbacks for a given instance from the global pool of callbacks to be executed. Once a hook has been disabled it will not be called again until enabled.
  49. For API consistency also returns the instance.
  50. Key events in the lifetime of asynchronous events have been categorized into
  51. four areas: instantiation, before/after the callback is called, and when the
  52. instance is destroyed.
  53. A unique ID for the async resource.
  54. The type of the async resource.
  55. The unique ID of the async resource in whose
  56. execution context this async resource was created.
  57. Reference to the resource representing the async operation, needs to be released during
  58. Called when a class is constructed that has the possibility to emit an asynchronous event. This does not mean the instance must call before is called, only that the possibility exists.
  59. This behavior can be observed by doing something like opening a resource then closing it before the resource can be used. The following snippet demonstrates this.
  60. OR
  61. Every new resource is assigned an ID that is unique within the scope of the current process.
  62. The is a string identifying the type of resource that caused to be called. Generally, it will correspond to the name of the resource's constructor.
  63. There is also the resource type, which is used to track instances and asynchronous work scheduled by them.
  64. Users are able to define their own when using the public embedder API.
  65. It is possible to have type name collisions. Embedders are encouraged to use unique prefixes, such as the npm package name, to prevent collisions when listening to the hooks.
  66. is the of the resource that caused (or "triggered") the new resource to initialize and that caused to call.
  67. This is different from that only shows when a resource was created, while shows why a resource was created.
  68. The following is a simple demonstration of
  69. Output when hitting the server with
  70. The is the new connection from the client.
  71. When a new connection is made, the instance is immediately constructed.
  72. This happens outside of any JavaScript stack.
  73. (An means that it is being executed from C++ with no JavaScript stack above it.) With only that information, it would be impossible to link resources together in terms of what caused them to be created, so is given the task of propagating what resource is responsible for the new resource's existence.
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×