Use ES6 style class
This commit is contained in:
parent
02f055b784
commit
aab1568682
1 changed files with 100 additions and 102 deletions
|
@ -1,9 +1,10 @@
|
||||||
const EventEmitter = require('events').EventEmitter;
|
'use strict';
|
||||||
const util = require('util');
|
|
||||||
const v8Util = process.atomBinding('v8_util');
|
|
||||||
|
|
||||||
function ObjectsRegistry() {
|
const EventEmitter = require('events').EventEmitter;
|
||||||
ObjectsRegistry.super_.call(this);
|
|
||||||
|
class ObjectsRegistry extends EventEmitter {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
|
||||||
this.setMaxListeners(Number.MAX_VALUE);
|
this.setMaxListeners(Number.MAX_VALUE);
|
||||||
this.nextId = 0;
|
this.nextId = 0;
|
||||||
|
@ -17,11 +18,9 @@ function ObjectsRegistry() {
|
||||||
this.owners = {};
|
this.owners = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
util.inherits(ObjectsRegistry, EventEmitter);
|
|
||||||
|
|
||||||
// Register a new object, the object would be kept referenced until you release
|
// Register a new object, the object would be kept referenced until you release
|
||||||
// it explicitly.
|
// it explicitly.
|
||||||
ObjectsRegistry.prototype.add = function(webContentsId, obj) {
|
add(webContentsId, obj) {
|
||||||
var base, base1, id;
|
var base, base1, id;
|
||||||
id = this.saveToStorage(obj);
|
id = this.saveToStorage(obj);
|
||||||
|
|
||||||
|
@ -36,18 +35,16 @@ ObjectsRegistry.prototype.add = function(webContentsId, obj) {
|
||||||
|
|
||||||
// Returns object's id
|
// Returns object's id
|
||||||
return id;
|
return id;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
// Get an object according to its ID.
|
// Get an object according to its ID.
|
||||||
ObjectsRegistry.prototype.get = function(id) {
|
get(id) {
|
||||||
var ref;
|
var ref;
|
||||||
return (ref = this.storage[id]) != null ? ref.object : void 0;
|
return (ref = this.storage[id]) != null ? ref.object : void 0;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
// Dereference an object according to its ID.
|
// Dereference an object according to its ID.
|
||||||
ObjectsRegistry.prototype.remove = function(webContentsId, id) {
|
remove(webContentsId, id) {
|
||||||
var pointer;
|
var pointer;
|
||||||
this.dereference(id, 1);
|
this.dereference(id, 1);
|
||||||
|
|
||||||
|
@ -60,10 +57,10 @@ ObjectsRegistry.prototype.remove = function(webContentsId, id) {
|
||||||
if (pointer[id] === 0) {
|
if (pointer[id] === 0) {
|
||||||
return delete pointer[id];
|
return delete pointer[id];
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
// Clear all references to objects refrenced by the WebContents.
|
// Clear all references to objects refrenced by the WebContents.
|
||||||
ObjectsRegistry.prototype.clear = function(webContentsId) {
|
clear(webContentsId) {
|
||||||
var count, id, ref;
|
var count, id, ref;
|
||||||
this.emit("clear-" + webContentsId);
|
this.emit("clear-" + webContentsId);
|
||||||
if (this.owners[webContentsId] == null) {
|
if (this.owners[webContentsId] == null) {
|
||||||
|
@ -75,10 +72,10 @@ ObjectsRegistry.prototype.clear = function(webContentsId) {
|
||||||
this.dereference(id, count);
|
this.dereference(id, count);
|
||||||
}
|
}
|
||||||
return delete this.owners[webContentsId];
|
return delete this.owners[webContentsId];
|
||||||
};
|
}
|
||||||
|
|
||||||
// Private: Saves the object into storage and assigns an ID for it.
|
// Private: Saves the object into storage and assigns an ID for it.
|
||||||
ObjectsRegistry.prototype.saveToStorage = function(object) {
|
saveToStorage(object) {
|
||||||
var id;
|
var id;
|
||||||
id = v8Util.getHiddenValue(object, 'atomId');
|
id = v8Util.getHiddenValue(object, 'atomId');
|
||||||
if (!id) {
|
if (!id) {
|
||||||
|
@ -91,10 +88,10 @@ ObjectsRegistry.prototype.saveToStorage = function(object) {
|
||||||
}
|
}
|
||||||
++this.storage[id].count;
|
++this.storage[id].count;
|
||||||
return id;
|
return id;
|
||||||
};
|
}
|
||||||
|
|
||||||
// Private: Dereference the object from store.
|
// Private: Dereference the object from store.
|
||||||
ObjectsRegistry.prototype.dereference = function(id, count) {
|
dereference(id, count) {
|
||||||
var pointer;
|
var pointer;
|
||||||
pointer = this.storage[id];
|
pointer = this.storage[id];
|
||||||
if (pointer == null) {
|
if (pointer == null) {
|
||||||
|
@ -105,6 +102,7 @@ ObjectsRegistry.prototype.dereference = function(id, count) {
|
||||||
v8Util.deleteHiddenValue(pointer.object, 'atomId');
|
v8Util.deleteHiddenValue(pointer.object, 'atomId');
|
||||||
return delete this.storage[id];
|
return delete this.storage[id];
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = new ObjectsRegistry;
|
module.exports = new ObjectsRegistry;
|
||||||
|
|
Loading…
Add table
Reference in a new issue