Throw error when session module is used before app is ready
authorCheng Zhao <zcbenz@gmail.com>
Wed, 1 Jun 2016 01:53:06 +0000 (10:53 +0900)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 1 Jun 2016 01:53:06 +0000 (10:53 +0900)
lib/browser/api/session.js

index 49b169b..dc1c529 100644 (file)
@@ -1,16 +1,26 @@
 const {EventEmitter} = require('events')
+const electron = require('electron')
 const bindings = process.atomBinding('session')
 
 const PERSIST_PREFIX = 'persist:'
 
+// Wrapper of binding.fromPartition that checks for ready event.
+const fromPartition = function (partition, persist) {
+  if (!electron.app.isReady()) {
+    throw new Error('session module can only be used when app is ready')
+  }
+
+  return bindings.fromPartition(partition, persist)
+}
+
 // Returns the Session from |partition| string.
 exports.fromPartition = function (partition = '') {
   if (partition === '') return exports.defaultSession
 
   if (partition.startsWith(PERSIST_PREFIX)) {
-    return bindings.fromPartition(partition.substr(PERSIST_PREFIX.length), false)
+    return fromPartition(partition.substr(PERSIST_PREFIX.length), false)
   } else {
-    return bindings.fromPartition(partition, true)
+    return fromPartition(partition, true)
   }
 }
 
@@ -18,7 +28,7 @@ exports.fromPartition = function (partition = '') {
 Object.defineProperty(exports, 'defaultSession', {
   enumerable: true,
   get: function () {
-    return bindings.fromPartition('', false)
+    return fromPartition('', false)
   }
 })