forked from nodejs/node-addon-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cc
More file actions
84 lines (69 loc) · 2.53 KB
/
example.cc
File metadata and controls
84 lines (69 loc) · 2.53 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <napi.h>
#include <memory>
using namespace Napi;
class AsyncIteratorExample : public ObjectWrap<AsyncIteratorExample> {
public:
AsyncIteratorExample(const CallbackInfo& info)
: ObjectWrap<AsyncIteratorExample>(info),
_from(info[0].As<Number>()),
_to(info[1].As<Number>()) {}
static Object Init(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(
env,
"AsyncIteratorExample",
{InstanceMethod(Napi::Symbol::WellKnown(env, "asyncIterator"),
&AsyncIteratorExample::Iterator)});
exports.Set("AsyncIteratorExample", func);
return exports;
}
Napi::Value Iterator(const CallbackInfo& info) {
auto env = info.Env();
auto iteratorObject = Napi::Object::New(env);
iteratorObject["current"] = Number::New(env, _from);
iteratorObject["last"] = Number::New(env, _to);
auto next = Function::New(env, [](const CallbackInfo& info) {
auto env = info.Env();
auto deferred =
std::make_shared<Promise::Deferred>(Promise::Deferred::New(env));
auto iteratorObject = info.This().As<Object>();
auto callback = Function::New(
env,
[=](const CallbackInfo& info) {
auto env = info.Env();
auto value = Object::New(env);
auto iteratorObject = info.This().As<Object>();
auto current =
iteratorObject.Get("current").As<Number>().Int32Value();
auto last = iteratorObject.Get("last").As<Number>().Int32Value();
auto done = current > last;
if (done) {
value["done"] = Boolean::New(env, true);
} else {
value["done"] = Boolean::New(env, false);
value["value"] = Number::New(env, current);
iteratorObject["current"] = Number::New(env, current + 1);
}
deferred->Resolve(value);
},
"next");
env.Global()
.Get("setTimeout")
.As<Function>()
.Call({callback.Get("bind").As<Function>().Call(callback,
{iteratorObject}),
Number::New(env, 1000)});
return deferred->Promise();
});
iteratorObject["next"] =
next.Get("bind").As<Function>().Call(next, {iteratorObject});
return iteratorObject;
}
private:
int _from;
int _to;
};
Napi::Object Init(Napi::Env env, Object exports) {
AsyncIteratorExample::Init(env, exports);
return exports;
}
NODE_API_MODULE(example, Init)