2015-10-28 15:59:46 +00:00
|
|
|
v8Util = process.atomBinding 'v8_util'
|
|
|
|
|
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) ->
|
2016-01-12 02:03:02 +00:00
|
|
|
### The callback is already added. ###
|
2015-10-31 07:00:06 +00:00
|
|
|
id = v8Util.getHiddenValue callback, 'callbackId'
|
|
|
|
return id if id?
|
2015-10-29 11:22:12 +00:00
|
|
|
|
2015-10-16 12:25:30 +00:00
|
|
|
id = ++@nextId
|
2015-10-16 01:36:02 +00:00
|
|
|
|
2016-01-12 02:03: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.
|
|
|
|
###
|
2015-10-16 01:36:02 +00:00
|
|
|
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 07:04:51 +00:00
|
|
|
@callbacks[id] = callback
|
2015-10-31 07:00:06 +00:00
|
|
|
v8Util.setHiddenValue callback, 'callbackId', id
|
2015-10-28 15:59:46 +00:00
|
|
|
v8Util.setHiddenValue callback, 'location', filenameAndLine
|
2013-12-06 07:04:51 +00:00
|
|
|
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]
|