-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds support for the AsyncResource API to allow native modules to asynchronously call back into JavaScript while preserving node's async context. This acts as a higher level alternative to the MakeCallback API. This is analogous to the AsyncResource JavaScript class exposed by [async_hooks][] and similar to the `napi_async_init`, `napi_async_destroy` and `napi_make_callback` APIs, albeit wrapped in a convenient RAII form-factor. Ref: nodejs/node#13254 [N-API]: https://nodejs.org/dist/latest-v9.x/docs/api/n-api.html#n_api_custom_asynchronous_operations [async_hooks]: https://nodejs.org/api/async_hooks.html
- Loading branch information
Showing
6 changed files
with
300 additions
and
2 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
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,69 @@ | ||
/********************************************************************* | ||
* NAN - Native Abstractions for Node.js | ||
* | ||
* Copyright (c) 2018 NAN contributors | ||
* | ||
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md> | ||
********************************************************************/ | ||
|
||
#include <nan.h> | ||
#include <unistd.h> | ||
|
||
using namespace Nan; // NOLINT(build/namespaces) | ||
|
||
class DelayRequest : public AsyncResource { | ||
public: | ||
DelayRequest(int milliseconds_, v8::Local<v8::Function> callback_) | ||
: AsyncResource(MaybeLocal<v8::Object>(), "nan:test.DelayRequest"), | ||
milliseconds(milliseconds_) { | ||
callback.Reset(callback_); | ||
request.data = this; | ||
} | ||
~DelayRequest() { | ||
callback.Reset(); | ||
} | ||
|
||
Persistent<v8::Function> callback; | ||
uv_work_t request; | ||
int milliseconds; | ||
}; | ||
|
||
void Delay(uv_work_t* req) { | ||
DelayRequest *delay_request = static_cast<DelayRequest*>(req->data); | ||
sleep(delay_request->milliseconds / 1000); | ||
} | ||
|
||
void AfterDelay(uv_work_t* req, int status) { | ||
HandleScope scope; | ||
|
||
DelayRequest *delay_request = static_cast<DelayRequest*>(req->data); | ||
v8::Local<v8::Function> callback = New(delay_request->callback); | ||
v8::Local<v8::Value> argv[0] = {}; | ||
|
||
v8::Local<v8::Object> target = New<v8::Object>(); | ||
|
||
// Run the callback in the async context. | ||
delay_request->runInAsyncScope(target, callback, 0, argv); | ||
|
||
delete delay_request; | ||
} | ||
|
||
NAN_METHOD(Delay) { | ||
int delay = To<int>(info[0]).FromJust(); | ||
v8::Local<v8::Function> cb = To<v8::Function>(info[1]).ToLocalChecked(); | ||
|
||
DelayRequest* delay_request = new DelayRequest(delay, cb); | ||
|
||
uv_queue_work( | ||
uv_default_loop() | ||
, &delay_request->request | ||
, Delay | ||
, reinterpret_cast<uv_after_work_cb>(AfterDelay)); | ||
} | ||
|
||
NAN_MODULE_INIT(Init) { | ||
Set(target, New<v8::String>("delay").ToLocalChecked(), | ||
GetFunction(New<v8::FunctionTemplate>(Delay)).ToLocalChecked()); | ||
} | ||
|
||
NODE_MODULE(asyncresource, Init) |
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,70 @@ | ||
/********************************************************************* | ||
* NAN - Native Abstractions for Node.js | ||
* | ||
* Copyright (c) 2018 NAN contributors | ||
* | ||
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md> | ||
********************************************************************/ | ||
|
||
try { | ||
require('async_hooks'); | ||
} catch (e) { | ||
process.exit(0); | ||
} | ||
|
||
const test = require('tap').test | ||
, testRoot = require('path').resolve(__dirname, '..') | ||
, delay = require('bindings')({ module_root: testRoot, bindings: 'asyncresource' }).delay | ||
, asyncHooks = require('async_hooks'); | ||
|
||
test('asyncresource', function (t) { | ||
t.plan(7); | ||
|
||
var resourceAsyncId; | ||
var originalExecutionAsyncId; | ||
var beforeCalled = false; | ||
var afterCalled = false; | ||
var destroyCalled = false; | ||
|
||
var hooks = asyncHooks.createHook({ | ||
init: function(asyncId, type, triggerAsyncId, resource) { | ||
if (type === 'nan:test.DelayRequest') { | ||
resourceAsyncId = asyncId; | ||
} | ||
}, | ||
before: function(asyncId) { | ||
if (asyncId === resourceAsyncId) { | ||
beforeCalled = true; | ||
} | ||
}, | ||
after: function(asyncId) { | ||
if (asyncId === resourceAsyncId) { | ||
afterCalled = true; | ||
} | ||
}, | ||
destroy: function(asyncId) { | ||
if (asyncId === resourceAsyncId) { | ||
destroyCalled = true; | ||
} | ||
} | ||
|
||
}); | ||
hooks.enable(); | ||
|
||
originalExecutionAsyncId = asyncHooks.executionAsyncId(); | ||
delay(1000, function() { | ||
t.equal(asyncHooks.executionAsyncId(), resourceAsyncId, | ||
'callback should have the correct execution context'); | ||
t.equal(asyncHooks.triggerAsyncId(), originalExecutionAsyncId, | ||
'callback should have the correct trigger context'); | ||
t.ok(beforeCalled, 'before should have been called'); | ||
t.notOk(afterCalled, 'after should not have been called yet'); | ||
setTimeout(function() { | ||
t.ok(afterCalled, 'after should have been called'); | ||
t.ok(destroyCalled, 'destroy should have been called'); | ||
t.equal(asyncHooks.triggerAsyncId(), resourceAsyncId, | ||
'setTimeout should have been triggered by the async resource'); | ||
hooks.disable(); | ||
}, 1); | ||
}); | ||
}); |