forked from kenny-y/wn-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeal.cpp
75 lines (58 loc) · 1.76 KB
/
meal.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
70
71
72
73
74
75
#include "meal.h"
Meal::Meal():
type_(""),
size_(0.0),
raw_meal_(true) {
}
Meal::Meal(const std::string& type) :
type_(type),
size_(1.0),
raw_meal_(true) {
// TODO: init your members
}
Meal::Meal(const std::string& type, const double& size) :
type_(type),
size_(size),
raw_meal_(true) {
// TODO: init your members
}
Meal::~Meal() {
}
void Meal::prepare(std::string type, double size) {
type_ = type;
size_ = size;
raw_meal_ = true;
}
void Meal::prepare(std::string type) {
this->prepare(type, 1.0);
}
v8::Handle<v8::Promise> Meal::cook(const std::string& chefName, v8::Isolate* isolate) {
using ResolverPersistent = Nan::Persistent<v8::Promise::Resolver>;
auto period = 3000; // In ms
auto resolver = v8::Promise::Resolver::New(isolate);
auto persistent = new ResolverPersistent(resolver);
struct MealData {
Meal* myself;
ResolverPersistent* persistent;
};
uv_timer_t* handle = new uv_timer_t;
handle->data = new MealData{this, persistent};
uv_timer_init(uv_default_loop(), handle);
// use capture-less lambda for c-callback
auto timercb = [](uv_timer_t* handle) -> void {
Nan::HandleScope scope;
auto persistent = static_cast<MealData*>(handle->data)->persistent;
auto myself = static_cast<MealData*>(handle->data)->myself;
delete static_cast<MealData*>(handle->data);
uv_timer_stop(handle);
uv_close(reinterpret_cast<uv_handle_t*>(handle),
[](uv_handle_t* handle) -> void {delete handle;});
myself->raw_meal_ = false;
auto resolver = Nan::New(*persistent);
resolver->Resolve(Nan::New("Microwave Ding").ToLocalChecked());
persistent->Reset();
delete persistent;
};
uv_timer_start(handle, timercb, period, 0);
return resolver->GetPromise();
}