-
Notifications
You must be signed in to change notification settings - Fork 504
/
Copy pathasyncresource.cpp
69 lines (53 loc) · 1.83 KB
/
asyncresource.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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 "sleep.h" // NOLINT(build/include)
using namespace Nan; // NOLINT(build/namespaces)
class DelayRequest : public AsyncResource {
public:
DelayRequest(int milliseconds_, v8::Local<v8::Function> callback_)
: AsyncResource("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);
}
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::Object> target = New<v8::Object>();
// Run the callback in the async context.
delay_request->runInAsyncScope(target, callback, 0, NULL);
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(
GetCurrentEventLoop()
, &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)