]> khamidou — GitHub mirrors - electron.git/commitdiff
Use random number as id in CallbacksRegistry.
authorCheng Zhao <zcbenz@gmail.com>
Fri, 6 Dec 2013 07:04:51 +0000 (15:04 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 6 Dec 2013 07:04:51 +0000 (15:04 +0800)
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

index 6a0120bf1d60f890f40b855645c582817325d089..b549d17c7285c9b7fa983aa07b40b7d66f45e337 100644 (file)
@@ -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...