Clean up the code handling renderer callback
This commit is contained in:
parent
323ab92299
commit
cb91d4487b
4 changed files with 24 additions and 33 deletions
|
@ -4,9 +4,6 @@ objectsRegistry = require './objects-registry.js'
|
||||||
v8Util = process.atomBinding 'v8_util'
|
v8Util = process.atomBinding 'v8_util'
|
||||||
IDWeakMap = process.atomBinding('id_weak_map').IDWeakMap
|
IDWeakMap = process.atomBinding('id_weak_map').IDWeakMap
|
||||||
|
|
||||||
# Object mapping from webcontents id to their renderer callbacks weakmap.
|
|
||||||
rendererRegistry = {}
|
|
||||||
|
|
||||||
# Convert a real value into meta data.
|
# Convert a real value into meta data.
|
||||||
valueToMeta = (sender, value, optimizeSimpleObject=false) ->
|
valueToMeta = (sender, value, optimizeSimpleObject=false) ->
|
||||||
meta = type: typeof value
|
meta = type: typeof value
|
||||||
|
@ -74,18 +71,15 @@ unwrapArgs = (sender, args) ->
|
||||||
returnValue = metaToValue meta.value
|
returnValue = metaToValue meta.value
|
||||||
-> returnValue
|
-> returnValue
|
||||||
when 'function'
|
when 'function'
|
||||||
webContentsId = sender.getId()
|
# Cache the callbacks in renderer.
|
||||||
rendererCallbacks = rendererRegistry[webContentsId]
|
unless sender.callbacks
|
||||||
if not rendererCallbacks?
|
sender.callbacks = new IDWeakMap
|
||||||
# Weak reference to callbacks with their ID
|
sender.on 'render-view-deleted', ->
|
||||||
rendererCallbacks = new IDWeakMap()
|
sender.callbacks.clear()
|
||||||
rendererRegistry[webContentsId] = rendererCallbacks
|
return sender.callbacks.get meta.id if sender.callbacks.has meta.id
|
||||||
|
|
||||||
if rendererCallbacks.has(meta.id)
|
|
||||||
return rendererCallbacks.get(meta.id)
|
|
||||||
|
|
||||||
rendererReleased = false
|
rendererReleased = false
|
||||||
objectsRegistry.once "clear-#{webContentsId}", ->
|
objectsRegistry.once "clear-#{sender.getId()}", ->
|
||||||
rendererReleased = true
|
rendererReleased = true
|
||||||
|
|
||||||
ret = ->
|
ret = ->
|
||||||
|
@ -95,9 +89,9 @@ unwrapArgs = (sender, args) ->
|
||||||
sender.send 'ATOM_RENDERER_CALLBACK', meta.id, valueToMeta(sender, arguments)
|
sender.send 'ATOM_RENDERER_CALLBACK', meta.id, valueToMeta(sender, arguments)
|
||||||
v8Util.setDestructor ret, ->
|
v8Util.setDestructor ret, ->
|
||||||
return if rendererReleased
|
return if rendererReleased
|
||||||
rendererCallbacks.remove meta.id
|
sender.callbacks.remove meta.id
|
||||||
sender.send 'ATOM_RENDERER_RELEASE_CALLBACK', meta.id
|
sender.send 'ATOM_RENDERER_RELEASE_CALLBACK', meta.id
|
||||||
rendererCallbacks.set meta.id, ret
|
sender.callbacks.set meta.id, ret
|
||||||
ret
|
ret
|
||||||
else throw new TypeError("Unknown type: #{meta.type}")
|
else throw new TypeError("Unknown type: #{meta.type}")
|
||||||
|
|
||||||
|
@ -116,8 +110,6 @@ callFunction = (event, func, caller, args) ->
|
||||||
|
|
||||||
# Send by BrowserWindow when its render view is deleted.
|
# Send by BrowserWindow when its render view is deleted.
|
||||||
process.on 'ATOM_BROWSER_RELEASE_RENDER_VIEW', (id) ->
|
process.on 'ATOM_BROWSER_RELEASE_RENDER_VIEW', (id) ->
|
||||||
if rendererRegistry.id?
|
|
||||||
delete rendererRegistry.id
|
|
||||||
objectsRegistry.clear id
|
objectsRegistry.clear id
|
||||||
|
|
||||||
ipc.on 'ATOM_BROWSER_REQUIRE', (event, module) ->
|
ipc.on 'ATOM_BROWSER_REQUIRE', (event, module) ->
|
||||||
|
|
|
@ -12,33 +12,32 @@ namespace atom {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
IDWeakMap::IDWeakMap() : id_weak_map_(new atom::IDWeakMap) {
|
IDWeakMap::IDWeakMap() {
|
||||||
}
|
}
|
||||||
|
|
||||||
IDWeakMap::~IDWeakMap() {
|
IDWeakMap::~IDWeakMap() {
|
||||||
id_weak_map_ = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void IDWeakMap::Set(v8::Isolate* isolate,
|
void IDWeakMap::Set(v8::Isolate* isolate,
|
||||||
int32_t id,
|
int32_t id,
|
||||||
v8::Local<v8::Object> object) {
|
v8::Local<v8::Object> object) {
|
||||||
id_weak_map_->Set(isolate, id, object);
|
id_weak_map_.Set(isolate, id, object);
|
||||||
}
|
}
|
||||||
|
|
||||||
v8::Local<v8::Object> IDWeakMap::Get(v8::Isolate* isolate, int32_t id) {
|
v8::Local<v8::Object> IDWeakMap::Get(v8::Isolate* isolate, int32_t id) {
|
||||||
return id_weak_map_->Get(isolate, id).ToLocalChecked();
|
return id_weak_map_.Get(isolate, id).ToLocalChecked();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IDWeakMap::Has(int32_t id) {
|
bool IDWeakMap::Has(int32_t id) {
|
||||||
return id_weak_map_->Has(id);
|
return id_weak_map_.Has(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IDWeakMap::Remove(int32_t id) {
|
void IDWeakMap::Remove(int32_t id) {
|
||||||
id_weak_map_->Remove(id);
|
id_weak_map_.Remove(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IDWeakMap::IsDestroyed() const {
|
void IDWeakMap::Clear() {
|
||||||
return !id_weak_map_;
|
id_weak_map_.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
|
@ -48,7 +47,8 @@ void IDWeakMap::BuildPrototype(v8::Isolate* isolate,
|
||||||
.SetMethod("set", &IDWeakMap::Set)
|
.SetMethod("set", &IDWeakMap::Set)
|
||||||
.SetMethod("get", &IDWeakMap::Get)
|
.SetMethod("get", &IDWeakMap::Get)
|
||||||
.SetMethod("has", &IDWeakMap::Has)
|
.SetMethod("has", &IDWeakMap::Has)
|
||||||
.SetMethod("remove", &IDWeakMap::Remove);
|
.SetMethod("remove", &IDWeakMap::Remove)
|
||||||
|
.SetMethod("clear", &IDWeakMap::Clear);
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
|
|
|
@ -24,17 +24,15 @@ class IDWeakMap : public mate::Wrappable {
|
||||||
IDWeakMap();
|
IDWeakMap();
|
||||||
~IDWeakMap();
|
~IDWeakMap();
|
||||||
|
|
||||||
// mate::Wrappable:
|
|
||||||
bool IsDestroyed() const override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Api for IDWeakMap.
|
// Api for IDWeakMap.
|
||||||
void Set(v8::Isolate* isolate, int32_t id, v8::Local<v8::Object> object);
|
void Set(v8::Isolate* isolate, int32_t id, v8::Local<v8::Object> object);
|
||||||
v8::Local<v8::Object> Get(v8::Isolate* isolate, int32_t id);
|
v8::Local<v8::Object> Get(v8::Isolate* isolate, int32_t id);
|
||||||
bool Has(int32_t id);
|
bool Has(int32_t id);
|
||||||
void Remove(int32_t id);
|
void Remove(int32_t id);
|
||||||
|
void Clear();
|
||||||
|
|
||||||
atom::IDWeakMap* id_weak_map_;
|
atom::IDWeakMap id_weak_map_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(IDWeakMap);
|
DISALLOW_COPY_AND_ASSIGN(IDWeakMap);
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,8 +7,9 @@ class CallbacksRegistry
|
||||||
@callbacks = {}
|
@callbacks = {}
|
||||||
|
|
||||||
add: (callback) ->
|
add: (callback) ->
|
||||||
if v8Util.getHiddenValue(callback, 'metaId')?
|
# The callback is already added.
|
||||||
return v8Util.getHiddenValue(callback, 'metaId')
|
id = v8Util.getHiddenValue callback, 'callbackId'
|
||||||
|
return id if id?
|
||||||
|
|
||||||
id = ++@nextId
|
id = ++@nextId
|
||||||
|
|
||||||
|
@ -25,7 +26,7 @@ class CallbacksRegistry
|
||||||
break
|
break
|
||||||
|
|
||||||
@callbacks[id] = callback
|
@callbacks[id] = callback
|
||||||
v8Util.setHiddenValue callback, 'metaId', id
|
v8Util.setHiddenValue callback, 'callbackId', id
|
||||||
v8Util.setHiddenValue callback, 'location', filenameAndLine
|
v8Util.setHiddenValue callback, 'location', filenameAndLine
|
||||||
id
|
id
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue