2022-10-10 12:50:21 +00:00
|
|
|
import { flashNotification } from "../../syscall/silverbullet-syscall/editor.ts";
|
2022-07-25 18:52:05 +00:00
|
|
|
|
2022-04-19 14:54:47 +00:00
|
|
|
export async function replaceAsync(
|
|
|
|
str: string,
|
|
|
|
regex: RegExp,
|
2022-10-12 09:47:13 +00:00
|
|
|
asyncFn: (match: string, ...args: any[]) => Promise<string>,
|
2022-04-19 14:54:47 +00:00
|
|
|
) {
|
|
|
|
const promises: Promise<string>[] = [];
|
|
|
|
str.replace(regex, (match: string, ...args: any[]): string => {
|
|
|
|
const promise = asyncFn(match, ...args);
|
|
|
|
promises.push(promise);
|
|
|
|
return "";
|
|
|
|
});
|
|
|
|
const data = await Promise.all(promises);
|
|
|
|
return str.replace(regex, () => data.shift()!);
|
|
|
|
}
|
2022-07-25 18:52:05 +00:00
|
|
|
|
|
|
|
export function isServer() {
|
2022-10-10 12:50:21 +00:00
|
|
|
return (
|
|
|
|
typeof window === "undefined" || typeof window.document === "undefined"
|
|
|
|
); // if something defines window the same way as the browser, this will fail.
|
2022-07-25 18:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// this helps keep if's condition as positive
|
|
|
|
export function isBrowser() {
|
|
|
|
return !isServer();
|
|
|
|
}
|
|
|
|
|
2022-10-10 12:50:21 +00:00
|
|
|
export async function notifyUser(message: string, type?: "info" | "error") {
|
2022-07-25 18:52:05 +00:00
|
|
|
if (isBrowser()) {
|
|
|
|
return flashNotification(message, type);
|
|
|
|
}
|
|
|
|
const log = type === "error" ? console.error : console.log;
|
|
|
|
log(message); // we should end up sending the message to the user, users dont read logs.
|
|
|
|
return;
|
2022-10-10 12:50:21 +00:00
|
|
|
}
|