refactor: add promise helper and change whenReady to be native impl (#13115)
* Add promise helper and change whenReady to be native impl * remove commented code * add GetInner helper to dedupe promise code * add Promise.reject helper to be consistent with JS * fix linting * update promise impl per feedback * remove param name from unused isolate * Use non-depreceated resolvers for promises * Add thread dchecks for promise helper, intiialize promise pointer to nullptr
This commit is contained in:
parent
e9971173d4
commit
92588be2bd
8 changed files with 145 additions and 18 deletions
37
atom/common/promise_util.cc
Normal file
37
atom/common/promise_util.cc
Normal file
|
@ -0,0 +1,37 @@
|
|||
// 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 "atom/common/promise_util.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace util {
|
||||
|
||||
v8::Maybe<bool> 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);
|
||||
return GetInner()->Reject(isolate()->GetCurrentContext(),
|
||||
mate::ConvertToV8(isolate(), error));
|
||||
}
|
||||
|
||||
v8::Local<v8::Object> Promise::GetHandle() const {
|
||||
return GetInner()->GetPromise();
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
|
||||
} // namespace atom
|
||||
|
||||
namespace mate {
|
||||
|
||||
v8::Local<v8::Value> mate::Converter<atom::util::Promise*>::ToV8(
|
||||
v8::Isolate*,
|
||||
atom::util::Promise* val) {
|
||||
return val->GetHandle();
|
||||
}
|
||||
|
||||
} // namespace mate
|
87
atom/common/promise_util.h
Normal file
87
atom/common/promise_util.h
Normal file
|
@ -0,0 +1,87 @@
|
|||
// 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 ATOM_COMMON_PROMISE_UTIL_H_
|
||||
#define ATOM_COMMON_PROMISE_UTIL_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "native_mate/converter.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace util {
|
||||
|
||||
class Promise {
|
||||
public:
|
||||
explicit Promise(v8::Isolate* isolate) {
|
||||
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
||||
isolate_ = isolate;
|
||||
resolver_.Reset(isolate, v8::Promise::Resolver::New(isolate));
|
||||
}
|
||||
~Promise() {}
|
||||
|
||||
v8::Isolate* isolate() const { return isolate_; }
|
||||
|
||||
virtual v8::Local<v8::Object> GetHandle() const;
|
||||
|
||||
v8::Maybe<bool> Resolve() {
|
||||
return GetInner()->Resolve(isolate()->GetCurrentContext(),
|
||||
v8::Undefined(isolate()));
|
||||
}
|
||||
|
||||
v8::Maybe<bool> Reject() {
|
||||
return GetInner()->Reject(isolate()->GetCurrentContext(),
|
||||
v8::Undefined(isolate()));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
v8::Maybe<bool> Resolve(T* value) {
|
||||
return GetInner()->Resolve(isolate()->GetCurrentContext(),
|
||||
mate::ConvertToV8(isolate(), value));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
v8::Maybe<bool> Reject(T* value) {
|
||||
return GetInner()->Reject(isolate()->GetCurrentContext(),
|
||||
mate::ConvertToV8(isolate(), value));
|
||||
}
|
||||
|
||||
v8::Maybe<bool> RejectWithErrorMessage(const std::string& error);
|
||||
|
||||
protected:
|
||||
v8::Isolate* isolate_;
|
||||
|
||||
private:
|
||||
v8::Local<v8::Promise::Resolver> GetInner() const {
|
||||
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
||||
return resolver_.Get(isolate());
|
||||
}
|
||||
|
||||
v8::Global<v8::Promise::Resolver> resolver_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(Promise);
|
||||
};
|
||||
|
||||
} // namespace util
|
||||
|
||||
} // namespace atom
|
||||
|
||||
namespace mate {
|
||||
|
||||
template <>
|
||||
struct Converter<atom::util::Promise*> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
atom::util::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 // ATOM_COMMON_PROMISE_UTIL_H_
|
Loading…
Add table
Add a link
Reference in a new issue