2022-06-22 05:34:04 +08:00
|
|
|
import { debounce } from "@ember/runloop";
|
2022-06-22 04:01:03 +08:00
|
|
|
import { isTesting } from "discourse-common/config/environment";
|
2021-01-22 04:55:39 +08:00
|
|
|
|
2020-12-18 21:18:52 +08:00
|
|
|
/**
|
|
|
|
Debounce a Javascript function. This means if it's called many times in a time limit it
|
|
|
|
should only be executed once (at the end of the limit counted from the last call made).
|
|
|
|
Original function will be called with the context and arguments from the last call made.
|
|
|
|
**/
|
|
|
|
|
|
|
|
export default function () {
|
|
|
|
if (isTesting()) {
|
2022-11-11 20:11:41 +08:00
|
|
|
const lastArgument = arguments[arguments.length - 1];
|
|
|
|
const hasImmediateArgument = typeof lastArgument === "boolean";
|
|
|
|
|
|
|
|
let args = [].slice.call(arguments, 0, hasImmediateArgument ? -2 : -1);
|
|
|
|
|
2022-06-22 05:34:04 +08:00
|
|
|
// Replace the time argument with 10ms
|
|
|
|
args.push(10);
|
|
|
|
|
2022-11-11 20:11:41 +08:00
|
|
|
if (hasImmediateArgument) {
|
|
|
|
args.push(lastArgument);
|
|
|
|
}
|
|
|
|
|
2022-06-22 05:34:04 +08:00
|
|
|
return debounce.apply(undefined, args);
|
2020-12-18 21:18:52 +08:00
|
|
|
} else {
|
|
|
|
return debounce(...arguments);
|
|
|
|
}
|
|
|
|
}
|