2015-07-30 02:31:49 +00:00
|
|
|
savedGlobal = global # the "global.global" might be deleted later
|
2015-07-30 02:27:34 +00:00
|
|
|
|
2013-05-05 12:30:38 +00:00
|
|
|
module.exports =
|
|
|
|
class CallbacksRegistry
|
|
|
|
constructor: ->
|
2013-12-06 07:04:51 +00:00
|
|
|
@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."
|
2013-05-05 12:30:38 +00:00
|
|
|
@callbacks = {}
|
|
|
|
|
|
|
|
add: (callback) ->
|
2013-12-06 07:04:51 +00:00
|
|
|
id = Math.random().toString()
|
2015-10-16 01:36:02 +00:00
|
|
|
|
|
|
|
# Capture the location of the function and put it in the ID string,
|
|
|
|
# so that release errors can be tracked down easily.
|
|
|
|
regexp = /at (.*)/gi
|
|
|
|
stackString = (new Error).stack
|
|
|
|
|
|
|
|
while (match = regexp.exec(stackString)) isnt null
|
|
|
|
[x, location] = match
|
|
|
|
continue if location.indexOf('(native)') isnt -1
|
|
|
|
continue if location.indexOf('atom.asar') isnt -1
|
|
|
|
[x, filenameAndLine] = /([^/^\)]*)\)?$/gi.exec(location)
|
|
|
|
id = "#{filenameAndLine} (#{id})"
|
|
|
|
break
|
|
|
|
|
2013-12-06 07:04:51 +00:00
|
|
|
@callbacks[id] = callback
|
|
|
|
id
|
2013-05-05 12:30:38 +00:00
|
|
|
|
|
|
|
get: (id) ->
|
2013-12-06 07:04:51 +00:00
|
|
|
@callbacks[id] ? ->
|
2013-05-05 12:30:38 +00:00
|
|
|
|
|
|
|
call: (id, args...) ->
|
2015-07-30 02:31:49 +00:00
|
|
|
@get(id).call savedGlobal, args...
|
2013-05-05 12:30:38 +00:00
|
|
|
|
|
|
|
apply: (id, args...) ->
|
2015-07-30 02:31:49 +00:00
|
|
|
@get(id).apply savedGlobal, args...
|
2013-05-05 12:30:38 +00:00
|
|
|
|
|
|
|
remove: (id) ->
|
|
|
|
delete @callbacks[id]
|