From aa0d4ea764959bd7ec5127f78dbec68de4dc8337 Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Thu, 10 Dec 2020 16:48:25 -0300 Subject: [PATCH] Revert "DEV: Wrap `Ember.run.debounce`. (#11352)" (#11465) This reverts commit 8b426431a41ad6e91de05614a2d35d48c5112fa7. --- .../addon/components/admin-backups-logs.js | 14 +- .../addon/components/admin-report-chart.js | 7 +- .../components/admin-report-stacked-chart.js | 7 +- .../controllers/admin-dashboard-reports.js | 4 +- .../addon/controllers/admin-email-bounced.js | 8 +- .../addon/controllers/admin-email-received.js | 8 +- .../addon/controllers/admin-email-rejected.js | 8 +- .../addon/controllers/admin-email-sent.js | 8 +- .../addon/controllers/admin-email-skipped.js | 8 +- .../admin-logs-screened-ip-addresses.js | 12 +- .../addon/controllers/admin-permalinks.js | 12 +- .../addon/controllers/admin-site-settings.js | 22 ++- .../controllers/admin-site-text-index.js | 6 +- .../controllers/admin-users-list-show.js | 8 +- .../addon/controllers/admin-watched-words.js | 16 +-- .../discourse-common/addon/lib/debounce.js | 15 --- .../app/components/choose-message.js | 55 ++++---- .../discourse/app/components/choose-topic.js | 65 ++++----- .../discourse/app/components/composer-body.js | 12 +- .../app/components/composer-editor.js | 9 +- .../app/components/composer-title.js | 7 +- .../discourse/app/components/d-editor.js | 5 +- .../app/components/group-flair-inputs.js | 4 +- .../components/groups-form-profile-fields.js | 74 +++++------ .../discourse/app/components/quote-button.js | 9 +- .../app/components/scrolling-post-stream.js | 8 +- .../discourse/app/components/text-field.js | 9 +- .../app/components/topic-navigation.js | 5 +- .../discourse/app/controllers/composer.js | 11 +- .../discourse/app/controllers/group-index.js | 14 +- .../app/controllers/group-requests.js | 14 +- .../discourse/app/controllers/groups-index.js | 4 +- .../app/controllers/insert-hyperlink.js | 5 +- .../discourse/app/controllers/topic.js | 23 ++-- .../app/controllers/user-invited-show.js | 22 ++- .../discourse/app/controllers/users.js | 14 +- .../discourse/app/lib/autocomplete.js | 5 +- .../discourse/app/lib/category-tag-search.js | 71 +++++----- .../discourse/app/lib/reports-loader.js | 9 +- .../discourse/app/lib/safari-hacks.js | 24 ++-- .../discourse/app/lib/user-search.js | 29 +--- .../discourse/app/mixins/docking.js | 5 +- .../app/mixins/mobile-scroll-direction.js | 12 +- .../discourse/app/mixins/scrolling.js | 6 +- .../app/mixins/username-validation.js | 125 ++++++++---------- .../discourse/app/widgets/post-stream.js | 4 +- .../discourse/app/widgets/search-menu.js | 4 +- .../pretty-text/addon/upload-short-url.js | 4 +- .../select-kit/addon/components/select-kit.js | 12 +- .../discourse-local-dates-create-form.js.es6 | 30 ++--- 50 files changed, 354 insertions(+), 518 deletions(-) delete mode 100644 app/assets/javascripts/discourse-common/addon/lib/debounce.js diff --git a/app/assets/javascripts/admin/addon/components/admin-backups-logs.js b/app/assets/javascripts/admin/addon/components/admin-backups-logs.js index f691f3ffc69..2531919aa2a 100644 --- a/app/assets/javascripts/admin/addon/components/admin-backups-logs.js +++ b/app/assets/javascripts/admin/addon/components/admin-backups-logs.js @@ -1,7 +1,7 @@ import { observes, on } from "discourse-common/utils/decorators"; import Component from "@ember/component"; import I18n from "I18n"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { scheduleOnce } from "@ember/runloop"; export default Component.extend({ @@ -33,7 +33,9 @@ export default Component.extend({ } }, - _updateFormattedLogsFunc: function () { + @on("init") + @observes("logs.[]") + _updateFormattedLogs: discourseDebounce(function () { const logs = this.logs; if (logs.length === 0) { return; @@ -55,13 +57,7 @@ export default Component.extend({ this.renderLogs(); scheduleOnce("afterRender", this, this._scrollDown); - }, - - @on("init") - @observes("logs.[]") - _updateFormattedLogs() { - discourseDebounce(this, this._updateFormattedLogsFunc, 150); - }, + }, 150), renderLogs() { const formattedLogs = this.formattedLogs; diff --git a/app/assets/javascripts/admin/addon/components/admin-report-chart.js b/app/assets/javascripts/admin/addon/components/admin-report-chart.js index 8a894b95629..40f927f55da 100644 --- a/app/assets/javascripts/admin/addon/components/admin-report-chart.js +++ b/app/assets/javascripts/admin/addon/components/admin-report-chart.js @@ -1,9 +1,8 @@ +import { debounce, schedule } from "@ember/runloop"; import Component from "@ember/component"; -import discourseDebounce from "discourse-common/lib/debounce"; import loadScript from "discourse/lib/load-script"; import { makeArray } from "discourse-common/lib/helpers"; import { number } from "discourse/lib/formatter"; -import { schedule } from "@ember/runloop"; export default Component.extend({ classNames: ["admin-report-chart"], @@ -15,7 +14,7 @@ export default Component.extend({ this._super(...arguments); this.resizeHandler = () => - discourseDebounce(this, this._scheduleChartRendering, 500); + debounce(this, this._scheduleChartRendering, 500); }, didInsertElement() { @@ -35,7 +34,7 @@ export default Component.extend({ didReceiveAttrs() { this._super(...arguments); - discourseDebounce(this, this._scheduleChartRendering, 100); + debounce(this, this._scheduleChartRendering, 100); }, _scheduleChartRendering() { diff --git a/app/assets/javascripts/admin/addon/components/admin-report-stacked-chart.js b/app/assets/javascripts/admin/addon/components/admin-report-stacked-chart.js index 4cac3b15dba..b07a8261c8e 100644 --- a/app/assets/javascripts/admin/addon/components/admin-report-stacked-chart.js +++ b/app/assets/javascripts/admin/addon/components/admin-report-stacked-chart.js @@ -1,9 +1,8 @@ +import { debounce, schedule } from "@ember/runloop"; import Component from "@ember/component"; -import discourseDebounce from "discourse-common/lib/debounce"; import loadScript from "discourse/lib/load-script"; import { makeArray } from "discourse-common/lib/helpers"; import { number } from "discourse/lib/formatter"; -import { schedule } from "@ember/runloop"; export default Component.extend({ classNames: ["admin-report-chart", "admin-report-stacked-chart"], @@ -12,7 +11,7 @@ export default Component.extend({ this._super(...arguments); this.resizeHandler = () => - discourseDebounce(this, this._scheduleChartRendering, 500); + debounce(this, this._scheduleChartRendering, 500); }, didInsertElement() { @@ -32,7 +31,7 @@ export default Component.extend({ didReceiveAttrs() { this._super(...arguments); - discourseDebounce(this, this._scheduleChartRendering, 100); + debounce(this, this._scheduleChartRendering, 100); }, _scheduleChartRendering() { diff --git a/app/assets/javascripts/admin/addon/controllers/admin-dashboard-reports.js b/app/assets/javascripts/admin/addon/controllers/admin-dashboard-reports.js index ab10591c808..445eb667d8b 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-dashboard-reports.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-dashboard-reports.js @@ -1,7 +1,7 @@ import Controller from "@ember/controller"; import { INPUT_DELAY } from "discourse-common/config/environment"; +import { debounce } from "@ember/runloop"; import discourseComputed from "discourse-common/utils/decorators"; -import discourseDebounce from "discourse-common/lib/debounce"; const { get } = Ember; @@ -34,7 +34,7 @@ export default Controller.extend({ actions: { filterReports(filter) { - discourseDebounce(this, this._performFiltering, filter, INPUT_DELAY); + debounce(this, this._performFiltering, filter, INPUT_DELAY); }, }, diff --git a/app/assets/javascripts/admin/addon/controllers/admin-email-bounced.js b/app/assets/javascripts/admin/addon/controllers/admin-email-bounced.js index 15005495645..874b7147fb9 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-email-bounced.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-email-bounced.js @@ -1,11 +1,11 @@ import AdminEmailLogsController from "admin/controllers/admin-email-logs"; import { INPUT_DELAY } from "discourse-common/config/environment"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { observes } from "discourse-common/utils/decorators"; export default AdminEmailLogsController.extend({ @observes("filter.{status,user,address,type}") - filterEmailLogs() { - discourseDebounce(this, this.loadLogs, INPUT_DELAY); - }, + filterEmailLogs: discourseDebounce(function () { + this.loadLogs(); + }, INPUT_DELAY), }); diff --git a/app/assets/javascripts/admin/addon/controllers/admin-email-received.js b/app/assets/javascripts/admin/addon/controllers/admin-email-received.js index 0a2933fa23d..8b977c4d507 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-email-received.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-email-received.js @@ -1,14 +1,14 @@ import AdminEmailLogsController from "admin/controllers/admin-email-logs"; import { INPUT_DELAY } from "discourse-common/config/environment"; import IncomingEmail from "admin/models/incoming-email"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { observes } from "discourse-common/utils/decorators"; export default AdminEmailLogsController.extend({ @observes("filter.{status,from,to,subject}") - filterIncomingEmails() { - discourseDebounce(this, this.loadLogs, IncomingEmail, INPUT_DELAY); - }, + filterIncomingEmails: discourseDebounce(function () { + this.loadLogs(IncomingEmail); + }, INPUT_DELAY), actions: { loadMore() { diff --git a/app/assets/javascripts/admin/addon/controllers/admin-email-rejected.js b/app/assets/javascripts/admin/addon/controllers/admin-email-rejected.js index 89c67f3cf95..5be59e6ab89 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-email-rejected.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-email-rejected.js @@ -1,14 +1,14 @@ import AdminEmailLogsController from "admin/controllers/admin-email-logs"; import { INPUT_DELAY } from "discourse-common/config/environment"; import IncomingEmail from "admin/models/incoming-email"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { observes } from "discourse-common/utils/decorators"; export default AdminEmailLogsController.extend({ @observes("filter.{status,from,to,subject,error}") - filterIncomingEmails() { - discourseDebounce(this, this.loadLogs, IncomingEmail, INPUT_DELAY); - }, + filterIncomingEmails: discourseDebounce(function () { + this.loadLogs(IncomingEmail); + }, INPUT_DELAY), actions: { loadMore() { diff --git a/app/assets/javascripts/admin/addon/controllers/admin-email-sent.js b/app/assets/javascripts/admin/addon/controllers/admin-email-sent.js index 5cc83109f8f..660f681c62e 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-email-sent.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-email-sent.js @@ -1,11 +1,11 @@ import AdminEmailLogsController from "admin/controllers/admin-email-logs"; import { INPUT_DELAY } from "discourse-common/config/environment"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { observes } from "discourse-common/utils/decorators"; export default AdminEmailLogsController.extend({ @observes("filter.{status,user,address,type,reply_key}") - filterEmailLogs() { - discourseDebounce(this, this.loadLogs, INPUT_DELAY); - }, + filterEmailLogs: discourseDebounce(function () { + this.loadLogs(); + }, INPUT_DELAY), }); diff --git a/app/assets/javascripts/admin/addon/controllers/admin-email-skipped.js b/app/assets/javascripts/admin/addon/controllers/admin-email-skipped.js index 15005495645..874b7147fb9 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-email-skipped.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-email-skipped.js @@ -1,11 +1,11 @@ import AdminEmailLogsController from "admin/controllers/admin-email-logs"; import { INPUT_DELAY } from "discourse-common/config/environment"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { observes } from "discourse-common/utils/decorators"; export default AdminEmailLogsController.extend({ @observes("filter.{status,user,address,type}") - filterEmailLogs() { - discourseDebounce(this, this.loadLogs, INPUT_DELAY); - }, + filterEmailLogs: discourseDebounce(function () { + this.loadLogs(); + }, INPUT_DELAY), }); diff --git a/app/assets/javascripts/admin/addon/controllers/admin-logs-screened-ip-addresses.js b/app/assets/javascripts/admin/addon/controllers/admin-logs-screened-ip-addresses.js index fb923131ed7..32ed7ad944d 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-logs-screened-ip-addresses.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-logs-screened-ip-addresses.js @@ -3,7 +3,7 @@ import I18n from "I18n"; import { INPUT_DELAY } from "discourse-common/config/environment"; import ScreenedIpAddress from "admin/models/screened-ip-address"; import bootbox from "bootbox"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { exportEntity } from "discourse/lib/export-csv"; import { observes } from "discourse-common/utils/decorators"; import { outputExportResult } from "discourse/lib/export-result"; @@ -13,17 +13,13 @@ export default Controller.extend({ filter: null, savedIpAddress: null, - _debouncedShow() { + @observes("filter") + show: discourseDebounce(function () { this.set("loading", true); ScreenedIpAddress.findAll(this.filter).then((result) => { this.setProperties({ model: result, loading: false }); }); - }, - - @observes("filter") - show() { - discourseDebounce(this, this._debouncedShow, INPUT_DELAY); - }, + }, INPUT_DELAY), actions: { allow(record) { diff --git a/app/assets/javascripts/admin/addon/controllers/admin-permalinks.js b/app/assets/javascripts/admin/addon/controllers/admin-permalinks.js index b11b3973117..2aaf42ff3e8 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-permalinks.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-permalinks.js @@ -3,24 +3,20 @@ import I18n from "I18n"; import { INPUT_DELAY } from "discourse-common/config/environment"; import Permalink from "admin/models/permalink"; import bootbox from "bootbox"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { observes } from "discourse-common/utils/decorators"; export default Controller.extend({ loading: false, filter: null, - _debouncedShow() { + @observes("filter") + show: discourseDebounce(function () { Permalink.findAll(this.filter).then((result) => { this.set("model", result); this.set("loading", false); }); - }, - - @observes("filter") - show() { - discourseDebounce(this, this._debouncedShow, INPUT_DELAY); - }, + }, INPUT_DELAY), actions: { recordAdded(arg) { diff --git a/app/assets/javascripts/admin/addon/controllers/admin-site-settings.js b/app/assets/javascripts/admin/addon/controllers/admin-site-settings.js index 58859ae9335..7dea3a237cf 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-site-settings.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-site-settings.js @@ -2,7 +2,7 @@ import Controller from "@ember/controller"; import I18n from "I18n"; import { INPUT_DELAY } from "discourse-common/config/environment"; import { alias } from "@ember/object/computed"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { isEmpty } from "@ember/utils"; import { observes } from "discourse-common/utils/decorators"; @@ -112,19 +112,13 @@ export default Controller.extend({ }, @observes("filter", "onlyOverridden", "model") - filterContent() { - discourseDebounce( - this, - () => { - if (this._skipBounce) { - this.set("_skipBounce", false); - } else { - this.filterContentNow(this.categoryNameKey); - } - }, - INPUT_DELAY - ); - }, + filterContent: discourseDebounce(function () { + if (this._skipBounce) { + this.set("_skipBounce", false); + } else { + this.filterContentNow(this.categoryNameKey); + } + }, INPUT_DELAY), actions: { clearFilter() { diff --git a/app/assets/javascripts/admin/addon/controllers/admin-site-text-index.js b/app/assets/javascripts/admin/addon/controllers/admin-site-text-index.js index 289c49da87f..fb1a238c3e9 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-site-text-index.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-site-text-index.js @@ -1,5 +1,5 @@ import Controller from "@ember/controller"; -import discourseDebounce from "discourse-common/lib/debounce"; +import { debounce } from "@ember/runloop"; let lastSearch; export default Controller.extend({ @@ -28,14 +28,14 @@ export default Controller.extend({ toggleOverridden() { this.toggleProperty("overridden"); this.set("searching", true); - discourseDebounce(this, this._performSearch, 400); + debounce(this, this._performSearch, 400); }, search() { const q = this.q; if (q !== lastSearch) { this.set("searching", true); - discourseDebounce(this, this._performSearch, 400); + debounce(this, this._performSearch, 400); lastSearch = q; } }, diff --git a/app/assets/javascripts/admin/addon/controllers/admin-users-list-show.js b/app/assets/javascripts/admin/addon/controllers/admin-users-list-show.js index b0b25706e5a..0c2952e9297 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-users-list-show.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-users-list-show.js @@ -4,7 +4,7 @@ import CanCheckEmails from "discourse/mixins/can-check-emails"; import Controller from "@ember/controller"; import I18n from "I18n"; import { INPUT_DELAY } from "discourse-common/config/environment"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { i18n } from "discourse/lib/computed"; export default Controller.extend(CanCheckEmails, { @@ -32,9 +32,9 @@ export default Controller.extend(CanCheckEmails, { }, @observes("listFilter") - _filterUsers() { - discourseDebounce(this, this.resetFilters, INPUT_DELAY); - }, + _filterUsers: discourseDebounce(function () { + this.resetFilters(); + }, INPUT_DELAY), resetFilters() { this._page = 1; diff --git a/app/assets/javascripts/admin/addon/controllers/admin-watched-words.js b/app/assets/javascripts/admin/addon/controllers/admin-watched-words.js index cfd86c6c22d..3ed8bee62fa 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-watched-words.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-watched-words.js @@ -2,7 +2,7 @@ import Controller from "@ember/controller"; import EmberObject from "@ember/object"; import { INPUT_DELAY } from "discourse-common/config/environment"; import { alias } from "@ember/object/computed"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { isEmpty } from "@ember/utils"; import { observes } from "discourse-common/utils/decorators"; @@ -48,16 +48,10 @@ export default Controller.extend({ }, @observes("filter") - filterContent() { - discourseDebounce( - this, - function () { - this.filterContentNow(); - this.set("filtered", !isEmpty(this.filter)); - }, - INPUT_DELAY - ); - }, + filterContent: discourseDebounce(function () { + this.filterContentNow(); + this.set("filtered", !isEmpty(this.filter)); + }, INPUT_DELAY), actions: { clearFilter() { diff --git a/app/assets/javascripts/discourse-common/addon/lib/debounce.js b/app/assets/javascripts/discourse-common/addon/lib/debounce.js deleted file mode 100644 index 541498fda63..00000000000 --- a/app/assets/javascripts/discourse-common/addon/lib/debounce.js +++ /dev/null @@ -1,15 +0,0 @@ -import { debounce, run } from "@ember/runloop"; -import { isTesting } from "discourse-common/config/environment"; -/** - 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()) { - return run(...arguments); - } else { - return debounce(...arguments); - } -} diff --git a/app/assets/javascripts/discourse/app/components/choose-message.js b/app/assets/javascripts/discourse/app/components/choose-message.js index 09d9a9123cb..22833e61f5e 100644 --- a/app/assets/javascripts/discourse/app/components/choose-message.js +++ b/app/assets/javascripts/discourse/app/components/choose-message.js @@ -1,5 +1,5 @@ import Component from "@ember/component"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { get } from "@ember/object"; import { isEmpty } from "@ember/utils"; import { next } from "@ember/runloop"; @@ -30,38 +30,31 @@ export default Component.extend({ this.set("loading", false); }, - search(title) { - discourseDebounce( - this, - function () { - const currentTopicId = this.currentTopicId; + search: discourseDebounce(function (title) { + const currentTopicId = this.currentTopicId; - if (isEmpty(title)) { - this.setProperties({ messages: null, loading: false }); - return; - } + if (isEmpty(title)) { + this.setProperties({ messages: null, loading: false }); + return; + } - searchForTerm(title, { - typeFilter: "private_messages", - searchForId: true, - restrictToArchetype: "private_message", - }).then((results) => { - if (results && results.posts && results.posts.length > 0) { - this.set( - "messages", - results.posts - .mapBy("topic") - .filter((t) => t.get("id") !== currentTopicId) - ); - } else { - this.setProperties({ messages: null, loading: false }); - } - }); - }, - title, - 300 - ); - }, + searchForTerm(title, { + typeFilter: "private_messages", + searchForId: true, + restrictToArchetype: "private_message", + }).then((results) => { + if (results && results.posts && results.posts.length > 0) { + this.set( + "messages", + results.posts + .mapBy("topic") + .filter((t) => t.get("id") !== currentTopicId) + ); + } else { + this.setProperties({ messages: null, loading: false }); + } + }); + }, 300), actions: { chooseMessage(message) { diff --git a/app/assets/javascripts/discourse/app/components/choose-topic.js b/app/assets/javascripts/discourse/app/components/choose-topic.js index ab0bc966bfc..01352333ae3 100644 --- a/app/assets/javascripts/discourse/app/components/choose-topic.js +++ b/app/assets/javascripts/discourse/app/components/choose-topic.js @@ -1,6 +1,6 @@ import discourseComputed, { observes } from "discourse-common/utils/decorators"; import Component from "@ember/component"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { isEmpty } from "@ember/utils"; import { next } from "@ember/runloop"; import { searchForTerm } from "discourse/lib/search"; @@ -64,46 +64,37 @@ export default Component.extend({ this.set("loading", false); }, - search(title) { - discourseDebounce( - this, - function () { - if (!this.element || this.isDestroying || this.isDestroyed) { - return; - } + search: discourseDebounce(function (title) { + if (!this.element || this.isDestroying || this.isDestroyed) { + return; + } - if (isEmpty(title) && isEmpty(this.additionalFilters)) { - this.setProperties({ topics: null, loading: false }); - return; - } + if (isEmpty(title) && isEmpty(this.additionalFilters)) { + this.setProperties({ topics: null, loading: false }); + return; + } - const currentTopicId = this.currentTopicId; - const titleWithFilters = `${title} ${this.additionalFilters}`; - let searchParams = {}; + const currentTopicId = this.currentTopicId; + const titleWithFilters = `${title} ${this.additionalFilters}`; + let searchParams = {}; - if (!isEmpty(title)) { - searchParams.typeFilter = "topic"; - searchParams.restrictToArchetype = "regular"; - searchParams.searchForId = true; - } + if (!isEmpty(title)) { + searchParams.typeFilter = "topic"; + searchParams.restrictToArchetype = "regular"; + searchParams.searchForId = true; + } - searchForTerm(titleWithFilters, searchParams).then((results) => { - if (results && results.posts && results.posts.length > 0) { - this.set( - "topics", - results.posts - .mapBy("topic") - .filter((t) => t.id !== currentTopicId) - ); - } else { - this.setProperties({ topics: null, loading: false }); - } - }); - }, - title, - 300 - ); - }, + searchForTerm(titleWithFilters, searchParams).then((results) => { + if (results && results.posts && results.posts.length > 0) { + this.set( + "topics", + results.posts.mapBy("topic").filter((t) => t.id !== currentTopicId) + ); + } else { + this.setProperties({ topics: null, loading: false }); + } + }); + }, 300), actions: { chooseTopic(topic) { diff --git a/app/assets/javascripts/discourse/app/components/composer-body.js b/app/assets/javascripts/discourse/app/components/composer-body.js index 4301331e15f..24f978c6877 100644 --- a/app/assets/javascripts/discourse/app/components/composer-body.js +++ b/app/assets/javascripts/discourse/app/components/composer-body.js @@ -1,10 +1,16 @@ -import { cancel, later, run, schedule, throttle } from "@ember/runloop"; +import { + cancel, + debounce, + later, + run, + schedule, + throttle, +} from "@ember/runloop"; import discourseComputed, { observes } from "discourse-common/utils/decorators"; import Component from "@ember/component"; import Composer from "discourse/models/composer"; import KeyEnterEscape from "discourse/mixins/key-enter-escape"; import afterTransition from "discourse/lib/after-transition"; -import discourseDebounce from "discourse-common/lib/debounce"; import { headerHeight } from "discourse/components/site-header"; import { iOSWithVisualViewport } from "discourse/lib/utilities"; import positioningWorkaround from "discourse/lib/safari-hacks"; @@ -70,7 +76,7 @@ export default Component.extend(KeyEnterEscape, { return; } - discourseDebounce(this, this.debounceMove, 300); + debounce(this, this.debounceMove, 300); }); }, diff --git a/app/assets/javascripts/discourse/app/components/composer-editor.js b/app/assets/javascripts/discourse/app/components/composer-editor.js index 45dc0da37f4..bc90e6bf8cc 100644 --- a/app/assets/javascripts/discourse/app/components/composer-editor.js +++ b/app/assets/javascripts/discourse/app/components/composer-editor.js @@ -15,6 +15,7 @@ import { inCodeBlock, tinyAvatar, } from "discourse/lib/utilities"; +import { debounce, later, next, run, schedule, throttle } from "@ember/runloop"; import discourseComputed, { observes, on, @@ -27,14 +28,12 @@ import { fetchUnseenMentions, linkSeenMentions, } from "discourse/lib/link-mentions"; -import { later, next, run, schedule, throttle } from "@ember/runloop"; import Component from "@ember/component"; import Composer from "discourse/models/composer"; import EmberObject from "@ember/object"; import I18n from "I18n"; import { ajax } from "discourse/lib/ajax"; import bootbox from "bootbox"; -import discourseDebounce from "discourse-common/lib/debounce"; import { findRawTemplate } from "discourse-common/lib/raw-templates"; import getURL from "discourse-common/lib/get-url"; import { iconHTML } from "discourse-common/lib/icon-library"; @@ -906,7 +905,7 @@ export default Component.extend({ // Paint mentions const unseenMentions = linkSeenMentions($preview, this.siteSettings); if (unseenMentions.length) { - discourseDebounce( + debounce( this, this._renderUnseenMentions, $preview, @@ -921,7 +920,7 @@ export default Component.extend({ // Paint category and tag hashtags const unseenHashtags = linkSeenHashtags($preview); if (unseenHashtags.length > 0) { - discourseDebounce(this, this._renderUnseenHashtags, $preview, 450); + debounce(this, this._renderUnseenHashtags, $preview, 450); } // Paint oneboxes @@ -948,7 +947,7 @@ export default Component.extend({ } }; - discourseDebounce(this, paintFunc, 450); + debounce(this, paintFunc, 450); // Short upload urls need resolution resolveAllShortUrls(ajax, this.siteSettings, $preview[0]); diff --git a/app/assets/javascripts/discourse/app/components/composer-title.js b/app/assets/javascripts/discourse/app/components/composer-title.js index 7aca53f07c4..25895f9ea30 100644 --- a/app/assets/javascripts/discourse/app/components/composer-title.js +++ b/app/assets/javascripts/discourse/app/components/composer-title.js @@ -1,11 +1,10 @@ import { alias, or } from "@ember/object/computed"; +import { debounce, next, schedule } from "@ember/runloop"; import discourseComputed, { observes } from "discourse-common/utils/decorators"; -import { next, schedule } from "@ember/runloop"; import Component from "@ember/component"; import EmberObject from "@ember/object"; import I18n from "I18n"; import { ajax } from "discourse/lib/ajax"; -import discourseDebounce from "discourse-common/lib/debounce"; import { isTesting } from "discourse-common/config/environment"; import { load } from "pretty-text/oneboxer"; import { lookupCache } from "pretty-text/oneboxer-cache"; @@ -23,7 +22,7 @@ export default Component.extend({ } if (this.get("composer.titleLength") > 0) { - discourseDebounce(this, this._titleChanged, 10); + debounce(this, this._titleChanged, 10); } }, @@ -84,7 +83,7 @@ export default Component.extend({ this._checkForUrl() ); } else { - discourseDebounce(this, this._checkForUrl, 500); + debounce(this, this._checkForUrl, 500); } }, diff --git a/app/assets/javascripts/discourse/app/components/d-editor.js b/app/assets/javascripts/discourse/app/components/d-editor.js index c5c636ab67e..9fd17ad9dec 100644 --- a/app/assets/javascripts/discourse/app/components/d-editor.js +++ b/app/assets/javascripts/discourse/app/components/d-editor.js @@ -5,13 +5,13 @@ import { inCodeBlock, safariHacksDisabled, } from "discourse/lib/utilities"; +import { debounce, later, next, schedule, scheduleOnce } from "@ember/runloop"; import discourseComputed, { observes, on, } from "discourse-common/utils/decorators"; import { emojiSearch, isSkinTonableEmoji } from "pretty-text/emoji"; import { emojiUrlFor, generateCookFunction } from "discourse/lib/text"; -import { later, next, schedule, scheduleOnce } from "@ember/runloop"; import Component from "@ember/component"; import I18n from "I18n"; import Mousetrap from "mousetrap"; @@ -19,7 +19,6 @@ import { Promise } from "rsvp"; import { SKIP } from "discourse/lib/autocomplete"; import { categoryHashtagTriggerRule } from "discourse/lib/category-hashtags"; import deprecated from "discourse-common/lib/deprecated"; -import discourseDebounce from "discourse-common/lib/debounce"; import { findRawTemplate } from "discourse-common/lib/raw-templates"; import { getRegister } from "discourse-common/lib/get-owner"; import { isEmpty } from "@ember/utils"; @@ -415,7 +414,7 @@ export default Component.extend({ if (isTesting()) { this._updatePreview(); } else { - discourseDebounce(this, this._updatePreview, 30); + debounce(this, this._updatePreview, 30); } }, diff --git a/app/assets/javascripts/discourse/app/components/group-flair-inputs.js b/app/assets/javascripts/discourse/app/components/group-flair-inputs.js index 5a53a6f740d..465940927b9 100644 --- a/app/assets/javascripts/discourse/app/components/group-flair-inputs.js +++ b/app/assets/javascripts/discourse/app/components/group-flair-inputs.js @@ -7,7 +7,7 @@ import I18n from "I18n"; import { action } from "@ember/object"; import { ajax } from "discourse/lib/ajax"; import { convertIconClass } from "discourse-common/lib/icon-library"; -import discourseDebounce from "discourse-common/lib/debounce"; +import { debounce } from "@ember/runloop"; import { escapeExpression } from "discourse/lib/utilities"; import getURL from "discourse-common/lib/get-url"; import { htmlSafe } from "@ember/template"; @@ -34,7 +34,7 @@ export default Component.extend({ @observes("model.flair_icon") _loadSVGIcon(flairIcon) { if (flairIcon) { - discourseDebounce(this, this._loadIcon, 1000); + debounce(this, this._loadIcon, 1000); } }, diff --git a/app/assets/javascripts/discourse/app/components/groups-form-profile-fields.js b/app/assets/javascripts/discourse/app/components/groups-form-profile-fields.js index d2a439ed543..23672bc5b85 100644 --- a/app/assets/javascripts/discourse/app/components/groups-form-profile-fields.js +++ b/app/assets/javascripts/discourse/app/components/groups-form-profile-fields.js @@ -3,7 +3,7 @@ import Component from "@ember/component"; import EmberObject from "@ember/object"; import Group from "discourse/models/group"; import I18n from "I18n"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { isEmpty } from "@ember/utils"; import { not } from "@ember/object/computed"; import { popupAjaxError } from "discourse/lib/ajax-error"; @@ -64,46 +64,40 @@ export default Component.extend({ ); }, - checkGroupName() { - discourseDebounce( - this, - function () { - if (isEmpty(this.nameInput)) { - return; + checkGroupName: discourseDebounce(function () { + if (isEmpty(this.nameInput)) { + return; + } + + Group.checkName(this.nameInput) + .then((response) => { + const validationName = "uniqueNameValidation"; + + if (response.available) { + this.set( + validationName, + EmberObject.create({ + ok: true, + reason: I18n.t("admin.groups.new.name.available"), + }) + ); + + this.set("disableSave", false); + this.set("model.name", this.nameInput); + } else { + let reason; + + if (response.errors) { + reason = response.errors.join(" "); + } else { + reason = I18n.t("admin.groups.new.name.not_available"); + } + + this.set(validationName, this._failedInputValidation(reason)); } - - Group.checkName(this.nameInput) - .then((response) => { - const validationName = "uniqueNameValidation"; - - if (response.available) { - this.set( - validationName, - EmberObject.create({ - ok: true, - reason: I18n.t("admin.groups.new.name.available"), - }) - ); - - this.set("disableSave", false); - this.set("model.name", this.nameInput); - } else { - let reason; - - if (response.errors) { - reason = response.errors.join(" "); - } else { - reason = I18n.t("admin.groups.new.name.not_available"); - } - - this.set(validationName, this._failedInputValidation(reason)); - } - }) - .catch(popupAjaxError); - }, - 500 - ); - }, + }) + .catch(popupAjaxError); + }, 500), _failedInputValidation(reason) { this.set("disableSave", true); diff --git a/app/assets/javascripts/discourse/app/components/quote-button.js b/app/assets/javascripts/discourse/app/components/quote-button.js index 45ba6178363..6a7393775e0 100644 --- a/app/assets/javascripts/discourse/app/components/quote-button.js +++ b/app/assets/javascripts/discourse/app/components/quote-button.js @@ -9,7 +9,7 @@ import Sharing from "discourse/lib/sharing"; import { action } from "@ember/object"; import { alias } from "@ember/object/computed"; import discourseComputed from "discourse-common/utils/decorators"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { getAbsoluteURL } from "discourse-common/lib/get-url"; import { schedule } from "@ember/runloop"; import toMarkdown from "discourse/lib/to-markdown"; @@ -183,9 +183,10 @@ export default Component.extend({ const { isWinphone, isAndroid } = this.capabilities; const wait = isWinphone || isAndroid ? INPUT_DELAY : 25; - const onSelectionChanged = () => { - discourseDebounce(this, this._selectionChanged, wait); - }; + const onSelectionChanged = discourseDebounce( + () => this._selectionChanged(), + wait + ); $(document) .on("mousedown.quote-button", (e) => { diff --git a/app/assets/javascripts/discourse/app/components/scrolling-post-stream.js b/app/assets/javascripts/discourse/app/components/scrolling-post-stream.js index f11fc810f2a..e8b84a41f33 100644 --- a/app/assets/javascripts/discourse/app/components/scrolling-post-stream.js +++ b/app/assets/javascripts/discourse/app/components/scrolling-post-stream.js @@ -1,8 +1,7 @@ import { cloak, uncloak } from "discourse/widgets/post-stream"; -import { next, scheduleOnce } from "@ember/runloop"; +import { debounce, next, scheduleOnce } from "@ember/runloop"; import DiscourseURL from "discourse/lib/url"; import MountWidget from "discourse/components/mount-widget"; -import discourseDebounce from "discourse-common/lib/debounce"; import { isWorkaroundActive } from "discourse/lib/safari-hacks"; import offsetCalculator from "discourse/lib/offset-calculator"; import { inject as service } from "@ember/service"; @@ -313,13 +312,14 @@ export default MountWidget.extend({ }, _debouncedScroll() { - discourseDebounce(this, this._scrollTriggered, DEBOUNCE_DELAY); + debounce(this, this._scrollTriggered, DEBOUNCE_DELAY); }, didInsertElement() { this._super(...arguments); + const debouncedScroll = () => - discourseDebounce(this, this._scrollTriggered, DEBOUNCE_DELAY); + debounce(this, this._scrollTriggered, DEBOUNCE_DELAY); this._previouslyNearby = {}; this.appEvents.on("post-stream:refresh", this, "_debouncedScroll"); diff --git a/app/assets/javascripts/discourse/app/components/text-field.js b/app/assets/javascripts/discourse/app/components/text-field.js index 6561aed20e4..187a1661b42 100644 --- a/app/assets/javascripts/discourse/app/components/text-field.js +++ b/app/assets/javascripts/discourse/app/components/text-field.js @@ -1,9 +1,8 @@ -import { cancel, next } from "@ember/runloop"; +import { cancel, debounce, next } from "@ember/runloop"; import { isLTR, isRTL, siteDir } from "discourse/lib/text-direction"; import I18n from "I18n"; import TextField from "@ember/component/text-field"; import discourseComputed from "discourse-common/utils/decorators"; -import discourseDebounce from "discourse-common/lib/debounce"; const DEBOUNCE_MS = 500; @@ -39,11 +38,7 @@ export default TextField.extend({ } if (this.onChange) { cancel(this._timer); - this._timer = discourseDebounce( - this, - this._debouncedChange, - DEBOUNCE_MS - ); + this._timer = debounce(this, this._debouncedChange, DEBOUNCE_MS); } } }, diff --git a/app/assets/javascripts/discourse/app/components/topic-navigation.js b/app/assets/javascripts/discourse/app/components/topic-navigation.js index 5b58ff9147e..bacc74415ef 100644 --- a/app/assets/javascripts/discourse/app/components/topic-navigation.js +++ b/app/assets/javascripts/discourse/app/components/topic-navigation.js @@ -3,10 +3,9 @@ import PanEvents, { SWIPE_VELOCITY, SWIPE_VELOCITY_THRESHOLD, } from "discourse/mixins/pan-events"; +import { debounce, later } from "@ember/runloop"; import Component from "@ember/component"; import EmberObject from "@ember/object"; -import discourseDebounce from "discourse-common/lib/debounce"; -import { later } from "@ember/runloop"; import { observes } from "discourse-common/utils/decorators"; import showModal from "discourse/lib/show-modal"; @@ -54,7 +53,7 @@ export default Component.extend(PanEvents, { }, _checkSize() { - discourseDebounce(this, this._performCheckSize, 300, true); + debounce(this, this._performCheckSize, 300, true); }, // we need to store this so topic progress has something to init with diff --git a/app/assets/javascripts/discourse/app/controllers/composer.js b/app/assets/javascripts/discourse/app/controllers/composer.js index 4b737b0a9fc..9cb9d444f42 100644 --- a/app/assets/javascripts/discourse/app/controllers/composer.js +++ b/app/assets/javascripts/discourse/app/controllers/composer.js @@ -6,7 +6,7 @@ import { authorizesOneOrMoreExtensions, uploadIcon, } from "discourse/lib/uploads"; -import { cancel, run } from "@ember/runloop"; +import { cancel, debounce, run } from "@ember/runloop"; import { cannotPostAgain, durationTextFromSeconds, @@ -22,7 +22,6 @@ import { Promise } from "rsvp"; import bootbox from "bootbox"; import { buildQuote } from "discourse/lib/quote"; import deprecated from "discourse-common/lib/deprecated"; -import discourseDebounce from "discourse-common/lib/debounce"; import { emojiUnescape } from "discourse/lib/text"; import { escapeExpression } from "discourse/lib/utilities"; import { getOwner } from "discourse-common/lib/get-owner"; @@ -1163,11 +1162,7 @@ export default Controller.extend({ // in test debounce is Ember.run, this will cause // an infinite loop if (!isTesting()) { - this._saveDraftDebounce = discourseDebounce( - this, - this._saveDraft, - 2000 - ); + this._saveDraftDebounce = debounce(this, this._saveDraft, 2000); } } else { this._saveDraftPromise = model.saveDraft().finally(() => { @@ -1193,7 +1188,7 @@ export default Controller.extend({ if (Date.now() - this._lastDraftSaved > 15000) { this._saveDraft(); } else { - let method = isTesting() ? run : discourseDebounce; + let method = isTesting() ? run : debounce; this._saveDraftDebounce = method(this, this._saveDraft, 2000); } } diff --git a/app/assets/javascripts/discourse/app/controllers/group-index.js b/app/assets/javascripts/discourse/app/controllers/group-index.js index b11eb199c53..fd37fe892d7 100644 --- a/app/assets/javascripts/discourse/app/controllers/group-index.js +++ b/app/assets/javascripts/discourse/app/controllers/group-index.js @@ -1,7 +1,7 @@ import Controller, { inject as controller } from "@ember/controller"; import discourseComputed, { observes } from "discourse-common/utils/decorators"; import { action } from "@ember/object"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { gt } from "@ember/object/computed"; import { popupAjaxError } from "discourse/lib/ajax-error"; @@ -19,15 +19,9 @@ export default Controller.extend({ showActions: false, @observes("filterInput") - _setFilter() { - discourseDebounce( - this, - function () { - this.set("filter", this.filterInput); - }, - 500 - ); - }, + _setFilter: discourseDebounce(function () { + this.set("filter", this.filterInput); + }, 500), @observes("order", "asc", "filter") _filtersChanged() { diff --git a/app/assets/javascripts/discourse/app/controllers/group-requests.js b/app/assets/javascripts/discourse/app/controllers/group-requests.js index 73da3601920..791da19ee0c 100644 --- a/app/assets/javascripts/discourse/app/controllers/group-requests.js +++ b/app/assets/javascripts/discourse/app/controllers/group-requests.js @@ -1,7 +1,7 @@ import Controller, { inject as controller } from "@ember/controller"; import discourseComputed, { observes } from "discourse-common/utils/decorators"; import { ajax } from "discourse/lib/ajax"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { popupAjaxError } from "discourse/lib/ajax-error"; export default Controller.extend({ @@ -17,15 +17,9 @@ export default Controller.extend({ loading: false, @observes("filterInput") - _setFilter() { - discourseDebounce( - this, - function () { - this.set("filter", this.filterInput); - }, - 500 - ); - }, + _setFilter: discourseDebounce(function () { + this.set("filter", this.filterInput); + }, 500), @observes("order", "desc", "filter") _filtersChanged() { diff --git a/app/assets/javascripts/discourse/app/controllers/groups-index.js b/app/assets/javascripts/discourse/app/controllers/groups-index.js index fdc3f909001..a68dbedf58a 100644 --- a/app/assets/javascripts/discourse/app/controllers/groups-index.js +++ b/app/assets/javascripts/discourse/app/controllers/groups-index.js @@ -2,8 +2,8 @@ import Controller, { inject as controller } from "@ember/controller"; import I18n from "I18n"; import { INPUT_DELAY } from "discourse-common/config/environment"; import { action } from "@ember/object"; +import { debounce } from "@ember/runloop"; import discourseComputed from "discourse-common/utils/decorators"; -import discourseDebounce from "discourse-common/lib/debounce"; export default Controller.extend({ application: controller(), @@ -45,7 +45,7 @@ export default Controller.extend({ @action onFilterChanged(filter) { - discourseDebounce(this, this._debouncedFilter, filter, INPUT_DELAY); + debounce(this, this._debouncedFilter, filter, INPUT_DELAY); }, @action diff --git a/app/assets/javascripts/discourse/app/controllers/insert-hyperlink.js b/app/assets/javascripts/discourse/app/controllers/insert-hyperlink.js index 42c3deeb323..3d4b3babadd 100644 --- a/app/assets/javascripts/discourse/app/controllers/insert-hyperlink.js +++ b/app/assets/javascripts/discourse/app/controllers/insert-hyperlink.js @@ -1,8 +1,7 @@ -import { cancel, schedule } from "@ember/runloop"; +import { cancel, debounce, schedule } from "@ember/runloop"; import Controller from "@ember/controller"; import ModalFunctionality from "discourse/mixins/modal-functionality"; import { bind } from "discourse-common/utils/decorators"; -import discourseDebounce from "discourse-common/lib/debounce"; import { isEmpty } from "@ember/utils"; import { prefixProtocol } from "discourse/lib/url"; import { searchForTerm } from "discourse/lib/search"; @@ -178,7 +177,7 @@ export default Controller.extend(ModalFunctionality, { } }, search() { - this._debounced = discourseDebounce(this, this.triggerSearch, 400); + this._debounced = debounce(this, this.triggerSearch, 400); }, }, }); diff --git a/app/assets/javascripts/discourse/app/controllers/topic.js b/app/assets/javascripts/discourse/app/controllers/topic.js index 4e714b7c1d1..996e5def746 100644 --- a/app/assets/javascripts/discourse/app/controllers/topic.js +++ b/app/assets/javascripts/discourse/app/controllers/topic.js @@ -18,7 +18,7 @@ import bootbox from "bootbox"; import { bufferedProperty } from "discourse/mixins/buffered-content"; import { buildQuote } from "discourse/lib/quote"; import { deepMerge } from "discourse-common/lib/object"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { escapeExpression } from "discourse/lib/utilities"; import { extractLinkMeta } from "discourse/lib/render-topic-featured-link"; import isElementInViewport from "discourse/lib/is-element-in-viewport"; @@ -1517,22 +1517,15 @@ export default Controller.extend(bufferedProperty("model"), { ); }, - _scrollToPost(postNumber) { - discourseDebounce( - this, - function () { - const $post = $(`.topic-post article#post_${postNumber}`); + _scrollToPost: discourseDebounce(function (postNumber) { + const $post = $(`.topic-post article#post_${postNumber}`); - if ($post.length === 0 || isElementInViewport($post)) { - return; - } + if ($post.length === 0 || isElementInViewport($post)) { + return; + } - $("html, body").animate({ scrollTop: $post.offset().top }, 1000); - }, - postNumber, - 500 - ); - }, + $("html, body").animate({ scrollTop: $post.offset().top }, 1000); + }, 500), unsubscribe() { // never unsubscribe when navigating from topic to topic diff --git a/app/assets/javascripts/discourse/app/controllers/user-invited-show.js b/app/assets/javascripts/discourse/app/controllers/user-invited-show.js index eb933835358..9781e87007b 100644 --- a/app/assets/javascripts/discourse/app/controllers/user-invited-show.js +++ b/app/assets/javascripts/discourse/app/controllers/user-invited-show.js @@ -5,7 +5,7 @@ import I18n from "I18n"; import { INPUT_DELAY } from "discourse-common/config/environment"; import Invite from "discourse/models/invite"; import bootbox from "bootbox"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { popupAjaxError } from "discourse/lib/ajax-error"; export default Controller.extend({ @@ -26,19 +26,13 @@ export default Controller.extend({ }, @observes("searchTerm") - _searchTermChanged() { - discourseDebounce( - this, - function () { - Invite.findInvitedBy( - this.user, - this.filter, - this.searchTerm - ).then((invites) => this.set("model", invites)); - }, - INPUT_DELAY - ); - }, + _searchTermChanged: discourseDebounce(function () { + Invite.findInvitedBy( + this.user, + this.filter, + this.searchTerm + ).then((invites) => this.set("model", invites)); + }, INPUT_DELAY), inviteRedeemed: equal("filter", "redeemed"), invitePending: equal("filter", "pending"), diff --git a/app/assets/javascripts/discourse/app/controllers/users.js b/app/assets/javascripts/discourse/app/controllers/users.js index 8ebaf7adf67..a0b180a1644 100644 --- a/app/assets/javascripts/discourse/app/controllers/users.js +++ b/app/assets/javascripts/discourse/app/controllers/users.js @@ -1,5 +1,5 @@ import Controller, { inject as controller } from "@ember/controller"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { equal } from "@ember/object/computed"; import { longDate } from "discourse/lib/formatter"; import { observes } from "discourse-common/utils/decorators"; @@ -37,15 +37,9 @@ export default Controller.extend({ }, @observes("nameInput") - _setName() { - discourseDebounce( - this, - function () { - this.set("name", this.nameInput); - }, - 500 - ); - }, + _setName: discourseDebounce(function () { + this.set("name", this.nameInput); + }, 500), @observes("model.canLoadMore") _showFooter: function () { diff --git a/app/assets/javascripts/discourse/app/lib/autocomplete.js b/app/assets/javascripts/discourse/app/lib/autocomplete.js index a5aab455753..f08c7851d2a 100644 --- a/app/assets/javascripts/discourse/app/lib/autocomplete.js +++ b/app/assets/javascripts/discourse/app/lib/autocomplete.js @@ -1,8 +1,7 @@ -import { cancel, later } from "@ember/runloop"; +import { cancel, debounce, later } from "@ember/runloop"; import { caretPosition, setCaretPosition } from "discourse/lib/utilities"; import { INPUT_DELAY } from "discourse-common/config/environment"; import Site from "discourse/models/site"; -import discourseDebounce from "discourse-common/lib/debounce"; import { iconHTML } from "discourse-common/lib/icon-library"; /** @@ -423,7 +422,7 @@ export default function (options) { $(this).on("keyup.autocomplete", function (e) { if (options.debounced) { - discourseDebounce(this, performAutocomplete, e, INPUT_DELAY); + debounce(this, performAutocomplete, e, INPUT_DELAY); } else { performAutocomplete(e); } diff --git a/app/assets/javascripts/discourse/app/lib/category-tag-search.js b/app/assets/javascripts/discourse/app/lib/category-tag-search.js index d2e1fd5c83f..95fcdab9e05 100644 --- a/app/assets/javascripts/discourse/app/lib/category-tag-search.js +++ b/app/assets/javascripts/discourse/app/lib/category-tag-search.js @@ -4,7 +4,7 @@ import Category from "discourse/models/category"; import { Promise } from "rsvp"; import { SEPARATOR } from "discourse/lib/category-hashtags"; import { TAG_HASHTAG_POSTFIX } from "discourse/lib/tag-hashtags"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import getURL from "discourse-common/lib/get-url"; import { isTesting } from "discourse-common/config/environment"; @@ -27,47 +27,38 @@ function searchTags(term, categories, limit) { isTesting() ? 50 : 5000 ); - const debouncedSearch = (q, cats, resultFunc) => { - discourseDebounce( - this, - function () { - oldSearch = $.ajax(getURL("/tags/filter/search"), { - type: "GET", - cache: true, - data: { limit: limit, q }, + const debouncedSearch = discourseDebounce((q, cats, resultFunc) => { + oldSearch = $.ajax(getURL("/tags/filter/search"), { + type: "GET", + cache: true, + data: { limit: limit, q }, + }); + + var returnVal = CANCELLED_STATUS; + + oldSearch + .then((r) => { + const categoryNames = cats.map((c) => c.model.get("name")); + + const tags = r.results.map((tag) => { + const tagName = tag.text; + + return { + name: tagName, + text: categoryNames.includes(tagName) + ? `${tagName}${TAG_HASHTAG_POSTFIX}` + : tagName, + count: tag.count, + }; }); - var returnVal = CANCELLED_STATUS; - - oldSearch - .then((r) => { - const categoryNames = cats.map((c) => c.model.get("name")); - - const tags = r.results.map((tag) => { - const tagName = tag.text; - - return { - name: tagName, - text: categoryNames.includes(tagName) - ? `${tagName}${TAG_HASHTAG_POSTFIX}` - : tagName, - count: tag.count, - }; - }); - - returnVal = cats.concat(tags); - }) - .always(() => { - oldSearch = null; - resultFunc(returnVal); - }); - }, - q, - cats, - resultFunc, - 300 - ); - }; + returnVal = cats.concat(tags); + }) + .always(() => { + oldSearch = null; + resultFunc(returnVal); + }); + }, 300); debouncedSearch(term, categories, (result) => { cancel(clearPromise); diff --git a/app/assets/javascripts/discourse/app/lib/reports-loader.js b/app/assets/javascripts/discourse/app/lib/reports-loader.js index afd0b132ee6..e0b108b7c7b 100644 --- a/app/assets/javascripts/discourse/app/lib/reports-loader.js +++ b/app/assets/javascripts/discourse/app/lib/reports-loader.js @@ -1,5 +1,6 @@ import { ajax } from "discourse/lib/ajax"; -import discourseDebounce from "discourse-common/lib/debounce"; +import { run } from "@ember/runloop"; +const { debounce } = run; let _queue = []; let _processing = 0; @@ -32,7 +33,7 @@ export default { _queue.push({ runnable: () => callback, type, params }); - discourseDebounce(this, this._processQueue, DEBOUNCING_DELAY); + debounce(this, this._processQueue, DEBOUNCING_DELAY); }, _processQueue() { @@ -49,7 +50,7 @@ export default { // if queue has still jobs after splice, we request a future processing if (_queue.length > 0) { - discourseDebounce(this, this._processQueue, DEBOUNCING_DELAY); + debounce(this, this._processQueue, DEBOUNCING_DELAY); } let reports = {}; @@ -78,7 +79,7 @@ export default { .finally(() => { _processing--; - discourseDebounce(this, this._processQueue, DEBOUNCING_DELAY); + debounce(this, this._processQueue, DEBOUNCING_DELAY); }); }, diff --git a/app/assets/javascripts/discourse/app/lib/safari-hacks.js b/app/assets/javascripts/discourse/app/lib/safari-hacks.js index 99c8b335e1f..7ecdb18b89c 100644 --- a/app/assets/javascripts/discourse/app/lib/safari-hacks.js +++ b/app/assets/javascripts/discourse/app/lib/safari-hacks.js @@ -3,7 +3,7 @@ import { safariHacksDisabled, } from "discourse/lib/utilities"; import { INPUT_DELAY } from "discourse-common/config/environment"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { helperContext } from "discourse-common/lib/helpers"; import { later } from "@ember/runloop"; @@ -145,9 +145,7 @@ function positioningWorkaround($fixedElement) { positioningWorkaround.blur(evt); }; - var blurred = function (evt) { - discourseDebounce(this, blurredNow, evt, INPUT_DELAY); - }; + var blurred = discourseDebounce(blurredNow, INPUT_DELAY); var positioningHack = function (evt) { let _this = this; @@ -216,19 +214,13 @@ function positioningWorkaround($fixedElement) { } } - const checkForInputs = function () { - discourseDebounce( - this, - function () { - attachTouchStart(fixedElement, lastTouched); + const checkForInputs = discourseDebounce(function () { + attachTouchStart(fixedElement, lastTouched); - $fixedElement.find("input[type=text],textarea").each(function () { - attachTouchStart(this, positioningHack); - }); - }, - 100 - ); - }; + $fixedElement.find("input[type=text],textarea").each(function () { + attachTouchStart(this, positioningHack); + }); + }, 100); positioningWorkaround.touchstartEvent = function (element) { var triggerHack = positioningHack.bind(element); diff --git a/app/assets/javascripts/discourse/app/lib/user-search.js b/app/assets/javascripts/discourse/app/lib/user-search.js index c77c5c6cd79..b68e0755e8a 100644 --- a/app/assets/javascripts/discourse/app/lib/user-search.js +++ b/app/assets/javascripts/discourse/app/lib/user-search.js @@ -1,7 +1,7 @@ import { cancel, later } from "@ember/runloop"; import { CANCELLED_STATUS } from "discourse/lib/autocomplete"; import { Promise } from "rsvp"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { emailValid } from "discourse/lib/utilities"; import { isTesting } from "discourse-common/config/environment"; import { userPath } from "discourse/lib/url"; @@ -81,32 +81,7 @@ function performSearch( }); } -var debouncedSearch = function ( - term, - topicId, - categoryId, - includeGroups, - includeMentionableGroups, - includeMessageableGroups, - allowedUsers, - groupMembersOf, - resultsFn -) { - discourseDebounce( - this, - performSearch, - term, - topicId, - categoryId, - includeGroups, - includeMentionableGroups, - includeMessageableGroups, - allowedUsers, - groupMembersOf, - resultsFn, - 300 - ); -}; +var debouncedSearch = discourseDebounce(performSearch, 300); function organizeResults(r, options) { if (r === CANCELLED_STATUS) { diff --git a/app/assets/javascripts/discourse/app/mixins/docking.js b/app/assets/javascripts/discourse/app/mixins/docking.js index fee3e73b26e..4147ae94de7 100644 --- a/app/assets/javascripts/discourse/app/mixins/docking.js +++ b/app/assets/javascripts/discourse/app/mixins/docking.js @@ -1,6 +1,5 @@ +import { debounce, later } from "@ember/runloop"; import Mixin from "@ember/object/mixin"; -import discourseDebounce from "discourse-common/lib/debounce"; -import { later } from "@ember/runloop"; const helper = { offset() { @@ -16,7 +15,7 @@ export default Mixin.create({ init() { this._super(...arguments); this.queueDockCheck = () => { - discourseDebounce(this, this.safeDockCheck, 5); + debounce(this, this.safeDockCheck, 5); }; }, diff --git a/app/assets/javascripts/discourse/app/mixins/mobile-scroll-direction.js b/app/assets/javascripts/discourse/app/mixins/mobile-scroll-direction.js index f751216c97d..2be71c0d636 100644 --- a/app/assets/javascripts/discourse/app/mixins/mobile-scroll-direction.js +++ b/app/assets/javascripts/discourse/app/mixins/mobile-scroll-direction.js @@ -1,5 +1,5 @@ import Mixin from "@ember/object/mixin"; -import discourseDebounce from "discourse-common/lib/debounce"; +import { debounce } from "@ember/runloop"; // Small buffer so that very tiny scrolls don't trigger mobile header switch const MOBILE_SCROLL_TOLERANCE = 5; @@ -47,13 +47,9 @@ export default Mixin.create({ // If the user reaches the very bottom of the topic, we only want to reset // this scroll direction after a second scrolldown. This is a nicer event // similar to what Safari and Chrome do. - discourseDebounce( - this, - function () { - this._bottomHit = 1; - }, - 1000 - ); + debounce(() => { + this._bottomHit = 1; + }, 1000); if (this._bottomHit === 1) { this.set("mobileScrollDirection", null); diff --git a/app/assets/javascripts/discourse/app/mixins/scrolling.js b/app/assets/javascripts/discourse/app/mixins/scrolling.js index 26a16f0a7c3..f1df25463a7 100644 --- a/app/assets/javascripts/discourse/app/mixins/scrolling.js +++ b/app/assets/javascripts/discourse/app/mixins/scrolling.js @@ -1,5 +1,5 @@ import Mixin from "@ember/object/mixin"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { scheduleOnce } from "@ember/runloop"; import { inject as service } from "@ember/service"; @@ -45,9 +45,7 @@ const Scrolling = Mixin.create({ }; if (opts.debounce) { - onScrollMethod = () => { - discourseDebounce(this, onScrollMethod, opts.debounce); - }; + onScrollMethod = discourseDebounce(onScrollMethod, opts.debounce); } ScrollingDOMMethods.bindOnScroll(onScrollMethod, opts.name); diff --git a/app/assets/javascripts/discourse/app/mixins/username-validation.js b/app/assets/javascripts/discourse/app/mixins/username-validation.js index 0749bd70f9f..9d26b91cea9 100644 --- a/app/assets/javascripts/discourse/app/mixins/username-validation.js +++ b/app/assets/javascripts/discourse/app/mixins/username-validation.js @@ -3,7 +3,7 @@ import I18n from "I18n"; import Mixin from "@ember/object/mixin"; import User from "discourse/models/user"; import discourseComputed from "discourse-common/utils/decorators"; -import discourseDebounce from "discourse-common/lib/debounce"; +import discourseDebounce from "discourse/lib/debounce"; import { isEmpty } from "@ember/utils"; import { setting } from "discourse/lib/computed"; @@ -14,26 +14,20 @@ export default Mixin.create({ minUsernameLength: setting("min_username_length"), - fetchExistingUsername() { - discourseDebounce( - this, - function () { - User.checkUsername(null, this.accountEmail).then((result) => { - if ( - result.suggestion && - (isEmpty(this.accountUsername) || - this.accountUsername === this.get("authOptions.username")) - ) { - this.setProperties({ - accountUsername: result.suggestion, - prefilledUsername: result.suggestion, - }); - } + fetchExistingUsername: discourseDebounce(function () { + User.checkUsername(null, this.accountEmail).then((result) => { + if ( + result.suggestion && + (isEmpty(this.accountUsername) || + this.accountUsername === this.get("authOptions.username")) + ) { + this.setProperties({ + accountUsername: result.suggestion, + prefilledUsername: result.suggestion, }); - }, - 500 - ); - }, + } + }); + }, 500), @discourseComputed("accountUsername") basicUsernameValidation(accountUsername) { @@ -93,61 +87,54 @@ export default Mixin.create({ ); }, - checkUsernameAvailability() { - discourseDebounce( - this, - function () { - if (this.shouldCheckUsernameAvailability()) { - return User.checkUsername( - this.accountUsername, - this.accountEmail - ).then((result) => { - this.set("isDeveloper", false); - if (result.available) { - if (result.is_developer) { - this.set("isDeveloper", true); - } + checkUsernameAvailability: discourseDebounce(function () { + if (this.shouldCheckUsernameAvailability()) { + return User.checkUsername(this.accountUsername, this.accountEmail).then( + (result) => { + this.set("isDeveloper", false); + if (result.available) { + if (result.is_developer) { + this.set("isDeveloper", true); + } + return this.set( + "uniqueUsernameValidation", + EmberObject.create({ + ok: true, + reason: I18n.t("user.username.available"), + }) + ); + } else { + const failedAttrs = { + failed: true, + element: document.querySelector("#new-account-username"), + }; + + if (result.suggestion) { return this.set( "uniqueUsernameValidation", - EmberObject.create({ - ok: true, - reason: I18n.t("user.username.available"), - }) + EmberObject.create( + Object.assign(failedAttrs, { + reason: I18n.t("user.username.not_available", result), + }) + ) ); } else { - const failedAttrs = { - failed: true, - element: document.querySelector("#new-account-username"), - }; - - if (result.suggestion) { - return this.set( - "uniqueUsernameValidation", - EmberObject.create( - Object.assign(failedAttrs, { - reason: I18n.t("user.username.not_available", result), - }) - ) - ); - } else { - return this.set( - "uniqueUsernameValidation", - EmberObject.create( - Object.assign(failedAttrs, { - reason: result.errors - ? result.errors.join(" ") - : I18n.t("user.username.not_available_no_suggestion"), - }) - ) - ); - } + return this.set( + "uniqueUsernameValidation", + EmberObject.create( + Object.assign(failedAttrs, { + reason: result.errors + ? result.errors.join(" ") + : I18n.t("user.username.not_available_no_suggestion"), + }) + ) + ); } - }); + } } - }, - 500 - ); - }, + ); + } + }, 500), // Actually wait for the async name check before we're 100% sure we're good to go @discourseComputed("uniqueUsernameValidation", "basicUsernameValidation") diff --git a/app/assets/javascripts/discourse/app/widgets/post-stream.js b/app/assets/javascripts/discourse/app/widgets/post-stream.js index 4ab76571105..be644e88c8f 100644 --- a/app/assets/javascripts/discourse/app/widgets/post-stream.js +++ b/app/assets/javascripts/discourse/app/widgets/post-stream.js @@ -4,7 +4,7 @@ import { Placeholder } from "discourse/lib/posts-with-placeholders"; import { addWidgetCleanCallback } from "discourse/components/mount-widget"; import { avatarFor } from "discourse/widgets/post"; import { createWidget } from "discourse/widgets/widget"; -import discourseDebounce from "discourse-common/lib/debounce"; +import { debounce } from "@ember/runloop"; import { h } from "virtual-dom"; import { iconNode } from "discourse-common/lib/icon-library"; import { isTesting } from "discourse-common/config/environment"; @@ -46,7 +46,7 @@ export function cloak(post, component) { _heights[post.id] = $post.outerHeight(); component.dirtyKeys.keyDirty(`post-${post.id}`); - discourseDebounce(component, "queueRerender", 1000); + debounce(component, "queueRerender", 1000); } export function uncloak(post, component) { diff --git a/app/assets/javascripts/discourse/app/widgets/search-menu.js b/app/assets/javascripts/discourse/app/widgets/search-menu.js index 28d8ce506b4..999e063742e 100644 --- a/app/assets/javascripts/discourse/app/widgets/search-menu.js +++ b/app/assets/javascripts/discourse/app/widgets/search-menu.js @@ -1,7 +1,7 @@ import { isValidSearchTerm, searchForTerm } from "discourse/lib/search"; import DiscourseURL from "discourse/lib/url"; import { createWidget } from "discourse/widgets/widget"; -import discourseDebounce from "discourse-common/lib/debounce"; +import { debounce } from "@ember/runloop"; import { get } from "@ember/object"; import getURL from "discourse-common/lib/get-url"; import { h } from "virtual-dom"; @@ -284,7 +284,7 @@ export default createWidget("search-menu", { searchData.noResults = false; this.searchService().set("highlightTerm", searchData.term); searchData.loading = true; - discourseDebounce(SearchHelper, SearchHelper.perform, this, 400); + debounce(SearchHelper, SearchHelper.perform, this, 400); }, moreOfType(type) { diff --git a/app/assets/javascripts/pretty-text/addon/upload-short-url.js b/app/assets/javascripts/pretty-text/addon/upload-short-url.js index febcbaf3198..5080a52ec8d 100644 --- a/app/assets/javascripts/pretty-text/addon/upload-short-url.js +++ b/app/assets/javascripts/pretty-text/addon/upload-short-url.js @@ -1,5 +1,5 @@ import I18n from "I18n"; -import discourseDebounce from "discourse-common/lib/debounce"; +import { debounce } from "@ember/runloop"; let _cache = {}; @@ -184,7 +184,7 @@ export function resolveAllShortUrls(ajax, siteSettings, scope, opts) { shortUploadElements = scope.querySelectorAll(attributes); if (shortUploadElements.length > 0) { // this is carefully batched so we can do a leading debounce (trigger right away) - return discourseDebounce( + return debounce( null, _loadShortUrls, shortUploadElements, diff --git a/app/assets/javascripts/select-kit/addon/components/select-kit.js b/app/assets/javascripts/select-kit/addon/components/select-kit.js index 54fbe4100b8..a440f8d0967 100644 --- a/app/assets/javascripts/select-kit/addon/components/select-kit.js +++ b/app/assets/javascripts/select-kit/addon/components/select-kit.js @@ -3,7 +3,14 @@ import PluginApiMixin, { applyContentPluginApiCallbacks, applyOnChangePluginApiCallbacks, } from "select-kit/mixins/plugin-api"; -import { bind, cancel, next, schedule, throttle } from "@ember/runloop"; +import { + bind, + cancel, + debounce, + next, + schedule, + throttle, +} from "@ember/runloop"; import { isEmpty, isNone, isPresent } from "@ember/utils"; import Component from "@ember/component"; import I18n from "I18n"; @@ -12,7 +19,6 @@ import { Promise } from "rsvp"; import UtilsMixin from "select-kit/mixins/utils"; import { createPopper } from "@popperjs/core"; import deprecated from "discourse-common/lib/deprecated"; -import discourseDebounce from "discourse-common/lib/debounce"; import { guidFor } from "@ember/object/internals"; import { makeArray } from "discourse-common/lib/helpers"; @@ -380,7 +386,7 @@ export default Component.extend( cancel(this._searchPromise); } - discourseDebounce(this, this._debouncedInput, event.target.value, 200); + debounce(this, this._debouncedInput, event.target.value, 200); }, _debouncedInput(filter) { diff --git a/plugins/discourse-local-dates/assets/javascripts/discourse/components/discourse-local-dates-create-form.js.es6 b/plugins/discourse-local-dates/assets/javascripts/discourse/components/discourse-local-dates-create-form.js.es6 index 454bf9ea9a5..581b5027813 100644 --- a/plugins/discourse-local-dates/assets/javascripts/discourse/components/discourse-local-dates-create-form.js.es6 +++ b/plugins/discourse-local-dates/assets/javascripts/discourse/components/discourse-local-dates-create-form.js.es6 @@ -6,7 +6,7 @@ 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 discourseDebounce from "discourse/lib/debounce"; import { isEmpty } from "@ember/utils"; import loadScript from "discourse/lib/load-script"; import { notEmpty } from "@ember/object/computed"; @@ -56,24 +56,18 @@ export default Component.extend({ }, @observes("markup") - _renderPreview() { - discourseDebounce( - this, - function () { - const markup = this.markup; + _renderPreview: discourseDebounce(function () { + const markup = this.markup; - if (markup) { - cookAsync(markup).then((result) => { - this.set("currentPreview", result); - schedule("afterRender", () => - this.$(".preview .discourse-local-date").applyLocalDates() - ); - }); - } - }, - INPUT_DELAY - ); - }, + if (markup) { + cookAsync(markup).then((result) => { + this.set("currentPreview", result); + schedule("afterRender", () => + this.$(".preview .discourse-local-date").applyLocalDates() + ); + }); + } + }, INPUT_DELAY), @computed("date", "toDate", "toTime") isRange(date, toDate, toTime) {