forked from nodejscn/node-api-cn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
404 changed files
with
2,874 additions
and
1,240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<!-- YAML | ||
added: v12.16.0 | ||
added: v13.6.0 | ||
--> | ||
|
||
* `string` {string} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<!-- YAML | ||
added: v12.16.0 | ||
added: v13.6.0 | ||
--> | ||
|
||
* `string` {string} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
<!-- YAML | ||
added: v13.9.0 | ||
--> | ||
|
||
* Returns: {Object} The resource representing the current execution. | ||
Useful to store data within the resource. | ||
|
||
Resource objects returned by `executionAsyncResource()` are most often internal | ||
Node.js handle objects with undocumented APIs. Using any functions or properties | ||
on the object is likely to crash your application and should be avoided. | ||
|
||
Using `executionAsyncResource()` in the top-level execution context will | ||
return an empty object as there is no handle or request object to use, | ||
but having an object representing the top-level can be helpful. | ||
|
||
```js | ||
const { open } = require('fs'); | ||
const { executionAsyncId, executionAsyncResource } = require('async_hooks'); | ||
console.log(executionAsyncId(), executionAsyncResource()); // 1 {} | ||
open(__filename, 'r', (err, fd) => { | ||
console.log(executionAsyncId(), executionAsyncResource()); // 7 FSReqWrap | ||
}); | ||
``` | ||
|
||
This can be used to implement continuation local storage without the | ||
use of a tracking `Map` to store the metadata: | ||
|
||
```js | ||
const { createServer } = require('http'); | ||
const { | ||
executionAsyncId, | ||
executionAsyncResource, | ||
createHook | ||
} = require('async_hooks'); | ||
const sym = Symbol('state'); // Private symbol to avoid pollution | ||
createHook({ | ||
init(asyncId, type, triggerAsyncId, resource) { | ||
const cr = executionAsyncResource(); | ||
if (cr) { | ||
resource[sym] = cr[sym]; | ||
} | ||
} | ||
}).enable(); | ||
const server = createServer(function(req, res) { | ||
executionAsyncResource()[sym] = { state: req.url }; | ||
setTimeout(function() { | ||
res.end(JSON.stringify(executionAsyncResource()[sym])); | ||
}, 100); | ||
}).listen(3000); | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!-- YAML | ||
added: v13.10.0 | ||
--> | ||
|
||
This method disables the instance of `AsyncLocalStorage`. All subsequent calls | ||
to `asyncLocalStorage.getStore()` will return `undefined` until | ||
`asyncLocalStorage.run()` is called again. | ||
|
||
When calling `asyncLocalStorage.disable()`, all current contexts linked to the | ||
instance will be exited. | ||
|
||
Calling `asyncLocalStorage.disable()` is required before the | ||
`asyncLocalStorage` can be garbage collected. This does not apply to stores | ||
provided by the `asyncLocalStorage`, as those objects are garbage collected | ||
along with the corresponding async resources. | ||
|
||
This method is to be used when the `asyncLocalStorage` is not in use anymore | ||
in the current process. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<!-- YAML | ||
added: v13.11.0 | ||
--> | ||
|
||
* `store` {any} | ||
|
||
Calling `asyncLocalStorage.enterWith(store)` will transition into the context | ||
for the remainder of the current synchronous execution and will persist | ||
through any following asynchronous calls. | ||
|
||
Example: | ||
|
||
```js | ||
const store = { id: 1 }; | ||
asyncLocalStorage.enterWith(store); | ||
asyncLocalStorage.getStore(); // Returns the store object | ||
someAsyncOperation(() => { | ||
asyncLocalStorage.getStore(); // Returns the same object | ||
}); | ||
``` | ||
|
||
This transition will continue for the _entire_ synchronous execution. | ||
This means that if, for example, the context is entered within an event | ||
handler subsequent event handlers will also run within that context unless | ||
specifically bound to another context with an `AsyncResource`. | ||
|
||
```js | ||
const store = { id: 1 }; | ||
emitter.on('my-event', () => { | ||
asyncLocalStorage.enterWith(store); | ||
}); | ||
emitter.on('my-event', () => { | ||
asyncLocalStorage.getStore(); // Returns the same object | ||
}); | ||
asyncLocalStorage.getStore(); // Returns undefined | ||
emitter.emit('my-event'); | ||
asyncLocalStorage.getStore(); // Returns the same object | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<!-- YAML | ||
added: v13.10.0 | ||
--> | ||
|
||
* `callback` {Function} | ||
* `...args` {any} | ||
|
||
This methods runs a function synchronously outside of a context and return its | ||
return value. The store is not accessible within the callback function or | ||
the asynchronous operations created within the callback. | ||
|
||
Optionally, arguments can be passed to the function. They will be passed to | ||
the callback function. | ||
|
||
If the callback function throws an error, it will be thrown by `exit` too. | ||
The stacktrace will not be impacted by this call and | ||
the context will be re-entered. | ||
|
||
Example: | ||
|
||
```js | ||
// Within a call to run | ||
try { | ||
asyncLocalStorage.getStore(); // Returns the store object or value | ||
asyncLocalStorage.exit(() => { | ||
asyncLocalStorage.getStore(); // Returns undefined | ||
throw new Error(); | ||
}); | ||
} catch (e) { | ||
asyncLocalStorage.getStore(); // Returns the same object or value | ||
// The error will be caught here | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!-- YAML | ||
added: v13.10.0 | ||
--> | ||
|
||
* Returns: {any} | ||
|
||
This method returns the current store. | ||
If this method is called outside of an asynchronous context initialized by | ||
calling `asyncLocalStorage.run`, it will return `undefined`. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<!-- YAML | ||
added: v13.10.0 | ||
--> | ||
|
||
* `store` {any} | ||
* `callback` {Function} | ||
* `...args` {any} | ||
|
||
This methods runs a function synchronously within a context and return its | ||
return value. The store is not accessible outside of the callback function or | ||
the asynchronous operations created within the callback. | ||
|
||
Optionally, arguments can be passed to the function. They will be passed to | ||
the callback function. | ||
|
||
If the callback function throws an error, it will be thrown by `run` too. | ||
The stacktrace will not be impacted by this call and the context will | ||
be exited. | ||
|
||
Example: | ||
|
||
```js | ||
const store = { id: 2 }; | ||
try { | ||
asyncLocalStorage.run(store, () => { | ||
asyncLocalStorage.getStore(); // Returns the store object | ||
throw new Error(); | ||
}); | ||
} catch (e) { | ||
asyncLocalStorage.getStore(); // Returns undefined | ||
// The error will be caught here | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
The class `AsyncHook` exposes an interface for tracking lifetime events | ||
of asynchronous operations. | ||
|
Oops, something went wrong.