2016-01-15 22:31:23 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-01-15 17:32:57 +00:00
|
|
|
var slice = [].slice;
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-15 17:32:57 +00:00
|
|
|
const v8Util = process.atomBinding('v8_util');
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-15 22:31:23 +00:00
|
|
|
class CallbacksRegistry {
|
|
|
|
constructor() {
|
2016-01-12 02:40:23 +00:00
|
|
|
this.nextId = 0;
|
|
|
|
this.callbacks = {};
|
|
|
|
}
|
|
|
|
|
2016-01-15 22:31:23 +00:00
|
|
|
add(callback) {
|
2016-01-14 18:35:29 +00:00
|
|
|
// The callback is already added.
|
2016-01-19 22:49:40 +00:00
|
|
|
var filenameAndLine, id, location, match, ref, regexp, stackString;
|
2016-01-12 02:40:23 +00:00
|
|
|
id = v8Util.getHiddenValue(callback, 'callbackId');
|
|
|
|
if (id != null) {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
id = ++this.nextId;
|
|
|
|
|
2016-01-14 18:44:21 +00:00
|
|
|
// Capture the location of the function and put it in the ID string,
|
|
|
|
// so that release errors can be tracked down easily.
|
2016-01-12 02:40:23 +00:00
|
|
|
regexp = /at (.*)/gi;
|
|
|
|
stackString = (new Error).stack;
|
|
|
|
while ((match = regexp.exec(stackString)) !== null) {
|
2016-01-19 22:49:40 +00:00
|
|
|
location = match[1];
|
2016-01-12 02:40:23 +00:00
|
|
|
if (location.indexOf('(native)') !== -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (location.indexOf('atom.asar') !== -1) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-01-19 22:49:40 +00:00
|
|
|
ref = /([^\/^\)]*)\)?$/gi.exec(location);
|
|
|
|
filenameAndLine = ref[1];
|
2016-01-12 02:40:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
this.callbacks[id] = callback;
|
|
|
|
v8Util.setHiddenValue(callback, 'callbackId', id);
|
|
|
|
v8Util.setHiddenValue(callback, 'location', filenameAndLine);
|
|
|
|
return id;
|
2016-01-15 22:31:23 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-15 22:31:23 +00:00
|
|
|
get(id) {
|
2016-01-12 02:40:23 +00:00
|
|
|
var ref;
|
|
|
|
return (ref = this.callbacks[id]) != null ? ref : function() {};
|
2016-01-15 22:31:23 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-15 22:31:23 +00:00
|
|
|
call() {
|
2016-01-12 02:40:23 +00:00
|
|
|
var args, id, ref;
|
|
|
|
id = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
|
|
|
|
return (ref = this.get(id)).call.apply(ref, [global].concat(slice.call(args)));
|
2016-01-15 22:31:23 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-15 22:31:23 +00:00
|
|
|
apply() {
|
2016-01-12 02:40:23 +00:00
|
|
|
var args, id, ref;
|
|
|
|
id = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
|
|
|
|
return (ref = this.get(id)).apply.apply(ref, [global].concat(slice.call(args)));
|
2016-01-15 22:31:23 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-15 22:31:23 +00:00
|
|
|
remove(id) {
|
2016-01-12 02:40:23 +00:00
|
|
|
return delete this.callbacks[id];
|
2016-01-15 22:31:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-16 06:26:01 +00:00
|
|
|
module.exports = CallbacksRegistry
|