Add mate::Promise
This commit is contained in:
Родитель
bb2fb93355
Коммит
b180f18b7e
|
@ -20,7 +20,7 @@ Dictionary::~Dictionary() {
|
|||
}
|
||||
|
||||
Dictionary Dictionary::CreateEmpty(v8::Isolate* isolate) {
|
||||
return Dictionary(isolate, v8::Object::New(isolate));;
|
||||
return Dictionary(isolate, v8::Object::New(isolate));
|
||||
}
|
||||
|
||||
v8::Local<v8::Object> Dictionary::GetHandle() const {
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
// Copyright (c) 2018 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "native_mate/promise.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
Promise::Promise()
|
||||
: isolate_(NULL) {
|
||||
}
|
||||
|
||||
Promise::Promise(v8::Isolate* isolate)
|
||||
: isolate_(isolate) {
|
||||
resolver_ = v8::Promise::Resolver::New(isolate);
|
||||
}
|
||||
|
||||
Promise::~Promise() {
|
||||
}
|
||||
|
||||
Promise Promise::Create(v8::Isolate* isolate) {
|
||||
return Promise(isolate);
|
||||
}
|
||||
|
||||
Promise Promise::Create() {
|
||||
return Promise::Create(v8::Isolate::GetCurrent());
|
||||
}
|
||||
|
||||
void Promise::RejectWithErrorMessage(const std::string& string) {
|
||||
v8::Local<v8::String> error_message =
|
||||
v8::String::NewFromUtf8(isolate(), string.c_str());
|
||||
v8::Local<v8::Value> error = v8::Exception::Error(error_message);
|
||||
resolver_->Reject(mate::ConvertToV8(isolate(), error));
|
||||
}
|
||||
|
||||
v8::Local<v8::Object> Promise::GetHandle() const {
|
||||
return resolver_->GetPromise();
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> Converter<Promise>::ToV8(v8::Isolate* isolate,
|
||||
Promise val) {
|
||||
return val.GetHandle();
|
||||
}
|
||||
|
||||
} // namespace mate
|
|
@ -0,0 +1,57 @@
|
|||
// Copyright (c) 2018 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef NATIVE_MATE_PROMISE_H_
|
||||
#define NATIVE_MATE_PROMISE_H_
|
||||
|
||||
#include "native_mate/converter.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
class Promise {
|
||||
public:
|
||||
Promise();
|
||||
Promise(v8::Isolate* isolate);
|
||||
virtual ~Promise();
|
||||
|
||||
static Promise Create(v8::Isolate* isolate);
|
||||
static Promise Create();
|
||||
|
||||
v8::Isolate* isolate() const { return isolate_; }
|
||||
|
||||
virtual v8::Local<v8::Object> GetHandle() const;
|
||||
|
||||
template<typename T>
|
||||
void Resolve(T* value) {
|
||||
resolver_->Resolve(mate::ConvertToV8(isolate(), value));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Reject(T* value) {
|
||||
resolver_->Reject(mate::ConvertToV8(isolate(), value));
|
||||
}
|
||||
|
||||
void RejectWithErrorMessage(const std::string& error);
|
||||
|
||||
protected:
|
||||
v8::Isolate* isolate_;
|
||||
|
||||
private:
|
||||
v8::Local<v8::Promise::Resolver> resolver_;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Converter<Promise> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
Promise val);
|
||||
// TODO(MarshallOfSound): Implement FromV8 to allow promise chaining
|
||||
// in native land
|
||||
// static bool FromV8(v8::Isolate* isolate,
|
||||
// v8::Local<v8::Value> val,
|
||||
// Promise* out);
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
|
||||
#endif // NATIVE_MATE_PROMISE_H_
|
|
@ -22,6 +22,8 @@
|
|||
'native_mate/wrappable.cc',
|
||||
'native_mate/wrappable.h',
|
||||
'native_mate/wrappable_base.h',
|
||||
'native_mate/promise.h',
|
||||
'native_mate/promise.cc',
|
||||
],
|
||||
},
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче