Merge pull request #1426 from deepak1556/api_pm_patch

pm: throw when initialising module before ready event
This commit is contained in:
Cheng Zhao 2015-04-20 20:02:44 +08:00
commit 5bdb4b9aa1
2 changed files with 11 additions and 5 deletions

View file

@ -4,6 +4,7 @@
#include "atom/browser/api/atom_api_power_monitor.h" #include "atom/browser/api/atom_api_power_monitor.h"
#include "atom/browser/browser.h"
#include "base/power_monitor/power_monitor.h" #include "base/power_monitor/power_monitor.h"
#include "base/power_monitor/power_monitor_device_source.h" #include "base/power_monitor/power_monitor_device_source.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
@ -38,8 +39,14 @@ void PowerMonitor::OnResume() {
} }
// static // static
mate::Handle<PowerMonitor> PowerMonitor::Create(v8::Isolate* isolate) { v8::Handle<v8::Value> PowerMonitor::Create(v8::Isolate* isolate) {
return CreateHandle(isolate, new PowerMonitor); if (!Browser::Get()->is_ready()) {
node::ThrowError("Cannot initialize \"power-monitor\" module"
"before app is ready");
return v8::Null(isolate);
}
return CreateHandle(isolate, new PowerMonitor).ToV8();
} }
} // namespace api } // namespace api
@ -57,9 +64,8 @@ void Initialize(v8::Handle<v8::Object> exports, v8::Handle<v8::Value> unused,
using atom::api::PowerMonitor; using atom::api::PowerMonitor;
v8::Isolate* isolate = context->GetIsolate(); v8::Isolate* isolate = context->GetIsolate();
mate::Handle<PowerMonitor> power_monitor = PowerMonitor::Create(isolate);
mate::Dictionary dict(isolate, exports); mate::Dictionary dict(isolate, exports);
dict.Set("powerMonitor", power_monitor); dict.Set("powerMonitor", PowerMonitor::Create(isolate));
} }
} // namespace } // namespace

View file

@ -17,7 +17,7 @@ namespace api {
class PowerMonitor : public mate::EventEmitter, class PowerMonitor : public mate::EventEmitter,
public base::PowerObserver { public base::PowerObserver {
public: public:
static mate::Handle<PowerMonitor> Create(v8::Isolate* isolate); static v8::Handle<v8::Value> Create(v8::Isolate* isolate);
protected: protected:
PowerMonitor(); PowerMonitor();