2022-11-16 17:30:20 +08:00
|
|
|
const handlers = [];
|
|
|
|
const disabledDeprecations = new Set();
|
2023-12-13 22:06:59 +08:00
|
|
|
const deprecationWorkflow = window.deprecationWorkflow;
|
|
|
|
const workflows = deprecationWorkflow?.config?.workflow;
|
2022-11-16 17:30:20 +08:00
|
|
|
|
2023-10-30 20:09:45 +08:00
|
|
|
let emberDeprecationSilencer;
|
|
|
|
|
2022-11-16 17:30:20 +08:00
|
|
|
/**
|
|
|
|
* Display a deprecation warning with the provided message. The warning will be prefixed with the theme/plugin name
|
|
|
|
* if it can be automatically determined based on the current stack.
|
|
|
|
* @param {String} msg The deprecation message
|
|
|
|
* @param {Object} [options] Deprecation options
|
|
|
|
* @param {String} [options.id] A unique identifier for this deprecation. This should be namespaced by dots (e.g. discourse.my_deprecation)
|
|
|
|
* @param {String} [options.since] The Discourse version this deprecation was introduced in
|
|
|
|
* @param {String} [options.dropFrom] The Discourse version this deprecation will be dropped in. Typically one major version after `since`
|
|
|
|
* @param {String} [options.url] A URL which provides more detail about the deprecation
|
|
|
|
* @param {boolean} [options.raiseError] Raise an error when this deprecation is triggered. Defaults to `false`
|
|
|
|
*/
|
|
|
|
export default function deprecated(msg, options = {}) {
|
|
|
|
const { id, since, dropFrom, url, raiseError } = options;
|
|
|
|
|
|
|
|
if (id && disabledDeprecations.has(id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-06 19:38:01 +08:00
|
|
|
msg = ["Deprecation notice:", msg];
|
2022-11-16 17:30:20 +08:00
|
|
|
if (since) {
|
|
|
|
msg.push(`[deprecated since Discourse ${since}]`);
|
2018-12-06 19:38:01 +08:00
|
|
|
}
|
2022-11-16 17:30:20 +08:00
|
|
|
if (dropFrom) {
|
|
|
|
msg.push(`[removal in Discourse ${dropFrom}]`);
|
2018-12-06 19:38:01 +08:00
|
|
|
}
|
2022-11-16 17:30:20 +08:00
|
|
|
if (id) {
|
|
|
|
msg.push(`[deprecation id: ${id}]`);
|
2018-12-06 19:38:01 +08:00
|
|
|
}
|
2022-11-16 17:30:20 +08:00
|
|
|
if (url) {
|
|
|
|
msg.push(`[info: ${url}]`);
|
|
|
|
}
|
|
|
|
msg = msg.join(" ");
|
2022-02-11 17:17:24 +08:00
|
|
|
|
|
|
|
let consolePrefix = "";
|
2023-10-31 18:56:11 +08:00
|
|
|
if (require.has("discourse/lib/source-identifier")) {
|
2022-02-11 17:17:24 +08:00
|
|
|
// This module doesn't exist in pretty-text/wizard/etc.
|
|
|
|
consolePrefix =
|
|
|
|
require("discourse/lib/source-identifier").consolePrefix() || "";
|
|
|
|
}
|
|
|
|
|
2022-11-16 17:30:20 +08:00
|
|
|
handlers.forEach((h) => h(msg, options));
|
|
|
|
|
2023-12-13 22:06:59 +08:00
|
|
|
const matchedWorkflow = workflows?.find((w) => w.matchId === id);
|
|
|
|
|
|
|
|
if (
|
|
|
|
raiseError ||
|
|
|
|
matchedWorkflow?.handler === "throw" ||
|
2023-12-29 00:35:06 +08:00
|
|
|
(!matchedWorkflow && deprecationWorkflow?.throwOnUnhandled)
|
2023-12-13 22:06:59 +08:00
|
|
|
) {
|
2022-11-16 17:30:20 +08:00
|
|
|
throw msg;
|
|
|
|
}
|
|
|
|
|
2023-03-10 18:39:42 +08:00
|
|
|
if (matchedWorkflow?.handler !== "silence") {
|
|
|
|
console.warn(...[consolePrefix, msg].filter(Boolean)); //eslint-disable-line no-console
|
|
|
|
}
|
2022-11-16 17:30:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a function which will be called whenever a deprecation is triggered
|
|
|
|
* @param {function} callback The callback function. Arguments will match those of `deprecated()`.
|
|
|
|
*/
|
|
|
|
export function registerDeprecationHandler(callback) {
|
|
|
|
handlers.push(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Silence one or more deprecations while running `callback`
|
2022-11-17 01:55:20 +08:00
|
|
|
* @param {(string|string[])} deprecationIds A single id, or an array of ids, of deprecations to silence
|
|
|
|
* @param {function} callback The function to call while deprecations are silenced.
|
|
|
|
*/
|
|
|
|
export function withSilencedDeprecations(deprecationIds, callback) {
|
2023-10-30 20:09:45 +08:00
|
|
|
ensureEmberDeprecationSilencer();
|
2022-11-17 01:55:20 +08:00
|
|
|
const idArray = [].concat(deprecationIds);
|
|
|
|
try {
|
|
|
|
idArray.forEach((id) => disabledDeprecations.add(id));
|
|
|
|
const result = callback();
|
|
|
|
if (result instanceof Promise) {
|
|
|
|
throw new Error(
|
|
|
|
"withSilencedDeprecations callback returned a promise. Use withSilencedDeprecationsAsync instead."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} finally {
|
|
|
|
idArray.forEach((id) => disabledDeprecations.delete(id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Silence one or more deprecations while running an async `callback`
|
2022-11-16 17:30:20 +08:00
|
|
|
* @async
|
|
|
|
* @param {(string|string[])} deprecationIds A single id, or an array of ids, of deprecations to silence
|
2022-11-17 01:55:20 +08:00
|
|
|
* @param {function} callback The asynchronous function to call while deprecations are silenced.
|
2022-11-16 17:30:20 +08:00
|
|
|
*/
|
2022-11-17 01:55:20 +08:00
|
|
|
export async function withSilencedDeprecationsAsync(deprecationIds, callback) {
|
2023-10-30 20:09:45 +08:00
|
|
|
ensureEmberDeprecationSilencer();
|
2022-11-16 17:30:20 +08:00
|
|
|
const idArray = [].concat(deprecationIds);
|
|
|
|
try {
|
|
|
|
idArray.forEach((id) => disabledDeprecations.add(id));
|
2022-11-17 01:55:20 +08:00
|
|
|
return await callback();
|
2022-11-16 17:30:20 +08:00
|
|
|
} finally {
|
|
|
|
idArray.forEach((id) => disabledDeprecations.delete(id));
|
|
|
|
}
|
2016-11-04 23:32:12 +08:00
|
|
|
}
|
2023-10-30 20:09:45 +08:00
|
|
|
|
|
|
|
function ensureEmberDeprecationSilencer() {
|
|
|
|
if (emberDeprecationSilencer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emberDeprecationSilencer = (message, options, next) => {
|
|
|
|
if (options?.id && disabledDeprecations.has(options.id)) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
next(message, options);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (require.has("@ember/debug")) {
|
|
|
|
require("@ember/debug").registerDeprecationHandler(
|
|
|
|
emberDeprecationSilencer
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|