From: Cheng Zhao Date: Fri, 6 Dec 2013 07:04:51 +0000 (+0800) Subject: Use random number as id in CallbacksRegistry. X-Git-Tag: v0.7.6~4^2~1 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=844fccc177791dc206e247bacc14604e2a1a415c;p=electron.git Use random number as id in CallbacksRegistry. It's very possible that the callbacks got GCed before the render view is closed (like page getting refreshed), so we should not let browser call the wrong callback, instead we should throw error whenever a callback is not found. --- diff --git a/common/api/lib/callbacks-registry.coffee b/common/api/lib/callbacks-registry.coffee index 6a0120bf..b549d17c 100644 --- a/common/api/lib/callbacks-registry.coffee +++ b/common/api/lib/callbacks-registry.coffee @@ -1,15 +1,19 @@ module.exports = class CallbacksRegistry constructor: -> - @nextId = 0 + @emptyFunc = -> throw new Error "Browser trying to call a non-exist callback + in renderer, this usually happens when renderer code forgot to release + a callback installed on objects in browser when renderer was going to be + unloaded or released." @callbacks = {} add: (callback) -> - @callbacks[++@nextId] = callback - @nextId + id = Math.random().toString() + @callbacks[id] = callback + id get: (id) -> - @callbacks[id] + @callbacks[id] ? -> call: (id, args...) -> @get(id).call global, args...