2018-06-27 21:06:08 +00:00
|
|
|
// 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>
|
|
|
|
|
2019-01-15 17:54:59 +00:00
|
|
|
#include "atom/common/api/locker.h"
|
2019-02-18 14:25:28 +00:00
|
|
|
#include "atom/common/native_mate_converters/callback.h"
|
2018-06-27 21:06:08 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
#include "native_mate/converter.h"
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace util {
|
|
|
|
|
2018-09-27 14:59:23 +00:00
|
|
|
class Promise : public base::RefCounted<Promise> {
|
2018-06-27 21:06:08 +00:00
|
|
|
public:
|
2018-06-28 21:20:11 +00:00
|
|
|
explicit Promise(v8::Isolate* isolate);
|
2018-06-27 21:06:08 +00:00
|
|
|
|
|
|
|
v8::Isolate* isolate() const { return isolate_; }
|
2019-01-15 17:54:59 +00:00
|
|
|
v8::Local<v8::Context> GetContext() {
|
|
|
|
return v8::Local<v8::Context>::New(isolate_, context_);
|
|
|
|
}
|
2018-06-27 21:06:08 +00:00
|
|
|
|
2018-09-27 14:59:23 +00:00
|
|
|
virtual v8::Local<v8::Promise> GetHandle() const;
|
2018-06-27 21:06:08 +00:00
|
|
|
|
|
|
|
v8::Maybe<bool> Resolve() {
|
2019-01-15 17:54:59 +00:00
|
|
|
v8::HandleScope handle_scope(isolate());
|
|
|
|
v8::MicrotasksScope script_scope(isolate(),
|
|
|
|
v8::MicrotasksScope::kRunMicrotasks);
|
|
|
|
v8::Context::Scope context_scope(
|
|
|
|
v8::Local<v8::Context>::New(isolate(), GetContext()));
|
|
|
|
|
|
|
|
return GetInner()->Resolve(GetContext(), v8::Undefined(isolate()));
|
2018-06-27 21:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v8::Maybe<bool> Reject() {
|
2019-01-15 17:54:59 +00:00
|
|
|
v8::HandleScope handle_scope(isolate());
|
|
|
|
v8::MicrotasksScope script_scope(isolate(),
|
|
|
|
v8::MicrotasksScope::kRunMicrotasks);
|
|
|
|
v8::Context::Scope context_scope(
|
|
|
|
v8::Local<v8::Context>::New(isolate(), GetContext()));
|
|
|
|
|
|
|
|
return GetInner()->Reject(GetContext(), v8::Undefined(isolate()));
|
2018-06-27 21:06:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 14:25:28 +00:00
|
|
|
v8::MaybeLocal<v8::Promise> Then(base::Closure cb) {
|
|
|
|
v8::HandleScope handle_scope(isolate());
|
|
|
|
v8::Context::Scope context_scope(
|
|
|
|
v8::Local<v8::Context>::New(isolate(), GetContext()));
|
|
|
|
|
|
|
|
v8::Local<v8::Value> value = mate::ConvertToV8(isolate(), cb);
|
|
|
|
v8::Local<v8::Function> handler = v8::Local<v8::Function>::Cast(value);
|
|
|
|
|
|
|
|
return GetHandle()->Then(GetContext(), handler);
|
|
|
|
}
|
|
|
|
|
2018-10-15 15:26:47 +00:00
|
|
|
// Promise resolution is a microtask
|
|
|
|
// We use the MicrotasksRunner to trigger the running of pending microtasks
|
2018-06-27 21:06:08 +00:00
|
|
|
template <typename T>
|
2018-09-27 14:59:23 +00:00
|
|
|
v8::Maybe<bool> Resolve(const T& value) {
|
2019-01-15 17:54:59 +00:00
|
|
|
v8::HandleScope handle_scope(isolate());
|
|
|
|
v8::MicrotasksScope script_scope(isolate(),
|
|
|
|
v8::MicrotasksScope::kRunMicrotasks);
|
|
|
|
v8::Context::Scope context_scope(
|
|
|
|
v8::Local<v8::Context>::New(isolate(), GetContext()));
|
|
|
|
|
|
|
|
return GetInner()->Resolve(GetContext(),
|
2018-06-27 21:06:08 +00:00
|
|
|
mate::ConvertToV8(isolate(), value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2018-09-27 14:59:23 +00:00
|
|
|
v8::Maybe<bool> Reject(const T& value) {
|
2019-01-15 17:54:59 +00:00
|
|
|
v8::HandleScope handle_scope(isolate());
|
|
|
|
v8::MicrotasksScope script_scope(isolate(),
|
|
|
|
v8::MicrotasksScope::kRunMicrotasks);
|
|
|
|
v8::Context::Scope context_scope(
|
|
|
|
v8::Local<v8::Context>::New(isolate(), GetContext()));
|
|
|
|
|
|
|
|
return GetInner()->Reject(GetContext(),
|
2018-06-27 21:06:08 +00:00
|
|
|
mate::ConvertToV8(isolate(), value));
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Maybe<bool> RejectWithErrorMessage(const std::string& error);
|
|
|
|
|
|
|
|
protected:
|
2018-09-27 14:59:23 +00:00
|
|
|
virtual ~Promise();
|
|
|
|
friend class base::RefCounted<Promise>;
|
2018-06-27 21:06:08 +00:00
|
|
|
v8::Isolate* isolate_;
|
2019-01-15 17:54:59 +00:00
|
|
|
v8::Global<v8::Context> context_;
|
2018-06-27 21:06:08 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
v8::Local<v8::Promise::Resolver> GetInner() const {
|
|
|
|
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_
|