Skip to content

Instantly share code, notes, and snippets.

@jupegarnica
Last active April 25, 2021 06:55
Show Gist options
  • Save jupegarnica/cb3a28a3a3e7f40363c14bd61abe9880 to your computer and use it in GitHub Desktop.
Save jupegarnica/cb3a28a3a3e7f40363c14bd61abe9880 to your computer and use it in GitHub Desktop.
import {
assertEquals,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { spy } from "https://deno.land/x/[email protected]/mod.ts";
import type { Spy } from "https://deno.land/x/[email protected]/mod.ts";
import { delay } from "https://deno.land/[email protected]/async/delay.ts";
type Callable = (...arg:any[])=>any
// BUG
// Uncaught RangeError: Maximum call stack size exceeded
// at handleAsyncMsgFromRust (deno:core/core.js:70:23)
// https://github.com/denoland/deno/blob/fd3b961126150960104a0d456ceac14b12f90c8b/core/core.js#L69
const debounce = (fn:Callable, ms = 0) => {
let timeoutId: number;
return function(...args:any[]) {
clearTimeout(timeoutId);
timeoutId = setTimeout(fn, ms, ...args);
};
};
// FIXED with setInterval
// const debounce = function (fn: Callable, delay = 0) {
// let id: number;
// let lastTime: number;
// function watcher() {
// if (Date.now() - lastTime > delay) {
// clearInterval(id);
// id = 0;
// fn();
// }
// }
// function debounced(): void {
// lastTime = Date.now();
// if (!id) {
// id = setInterval(watcher, delay);
// }
// }
// return debounced;
// };
const RUNS =
60709; // start failing
60708; // no fails
const runner = () => {
console.count("run");
return;
};
const run: Spy = spy(console, "count");
const debounced = debounce(runner, 10);
for (let i = 0; i < RUNS ; i++) {
debounced()
}
await delay(1000);
assertEquals(run.calls.length, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment