-
Notifications
You must be signed in to change notification settings - Fork 29.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
async_hooks: add AsyncResource.bind utility #34574
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,15 @@ | |
|
||
const { | ||
NumberIsSafeInteger, | ||
ObjectDefineProperties, | ||
ReflectApply, | ||
Symbol, | ||
} = primordials; | ||
|
||
const { | ||
ERR_ASYNC_CALLBACK, | ||
ERR_ASYNC_TYPE, | ||
ERR_INVALID_ARG_TYPE, | ||
ERR_INVALID_ASYNC_ID | ||
} = require('internal/errors').codes; | ||
const { validateString } = require('internal/validators'); | ||
|
@@ -211,6 +213,28 @@ class AsyncResource { | |
triggerAsyncId() { | ||
return this[trigger_async_id_symbol]; | ||
} | ||
|
||
bind(fn) { | ||
if (typeof fn !== 'function') | ||
throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn); | ||
const ret = this.runInAsyncScope.bind(this, fn); | ||
ObjectDefineProperties(ret, { | ||
'length': { | ||
enumerable: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
value: fn.length, | ||
}, | ||
'asyncResource': { | ||
enumerable: true, | ||
value: this, | ||
} | ||
}); | ||
return ret; | ||
} | ||
|
||
static bind(fn, type) { | ||
type = type || fn.name; | ||
return (new AsyncResource(type || 'bound-anonymous-fn')).bind(fn); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no points will be deducted for using a temporary variable :P There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. parentheses unnecessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah but stylistically I prefer them (more readable in my opinion) |
||
} | ||
} | ||
|
||
const storageList = []; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { AsyncResource, executionAsyncId } = require('async_hooks'); | ||
|
||
const fn = common.mustCall(AsyncResource.bind(() => { | ||
return executionAsyncId(); | ||
})); | ||
|
||
setImmediate(() => { | ||
const asyncId = executionAsyncId(); | ||
assert.notStrictEqual(asyncId, fn()); | ||
}); | ||
|
||
const asyncResource = new AsyncResource('test'); | ||
|
||
[1, false, '', {}, []].forEach((i) => { | ||
assert.throws(() => asyncResource.bind(i), { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
}); | ||
|
||
const fn2 = asyncResource.bind((a, b) => { | ||
return executionAsyncId(); | ||
}); | ||
|
||
assert.strictEqual(fn2.asyncResource, asyncResource); | ||
assert.strictEqual(fn2.length, 2); | ||
|
||
setImmediate(() => { | ||
const asyncId = executionAsyncId(); | ||
assert.strictEqual(asyncResource.asyncId(), fn2()); | ||
assert.notStrictEqual(asyncId, fn2()); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think both properties should use
configurable: true
.