From 844fccc177791dc206e247bacc14604e2a1a415c Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 6 Dec 2013 15:04:51 +0800 Subject: [PATCH] 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. --- common/api/lib/callbacks-registry.coffee | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/common/api/lib/callbacks-registry.coffee b/common/api/lib/callbacks-registry.coffee index 6a0120bf1d60..b549d17c7285 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...