Last active
March 11, 2018 11:21
-
-
Save guyguyon/66f5f15a38f16f38e56344bada49e002 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // node-request-context/index.js | |
| const asyncHooks = require('async_hooks'); | |
| function createHooks(namespace) { | |
| function init(asyncId, type, triggerId, resource) { | |
| // Check if trigger has context | |
| // Or in other words, check if tigger is part of any request | |
| if (namespace.context[triggerId]) { | |
| // Here we keep passing the context from | |
| // the triggerId to the new asyncId | |
| namespace.context[asyncId] = namespace.context[triggerId]; | |
| } | |
| } | |
| function destroy(asyncId) { | |
| delete namespace.context[asyncId]; | |
| } | |
| const asyncHook = asyncHooks.createHook({ init, destroy }); | |
| asyncHook.enable(); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment