Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
16 changes: 7 additions & 9 deletions ts/editable/Mathjax.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,31 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { pageTheme } from "$lib/sveltelib/theme";

import { convertMathjax, unescapeSomeEntities } from "./mathjax";
import { ChangeTimer } from "./change-timer";
import { CooldownTimer } from "./cooldown-timer";

export let mathjax: string;
export let block: boolean;
export let fontSize: number;

let converted: string, title: string;

const debouncedMathjax = writable(mathjax);
const debouncer = new ChangeTimer();
$: debouncer.schedule(() => debouncedMathjax.set(mathjax), 500);
const debouncer = new CooldownTimer(500);

$: {
$: debouncer.schedule(() => {
const cache = getCache($pageTheme.isDark, fontSize);
const entry = cache.get($debouncedMathjax);
const entry = cache.get(mathjax);
if (entry) {
[converted, title] = entry;
} else {
const entry = convertMathjax(
unescapeSomeEntities($debouncedMathjax),
unescapeSomeEntities(mathjax),
$pageTheme.isDark,
fontSize,
);
[converted, title] = entry;
cache.set($debouncedMathjax, entry);
cache.set(mathjax, entry);
}
}
});
$: empty = title === "MathJax";
$: encoded = encodeURIComponent(converted);

Expand Down
31 changes: 31 additions & 0 deletions ts/editable/cooldown-timer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html

export class CooldownTimer {
private executing = false;
private queuedAction: (() => void) | null = null;
private delay: number;

constructor(delayMs: number) {
this.delay = delayMs;
}

schedule(action: () => void): void {
if (this.executing) {
this.queuedAction = action;
} else {
this.executing = true;
action();
setTimeout(this.#pop.bind(this), this.delay);
}
}

#pop(): void {
this.executing = false;
if (this.queuedAction) {
const action = this.queuedAction;
this.queuedAction = null;
this.schedule(action);
}
}
}