first pass at standardizing; suite still passing!

This commit is contained in:
Zeke Sikelianos 2016-03-24 13:15:04 -07:00 committed by Kevin Sawicki
parent f25c3d33b6
commit 4794385fac
30 changed files with 1454 additions and 1462 deletions

View file

@ -1,94 +1,94 @@
'use strict';
'use strict'
const v8Util = process.atomBinding('v8_util');
const v8Util = process.atomBinding('v8_util')
class ObjectsRegistry {
constructor() {
this.nextId = 0;
constructor () {
this.nextId = 0
// Stores all objects by ref-counting.
// (id) => {object, count}
this.storage = {};
this.storage = {}
// Stores the IDs of objects referenced by WebContents.
// (webContentsId) => [id]
this.owners = {};
this.owners = {}
}
// Register a new object and return its assigned ID. If the object is already
// registered then the already assigned ID would be returned.
add(webContents, obj) {
add (webContents, obj) {
// Get or assign an ID to the object.
let id = this.saveToStorage(obj);
let id = this.saveToStorage(obj)
// Add object to the set of referenced objects.
let webContentsId = webContents.getId();
let owner = this.owners[webContentsId];
let webContentsId = webContents.getId()
let owner = this.owners[webContentsId]
if (!owner) {
owner = this.owners[webContentsId] = new Set();
owner = this.owners[webContentsId] = new Set()
// Clear the storage when webContents is reloaded/navigated.
webContents.once('render-view-deleted', (event, id) => {
this.clear(id);
});
this.clear(id)
})
}
if (!owner.has(id)) {
owner.add(id);
owner.add(id)
// Increase reference count if not referenced before.
this.storage[id].count++;
this.storage[id].count++
}
return id;
return id
}
// Get an object according to its ID.
get(id) {
return this.storage[id].object;
get (id) {
return this.storage[id].object
}
// Dereference an object according to its ID.
remove(webContentsId, id) {
remove (webContentsId, id) {
// Dereference from the storage.
this.dereference(id);
this.dereference(id)
// Also remove the reference in owner.
this.owners[webContentsId].delete(id);
this.owners[webContentsId].delete(id)
}
// Clear all references to objects refrenced by the WebContents.
clear(webContentsId) {
let owner = this.owners[webContentsId];
if (!owner)
return;
for (let id of owner)
this.dereference(id);
delete this.owners[webContentsId];
clear (webContentsId) {
let owner = this.owners[webContentsId]
if (!owner) return
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) {
let id = v8Util.getHiddenValue(object, 'atomId');
saveToStorage (object) {
let id = v8Util.getHiddenValue(object, 'atomId')
if (!id) {
id = ++this.nextId;
id = ++this.nextId
this.storage[id] = {
count: 0,
object: object
};
v8Util.setHiddenValue(object, 'atomId', id);
}
v8Util.setHiddenValue(object, 'atomId', id)
}
return id;
return id
}
// Private: Dereference the object from store.
dereference(id) {
let pointer = this.storage[id];
dereference (id) {
let pointer = this.storage[id]
if (pointer == null) {
return;
return
}
pointer.count -= 1;
pointer.count -= 1
if (pointer.count === 0) {
v8Util.deleteHiddenValue(pointer.object, 'atomId');
return delete this.storage[id];
v8Util.deleteHiddenValue(pointer.object, 'atomId')
return delete this.storage[id]
}
}
}
module.exports = new ObjectsRegistry;
module.exports = new ObjectsRegistry()