electron/lib/common/api/deprecate.js

109 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-01-14 18:35:29 +00:00
// Deprecate a method.
2016-03-18 18:51:02 +00:00
const deprecate = function(oldName, newName, fn) {
2016-01-12 02:40:23 +00:00
var warned;
warned = false;
return function() {
if (!(warned || process.noDeprecation)) {
warned = true;
deprecate.warn(oldName, newName);
}
return fn.apply(this, arguments);
};
};
2016-01-14 18:35:29 +00:00
// The method is renamed.
2016-01-12 02:40:23 +00:00
deprecate.rename = function(object, oldName, newName) {
var newMethod, warned;
warned = false;
newMethod = function() {
if (!(warned || process.noDeprecation)) {
warned = true;
deprecate.warn(oldName, newName);
}
return this[newName].apply(this, arguments);
};
if (typeof object === 'function') {
return object.prototype[oldName] = newMethod;
} else {
return object[oldName] = newMethod;
}
};
2016-01-14 18:35:29 +00:00
// Forward the method to member.
2016-01-12 02:40:23 +00:00
deprecate.member = function(object, method, member) {
var warned;
warned = false;
return object.prototype[method] = function() {
if (!(warned || process.noDeprecation)) {
warned = true;
deprecate.warn(method, member + "." + method);
}
return this[member][method].apply(this[member], arguments);
};
};
2016-01-14 18:35:29 +00:00
// Deprecate a property.
2016-01-12 02:40:23 +00:00
deprecate.property = function(object, property, method) {
return Object.defineProperty(object, property, {
get: function() {
var warned;
warned = false;
if (!(warned || process.noDeprecation)) {
warned = true;
deprecate.warn(property + " property", method + " method");
}
return this[method]();
}
});
};
2016-01-14 18:35:29 +00:00
// Deprecate an event.
2016-01-12 02:40:23 +00:00
deprecate.event = function(emitter, oldName, newName, fn) {
var warned;
warned = false;
2016-03-18 18:51:02 +00:00
return emitter.on(newName, function(...args) {
2016-01-14 18:35:29 +00:00
// there is listeners for old API.
2016-01-12 02:40:23 +00:00
if (this.listenerCount(oldName) > 0) {
if (!(warned || process.noDeprecation)) {
warned = true;
deprecate.warn("'" + oldName + "' event", "'" + newName + "' event");
}
if (fn != null) {
return fn.apply(this, arguments);
} else {
2016-03-18 18:51:02 +00:00
return this.emit.apply(this, [oldName].concat(args));
2016-01-12 02:40:23 +00:00
}
}
});
};
2016-01-14 18:35:29 +00:00
// Print deprecation warning.
2016-01-12 02:40:23 +00:00
deprecate.warn = function(oldName, newName) {
return deprecate.log(oldName + " is deprecated. Use " + newName + " instead.");
};
var deprecationHandler = null;
2016-01-14 18:35:29 +00:00
// Print deprecation message.
2016-01-12 02:40:23 +00:00
deprecate.log = function(message) {
if (typeof deprecationHandler === 'function') {
deprecationHandler(message);
} else if (process.throwDeprecation) {
2016-01-12 02:40:23 +00:00
throw new Error(message);
} else if (process.traceDeprecation) {
return console.trace(message);
} else {
return console.warn("(electron) " + message);
}
};
deprecate.setHandler = function(handler) {
deprecationHandler = handler;
};
deprecate.getHandler = function() {
return deprecationHandler;
2016-02-16 23:26:58 +00:00
};
2016-01-12 02:40:23 +00:00
module.exports = deprecate;