remote: return webcontents instance from cache
authorRobo <hop2deep@gmail.com>
Wed, 17 Feb 2016 07:57:46 +0000 (13:27 +0530)
committerRobo <hop2deep@gmail.com>
Thu, 18 Feb 2016 12:10:10 +0000 (17:40 +0530)
atom/renderer/api/lib/remote.js
spec/api-ipc-spec.js

index ad01e77ac51bb42f498914ab4542607f4e2170a9..d8a5508621e0219323306ec73d801684004567e8 100644 (file)
@@ -1,11 +1,14 @@
 const ipcRenderer = require('electron').ipcRenderer;
 const CallbacksRegistry = require('electron').CallbacksRegistry;
 const v8Util = process.atomBinding('v8_util');
+const IDWeakMap = process.atomBinding('id_weak_map').IDWeakMap;
 
 const callbacksRegistry = new CallbacksRegistry;
 
 var includes = [].includes;
 
+var remoteObjectCache = new IDWeakMap;
+
 // Check for circular reference.
 var isCircular = function(field, visited) {
   if (typeof field === 'object') {
@@ -154,6 +157,9 @@ var metaToValue = function(meta) {
         }
       }
 
+      if (remoteObjectCache.has(meta.id))
+        return remoteObjectCache.get(meta.id);
+
       // Track delegate object's life time, and tell the browser to clean up
       // when the object is GCed.
       v8Util.setDestructor(ret, function() {
@@ -162,6 +168,7 @@ var metaToValue = function(meta) {
 
       // Remember object's id.
       v8Util.setHiddenValue(ret, 'atomId', meta.id);
+      remoteObjectCache.set(meta.id, ret);
       return ret;
   }
 };
index 2c8bc28f1560fe848d981d544328306dbc82368c..9cf741412b082436a51e2091b5fbf4acdb40151a 100644 (file)
@@ -90,6 +90,14 @@ describe('ipc module', function() {
     });
   });
 
+  describe('remote webContents', function() {
+    it('can return same object with different getters', function() {
+      var contents1 = remote.getCurrentWindow().webContents;
+      var contents2 = remote.getCurrentWebContents();
+      assert(contents1 == contents2);
+    });
+  });
+
   describe('ipc.sender.send', function() {
     it('should work when sending an object containing id property', function(done) {
       var obj = {