Skip to content

Instantly share code, notes, and snippets.

@watson
Forked from garybernhardt/print-leaked-events.js
Created March 4, 2019 21:55
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save watson/3e1e8296b00f7b93e63f393b5ac45c38 to your computer and use it in GitHub Desktop.
'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