Although some may argue that users need to be quick in reading auto-hiding messages, I personally find them useful in specific scenarios. In the past, VS Code had message toasts that were perfect for such messages, but they have been removed. As a result, I created a replacement using the progress API:
/**
* Displays a message that automatically closes after a set timeout. Since there is no dedicated API for this functionality,
* the progress output is utilized instead, as it automatically closes at 100%.
* Therefore, this function is not suitable (and should not be used) for warnings or errors, which require
* user attention.
*
* @param message The message to display.
* @param timeout The time in milliseconds before the message closes (default is 3 seconds).
*/
export const showMessageWithTimeout = (message: string, timeout = 3000): void => {
void window.withProgress(
{
location: ProgressLocation.Notification,
title: message,
cancellable: false,
},
async (progress): Promise<void> => {
await waitFor(timeout, () => { return false; });
progress.report({ increment: 100 });
},
);
};
(excerpt from https://github.com/mysql/mysql-shell-plugins/blob/master/gui/extension/src/utilities.ts)
and
/**
* Awaits the fulfillment of a condition.
*
* @param timeout The duration in milliseconds to wait for the condition.
* @param condition A function that evaluates whether the condition has been met.
*
* @returns A promise that resolves to true if the condition is met within the specified timeout; otherwise false.
*/
export const waitFor = async (timeout: number, condition: () => boolean): Promise<boolean> => {
while (!condition() && timeout > 0) {
timeout -= 100;
await sleep(100);
}
return timeout > 0 ? true : false;
};
(excerpt from https://github.com/mysql/mysql-shell-plugins/blob/master/gui/frontend/src/utilities/helpers.ts)