DEV: Add `withSilencedDeprecationsAsync` for async functions (#19057)

Previously we were trying to handle both async and sync use cases in a single function, but it was confusing to read and led to subtle race conditions. This commit separates the async version into a separate function.
This commit is contained in:
David Taylor 2022-11-16 17:55:20 +00:00 committed by GitHub
parent ce7172bc9b
commit 6d6d5a200f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 9 deletions

View File

@ -60,18 +60,37 @@ export function registerDeprecationHandler(callback) {
/**
* Silence one or more deprecations while running `callback`
* @async
* @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. Can be asynchronous.
* @param {function} callback The function to call while deprecations are silenced.
*/
export async function withSilencedDeprecations(deprecationIds, callback) {
export function withSilencedDeprecations(deprecationIds, callback) {
const idArray = [].concat(deprecationIds);
let result;
try {
idArray.forEach((id) => disabledDeprecations.add(id));
result = callback();
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`
* @async
* @param {(string|string[])} deprecationIds A single id, or an array of ids, of deprecations to silence
* @param {function} callback The asynchronous function to call while deprecations are silenced.
*/
export async function withSilencedDeprecationsAsync(deprecationIds, callback) {
const idArray = [].concat(deprecationIds);
try {
idArray.forEach((id) => disabledDeprecations.add(id));
return await callback();
} finally {
idArray.forEach((id) => disabledDeprecations.delete(id));
return result;
}
}

View File

@ -3,7 +3,7 @@ import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import { render } from "@ember/test-helpers";
import { query } from "discourse/tests/helpers/qunit-helpers";
import { hbs } from "ember-cli-htmlbars";
import { withSilencedDeprecations } from "discourse-common/lib/deprecated";
import { withSilencedDeprecationsAsync } from "discourse-common/lib/deprecated";
function paste(element, text) {
let e = new Event("paste");
@ -17,7 +17,7 @@ module("Integration | Component | user-selector", function (hooks) {
test("pasting a list of usernames", async function (assert) {
this.set("usernames", "evil,trout");
await withSilencedDeprecations(
await withSilencedDeprecationsAsync(
"discourse.user-selector-component",
async () => {
await render(
@ -51,7 +51,7 @@ module("Integration | Component | user-selector", function (hooks) {
this.set("usernames", "mark");
this.set("excludedUsernames", ["jeff", "sam", "robin"]);
await withSilencedDeprecations(
await withSilencedDeprecationsAsync(
"discourse.user-selector-component",
async () => {
await render(

View File

@ -1,6 +1,7 @@
import {
default as deprecated,
withSilencedDeprecations,
withSilencedDeprecationsAsync,
} from "discourse-common/lib/deprecated";
import DeprecationCounter from "discourse/tests/helpers/deprecation-counter";
import { module, test } from "qunit";
@ -175,4 +176,33 @@ module("Unit | Utility | deprecated", function (hooks) {
"counter is not incremented"
);
});
test("can silence deprecations with async callback in tests", async function (assert) {
await withSilencedDeprecationsAsync("discourse.one", async () => {
await Promise.resolve();
deprecated("message", { id: "discourse.one" });
});
assert.strictEqual(
this.warnStub.callCount,
0,
"console.warn is not called"
);
assert.strictEqual(
this.counterStub.callCount,
0,
"counter is not incremented"
);
deprecated("message", { id: "discourse.one" });
assert.strictEqual(
this.warnStub.callCount,
1,
"console.warn is called outside the silenced function"
);
assert.strictEqual(
this.counterStub.callCount,
1,
"counter is incremented outside the silenced function"
);
});
});