2015-10-28 21:29:46 +05:30
|
|
|
v8Util = process.atomBinding 'v8_util'
|
|
|
|
|
2013-05-05 20:30:38 +08:00
|
|
|
module.exports =
|
|
|
|
class CallbacksRegistry
|
|
|
|
constructor: ->
|
2015-10-16 20:25:30 +08:00
|
|
|
@nextId = 0
|
2013-05-05 20:30:38 +08:00
|
|
|
@callbacks = {}
|
|
|
|
|
|
|
|
add: (callback) ->
|
2015-10-31 15:00:06 +08:00
|
|
|
# The callback is already added.
|
|
|
|
id = v8Util.getHiddenValue callback, 'callbackId'
|
|
|
|
return id if id?
|
2015-10-29 16:52:12 +05:30
|
|
|
|
2015-10-16 20:25:30 +08:00
|
|
|
id = ++@nextId
|
2015-10-15 18:36:02 -07: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)
|
|
|
|
break
|
|
|
|
|
2013-12-06 15:04:51 +08:00
|
|
|
@callbacks[id] = callback
|
2015-10-31 15:00:06 +08:00
|
|
|
v8Util.setHiddenValue callback, 'callbackId', id
|
2015-10-28 21:29:46 +05:30
|
|
|
v8Util.setHiddenValue callback, 'location', filenameAndLine
|
2013-12-06 15:04:51 +08:00
|
|
|
id
|
2013-05-05 20:30:38 +08:00
|
|
|
|
|
|
|
get: (id) ->
|
2013-12-06 15:04:51 +08:00
|
|
|
@callbacks[id] ? ->
|
2013-05-05 20:30:38 +08:00
|
|
|
|
|
|
|
call: (id, args...) ->
|
2015-10-16 20:25:30 +08:00
|
|
|
@get(id).call global, args...
|
2013-05-05 20:30:38 +08:00
|
|
|
|
|
|
|
apply: (id, args...) ->
|
2015-10-16 20:25:30 +08:00
|
|
|
@get(id).apply global, args...
|
2013-05-05 20:30:38 +08:00
|
|
|
|
|
|
|
remove: (id) ->
|
|
|
|
delete @callbacks[id]
|