2013-05-05 12:30:38 +00:00
|
|
|
module.exports =
|
|
|
|
class CallbacksRegistry
|
|
|
|
constructor: ->
|
2015-10-16 12:25:30 +00:00
|
|
|
@nextId = 0
|
2013-05-05 12:30:38 +00:00
|
|
|
@callbacks = {}
|
|
|
|
|
|
|
|
add: (callback) ->
|
2015-10-16 12:25:30 +00:00
|
|
|
id = ++@nextId
|
2015-10-16 01:36:02 +00:00
|
|
|
|
2015-10-27 22:15:46 +00:00
|
|
|
for id,value of @callbacks
|
|
|
|
if value == callback
|
|
|
|
return id
|
|
|
|
|
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-10-16 12:25:30 +00:00
|
|
|
@get(id).call global, args...
|
2013-05-05 12:30:38 +00:00
|
|
|
|
|
|
|
apply: (id, args...) ->
|
2015-10-16 12:25:30 +00:00
|
|
|
@get(id).apply global, args...
|
2013-05-05 12:30:38 +00:00
|
|
|
|
|
|
|
remove: (id) ->
|
|
|
|
delete @callbacks[id]
|