electron/atom/common/api/lib/callbacks-registry.js

66 lines
2 KiB
JavaScript
Raw Normal View History

2016-01-12 02:40:23 +00:00
var CallbacksRegistry, v8Util,
slice = [].slice;
v8Util = process.atomBinding('v8_util');
module.exports = CallbacksRegistry = (function() {
function CallbacksRegistry() {
this.nextId = 0;
this.callbacks = {};
}
CallbacksRegistry.prototype.add = function(callback) {
2016-01-14 18:35:29 +00:00
// The callback is already added.
2016-01-12 02:40:23 +00:00
var filenameAndLine, id, location, match, ref, regexp, stackString, x;
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) {
x = match[0], location = match[1];
if (location.indexOf('(native)') !== -1) {
continue;
}
if (location.indexOf('atom.asar') !== -1) {
continue;
}
ref = /([^\/^\)]*)\)?$/gi.exec(location), x = ref[0], filenameAndLine = ref[1];
break;
}
this.callbacks[id] = callback;
v8Util.setHiddenValue(callback, 'callbackId', id);
v8Util.setHiddenValue(callback, 'location', filenameAndLine);
return id;
};
CallbacksRegistry.prototype.get = function(id) {
var ref;
return (ref = this.callbacks[id]) != null ? ref : function() {};
};
CallbacksRegistry.prototype.call = function() {
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)));
};
CallbacksRegistry.prototype.apply = function() {
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)));
};
CallbacksRegistry.prototype.remove = function(id) {
return delete this.callbacks[id];
};
return CallbacksRegistry;
})();