No longer need to count the references in webContents
All remote objects are now cached in renderer process, so there is always only one reference to one remote object for each webContents.
This commit is contained in:
parent
fe7462b352
commit
2b04af4349
2 changed files with 30 additions and 45 deletions
|
@ -15,70 +15,57 @@ class ObjectsRegistry extends EventEmitter {
|
|||
this.storage = {};
|
||||
|
||||
// Stores the IDs of objects referenced by WebContents.
|
||||
// (webContentsId) => {(id) => (count)}
|
||||
// (webContentsId) => [id]
|
||||
this.owners = {};
|
||||
}
|
||||
|
||||
// Register a new object, the object would be kept referenced until you release
|
||||
// it explicitly.
|
||||
// Register a new object and return its assigned ID. If the object is already
|
||||
// registered then the already assigned ID would be returned.
|
||||
add(webContentsId, obj) {
|
||||
var base, base1, id;
|
||||
id = this.saveToStorage(obj);
|
||||
// Get or assign an ID to the object.
|
||||
let id = this.saveToStorage(obj);
|
||||
|
||||
// Remember the owner.
|
||||
if ((base = this.owners)[webContentsId] == null) {
|
||||
base[webContentsId] = {};
|
||||
// Add object to the set of referenced objects.
|
||||
let owner = this.owners[webContentsId];
|
||||
if (!owner)
|
||||
owner = this.owners[webContentsId] = new Set();
|
||||
if (!owner.has(id)) {
|
||||
owner.add(id);
|
||||
// Increase reference count if not referenced before.
|
||||
this.storage[id].count++;
|
||||
}
|
||||
if ((base1 = this.owners[webContentsId])[id] == null) {
|
||||
base1[id] = 0;
|
||||
}
|
||||
this.owners[webContentsId][id]++;
|
||||
|
||||
// Returns object's id
|
||||
return id;
|
||||
}
|
||||
|
||||
// Get an object according to its ID.
|
||||
get(id) {
|
||||
var ref;
|
||||
return (ref = this.storage[id]) != null ? ref.object : void 0;
|
||||
return this.storage[id].object;
|
||||
}
|
||||
|
||||
// Dereference an object according to its ID.
|
||||
remove(webContentsId, id) {
|
||||
var pointer;
|
||||
this.dereference(id, 1);
|
||||
// Dereference from the storage.
|
||||
this.dereference(id);
|
||||
|
||||
// Also reduce the count in owner.
|
||||
pointer = this.owners[webContentsId];
|
||||
if (pointer == null) {
|
||||
return;
|
||||
}
|
||||
--pointer[id];
|
||||
if (pointer[id] === 0) {
|
||||
return delete pointer[id];
|
||||
}
|
||||
// Also remove the reference in owner.
|
||||
this.owners[webContentsId].delete(id);
|
||||
}
|
||||
|
||||
// Clear all references to objects refrenced by the WebContents.
|
||||
clear(webContentsId) {
|
||||
var count, id, ref;
|
||||
this.emit("clear-" + webContentsId);
|
||||
if (this.owners[webContentsId] == null) {
|
||||
this.emit(`clear-${webContentsId}`);
|
||||
|
||||
let owner = this.owners[webContentsId];
|
||||
if (!owner)
|
||||
return;
|
||||
}
|
||||
ref = this.owners[webContentsId];
|
||||
for (id in ref) {
|
||||
count = ref[id];
|
||||
this.dereference(id, count);
|
||||
}
|
||||
return delete this.owners[webContentsId];
|
||||
for (let id of owner)
|
||||
this.dereference(id);
|
||||
delete this.owners[webContentsId];
|
||||
}
|
||||
|
||||
// Private: Saves the object into storage and assigns an ID for it.
|
||||
saveToStorage(object) {
|
||||
var id;
|
||||
id = v8Util.getHiddenValue(object, 'atomId');
|
||||
let id = v8Util.getHiddenValue(object, 'atomId');
|
||||
if (!id) {
|
||||
id = ++this.nextId;
|
||||
this.storage[id] = {
|
||||
|
@ -87,18 +74,16 @@ class ObjectsRegistry extends EventEmitter {
|
|||
};
|
||||
v8Util.setHiddenValue(object, 'atomId', id);
|
||||
}
|
||||
++this.storage[id].count;
|
||||
return id;
|
||||
}
|
||||
|
||||
// Private: Dereference the object from store.
|
||||
dereference(id, count) {
|
||||
var pointer;
|
||||
pointer = this.storage[id];
|
||||
dereference(id) {
|
||||
let pointer = this.storage[id];
|
||||
if (pointer == null) {
|
||||
return;
|
||||
}
|
||||
pointer.count -= count;
|
||||
pointer.count -= 1;
|
||||
if (pointer.count === 0) {
|
||||
v8Util.deleteHiddenValue(pointer.object, 'atomId');
|
||||
return delete this.storage[id];
|
||||
|
|
|
@ -145,7 +145,7 @@ var exceptionToMeta = function(error) {
|
|||
var unwrapArgs = function(sender, args) {
|
||||
var metaToValue;
|
||||
metaToValue = function(meta) {
|
||||
var i, len, member, ref, rendererReleased, returnValue;
|
||||
var i, len, member, ref, returnValue;
|
||||
switch (meta.type) {
|
||||
case 'value':
|
||||
return meta.value;
|
||||
|
|
Loading…
Reference in a new issue