FIX: Correctly debounce various functions (#18673)

Debouncing inline anonymous functions does not work.

This fixes all instances of that error by extracting the function or using the new `@debounce(delay)` decorator
This commit is contained in:
Jarek Radosz
2022-10-20 13:28:09 +02:00
committed by GitHub
parent ce53152e53
commit 8304f40f84
12 changed files with 170 additions and 204 deletions

View File

@@ -1,12 +1,14 @@
/* global Pikaday:true */
import computed, { observes } from "discourse-common/utils/decorators";
import computed, {
debounce,
observes,
} from "discourse-common/utils/decorators";
import Component from "@ember/component";
import EmberObject, { action } from "@ember/object";
import I18n from "I18n";
import { INPUT_DELAY } from "discourse-common/config/environment";
import { Promise } from "rsvp";
import { cookAsync } from "discourse/lib/text";
import discourseDebounce from "discourse-common/lib/debounce";
import { isEmpty } from "@ember/utils";
import loadScript from "discourse/lib/load-script";
import { notEmpty } from "@ember/object/computed";
@@ -59,25 +61,19 @@ export default Component.extend({
},
@observes("computedConfig.{from,to,options}", "options", "isValid", "isRange")
_renderPreview() {
discourseDebounce(
this,
function () {
const markup = this.markup;
if (markup) {
cookAsync(markup).then((result) => {
this.set("currentPreview", result);
schedule("afterRender", () => {
applyLocalDates(
document.querySelectorAll(".preview .discourse-local-date"),
this.siteSettings
);
});
});
}
},
INPUT_DELAY
);
@debounce(INPUT_DELAY)
async _renderPreview() {
if (this.markup) {
const result = await cookAsync(this.markup);
this.set("currentPreview", result);
schedule("afterRender", () => {
applyLocalDates(
document.querySelectorAll(".preview .discourse-local-date"),
this.siteSettings
);
});
}
},
@computed("date", "toDate", "toTime")

View File

@@ -1,5 +1,5 @@
import { debounce } from "discourse-common/utils/decorators";
import { ajax } from "discourse/lib/ajax";
import discourseDebounce from "discourse-common/lib/debounce";
import { headerOffset } from "discourse/lib/offset-calculator";
import isElementInViewport from "discourse/lib/is-element-in-viewport";
import { withPluginApi } from "discourse/lib/plugin-api";
@@ -74,28 +74,22 @@ function initialize(api) {
// No need to unsubscribe, core unsubscribes /topic/* routes
},
@debounce(500)
_scrollToDiscobotPost(postNumber) {
discourseDebounce(
this,
function () {
const post = document.querySelector(
`.topic-post article#post_${postNumber}`
);
if (!post || isElementInViewport(post)) {
return;
}
const viewportOffset = post.getBoundingClientRect();
window.scrollTo({
top: window.scrollY + viewportOffset.top - headerOffset(),
behavior: "smooth",
});
},
postNumber,
500
const post = document.querySelector(
`.topic-post article#post_${postNumber}`
);
if (!post || isElementInViewport(post)) {
return;
}
const viewportOffset = post.getBoundingClientRect();
window.scrollTo({
top: window.scrollY + viewportOffset.top - headerOffset(),
behavior: "smooth",
});
},
});