Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor permalog tweaks #16300

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions ui/common/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,17 @@ export const requestIdleCallback = (f: () => void, timeout?: number): void => {
else requestAnimationFrame(f);
};

export const escapeHtml = (str: string): string =>
/[&<>"']/.test(str)
export function escapeHtml(str: string): string {
if (typeof str !== 'string') str = JSON.stringify(str); // throws
return /[&<>"']/.test(str)
? str
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/'/g, '&#39;')
.replace(/"/g, '&quot;')
: str;
}

export function frag<T extends Node = Node>(html: string): T {
const div = document.createElement('div');
Expand Down
42 changes: 27 additions & 15 deletions ui/common/src/permalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,24 @@ function makeLog(): LichessLog {
}

const log: LichessLog = async (...args: any[]) => {
const msg = `#${site.info ? `${site.info.commit.substring(0, 7)} - ` : ''}${args
.map(stringify)
.join(' ')}`;
let nextKey = Date.now();
console.log(...args);
if (nextKey === lastKey) {
nextKey += drift;
drift += 0.001;
} else {
drift = 0.001;
lastKey = nextKey;
try {
const msg = `#${site.info ? `${site.info.commit.substring(0, 7)} - ` : ''}${args
.map(stringify)
.join(' ')}`;
let nextKey = Date.now();
console.log(...args);
if (nextKey === lastKey) {
nextKey += drift;
drift += 0.001;
} else {
drift = 0.001;
lastKey = nextKey;
}
await ready;
await store?.put(nextKey, msg);
} catch (e) {
console.error(e);
}
await ready;
await store?.put(nextKey, msg);
};

log.clear = async () => {
Expand Down Expand Up @@ -96,11 +100,19 @@ function makeLog(): LichessLog {
show: true,
});
});

window.addEventListener('unhandledrejection', async e => {
log(`${terseHref()} - ${e.reason}`);
let reason = e.reason;
if (typeof reason !== 'string')
try {
reason = JSON.stringify(e.reason);
} catch (_) {
reason = 'unhandled rejection, reason not a string';
}
log(`${terseHref()} - ${reason}`);
if (site.debug)
domDialog({
htmlText: escapeHtml(e.reason.toString()),
htmlText: escapeHtml(reason),
class: 'debug',
show: true,
});
Expand Down
Loading