-
-
Save watson/3e1e8296b00f7b93e63f393b5ac45c38 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
'use strict' | |
const asyncHooks = require('async_hooks') | |
const asyncIds = new Set() | |
const asyncHook = asyncHooks.createHook({ | |
init (asyncId, type, triggerAsyncId, resource) { | |
const eid = asyncHooks.executionAsyncId() | |
process._rawDebug(`${type}(${asyncId}): trigger: ${triggerAsyncId} execution: ${eid}`, resource) | |
}, | |
before (asyncId) { | |
process._rawDebug(`before(${asyncId})`) | |
asyncIds.add(asyncId) | |
}, | |
after (asyncId) { | |
process._rawDebug(`after(${asyncId})`) | |
asyncIds.delete(asyncId) | |
}, | |
destroy (asyncId) { | |
process._rawDebug(`destroy(${asyncId})`) | |
} | |
}) | |
asyncHook.enable() | |
async function printLeakedEvents (f) { | |
process._rawDebug('-- printLeakedEvents start') | |
const before = new Set(asyncIds.values()) | |
const value = await f() | |
process._rawDebug('-- value:', value) | |
const diff = difference(asyncIds, before) | |
process._rawDebug('-- before:', before) | |
process._rawDebug('-- after:', asyncIds) | |
process._rawDebug('-- diff:', diff) | |
process._rawDebug('-- printLeakedEvents end') | |
} | |
function difference (setA, setB) { | |
var _difference = new Set(setA) | |
for (var elem of setB) { | |
_difference.delete(elem) | |
} | |
return _difference | |
} | |
async function main () { | |
await printLeakedEvents(async () => 0) | |
await printLeakedEvents(async () => 1) | |
await printLeakedEvents(async () => new Promise(function (resolve) { resolve(2) })) | |
await printLeakedEvents(async () => { | |
new Promise(function (resolve, reject) { | |
process._rawDebug('-- resolving...') | |
resolve() | |
}) | |
}) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment