electron/common/api/lib/callbacks-registry.coffee
Cheng Zhao 844fccc177 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.
2013-12-06 15:04:51 +08:00

25 lines
645 B
CoffeeScript

module.exports =
class CallbacksRegistry
constructor: ->
@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) ->
id = Math.random().toString()
@callbacks[id] = callback
id
get: (id) ->
@callbacks[id] ? ->
call: (id, args...) ->
@get(id).call global, args...
apply: (id, args...) ->
@get(id).apply global, args...
remove: (id) ->
delete @callbacks[id]