{
const iconWrapper = ;
const messageWrapper = {message};
const toast = (
{iconWrapper}
{messageWrapper}
);
const updateToast = (message: string): void => {
messageWrapper.textContent = message;
};
document.body.append(toast);
await delay(30); // Without this, the Toast doesn't appear in time
try {
if (task instanceof Error) {
throw task;
}
// eslint-disable-next-line unicorn/prefer-ternary -- Naw man, that's less readable
if (typeof task === 'function') {
await task(updateToast);
} else {
await task;
}
toast.classList.replace('Toast--loading', 'Toast--success');
updateToast(doneMessage);
iconWrapper.firstChild!.replaceWith();
} catch (error) {
assertError(error);
toast.classList.replace('Toast--loading', 'Toast--error');
updateToast(error.message);
iconWrapper.firstChild!.replaceWith();
throw error;
} finally {
// Without rAF the toast might be removed before the first page paint
// rAF also allows showToast to resolve as soon as task is done
requestAnimationFrame(() => {
setTimeout(() => {
toast.remove();
}, 3000);
});
}
}