From 184ec95d01a7bb1bb9ed3515094107e8d9b2c5fc Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Tue, 21 Jan 2025 11:24:33 +0100 Subject: [PATCH] DEV: Don't nest deferring calls (#30449) Each case simplified: `next(() => later(() => ...))` -> "wait 0 ms then wait X ms" `next(() => debounce(() => ...))` -> "wait 0 ms then wait X ms (debounced)" `next(() => scheduleAfter("render", ...))` -> "in the next (empty) run loop, do the thing (after a no-op render step)" --- .../app/components/composer-editor.js | 14 +++---- .../app/services/route-scroll-manager.js | 8 +--- .../app/services/scroll-direction.js | 9 ++--- .../select-kit/addon/components/select-kit.js | 12 +++--- .../discourse/lib/textarea-interactor.js | 38 +++++++++---------- 5 files changed, 33 insertions(+), 48 deletions(-) diff --git a/app/assets/javascripts/discourse/app/components/composer-editor.js b/app/assets/javascripts/discourse/app/components/composer-editor.js index 5865d89ff66..8e76b30d93e 100644 --- a/app/assets/javascripts/discourse/app/components/composer-editor.js +++ b/app/assets/javascripts/discourse/app/components/composer-editor.js @@ -1,7 +1,7 @@ import Component from "@ember/component"; import EmberObject, { action, computed } from "@ember/object"; import { getOwner } from "@ember/owner"; -import { next, schedule, throttle } from "@ember/runloop"; +import { schedule, throttle } from "@ember/runloop"; import { service } from "@ember/service"; import { classNameBindings } from "@ember-decorators/component"; import { observes, on } from "@ember-decorators/object"; @@ -803,13 +803,11 @@ export default class ComposerEditor extends Component { this.appEvents.trigger(`${this.composerEventPrefix}:will-close`); - next(() => { - // need to wait a bit for the "slide down" transition of the composer - discourseLater( - () => this.appEvents.trigger(`${this.composerEventPrefix}:closed`), - 400 - ); - }); + // need to wait a bit for the "slide down" transition of the composer + discourseLater( + () => this.appEvents.trigger(`${this.composerEventPrefix}:closed`), + 400 + ); preview?.removeEventListener("click", this._handleAltTextCancelButtonClick); preview?.removeEventListener("click", this._handleAltTextEditButtonClick); diff --git a/app/assets/javascripts/discourse/app/services/route-scroll-manager.js b/app/assets/javascripts/discourse/app/services/route-scroll-manager.js index a615f600cd3..d121d3058a3 100644 --- a/app/assets/javascripts/discourse/app/services/route-scroll-manager.js +++ b/app/assets/javascripts/discourse/app/services/route-scroll-manager.js @@ -1,4 +1,4 @@ -import { next, schedule } from "@ember/runloop"; +import { next } from "@ember/runloop"; import Service, { service } from "@ember/service"; import { bind } from "discourse/lib/decorators"; import { isTesting } from "discourse/lib/environment"; @@ -55,11 +55,7 @@ export default class RouteScrollManager extends Service { const scrollLocation = this.historyStore.get(STORE_KEY) || [0, 0]; - next(() => - schedule("afterRender", () => - this.scrollElement.scrollTo(...scrollLocation) - ) - ); + next(() => this.scrollElement.scrollTo(...scrollLocation)); } #shouldScroll(routeInfo) { diff --git a/app/assets/javascripts/discourse/app/services/scroll-direction.js b/app/assets/javascripts/discourse/app/services/scroll-direction.js index 0882915f43c..af78d327b77 100644 --- a/app/assets/javascripts/discourse/app/services/scroll-direction.js +++ b/app/assets/javascripts/discourse/app/services/scroll-direction.js @@ -1,5 +1,5 @@ import { tracked } from "@glimmer/tracking"; -import { next, throttle } from "@ember/runloop"; +import { throttle } from "@ember/runloop"; import Service, { service } from "@ember/service"; import discourseDebounce from "discourse/lib/debounce"; import { bind } from "discourse/lib/decorators"; @@ -49,11 +49,8 @@ export default class ScrollDirection extends Service { // User hasn't scrolled yet on this route this.lastScrollDirection = UNSCROLLED; - // Wait for the initial DOM render to be done - next(() => { - // Then allow a bit of extra time for any DOM shifts to settle - discourseDebounce(this.unpause, PAUSE_AFTER_TRANSITION_MS); - }); + // Allow a bit of extra time for any DOM shifts to settle + discourseDebounce(this.unpause, PAUSE_AFTER_TRANSITION_MS); } @bind 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 20981bf52ca..b7fa22917a6 100644 --- a/app/assets/javascripts/select-kit/addon/components/select-kit.js +++ b/app/assets/javascripts/select-kit/addon/components/select-kit.js @@ -1,7 +1,7 @@ import Component from "@ember/component"; import EmberObject, { computed, get } from "@ember/object"; import { guidFor } from "@ember/object/internals"; -import { bind, cancel, next, schedule, throttle } from "@ember/runloop"; +import { bind, cancel, next, throttle } from "@ember/runloop"; import { service } from "@ember/service"; import { isEmpty, isNone, isPresent } from "@ember/utils"; import { @@ -760,13 +760,11 @@ export default class SelectKit extends Component.extend(UtilsMixin) { _safeAfterRender(fn) { next(() => { - schedule("afterRender", () => { - if (!this.element || this.isDestroyed || this.isDestroying) { - return; - } + if (!this.element || this.isDestroyed || this.isDestroying) { + return; + } - fn(); - }); + fn(); }); } diff --git a/plugins/chat/assets/javascripts/discourse/lib/textarea-interactor.js b/plugins/chat/assets/javascripts/discourse/lib/textarea-interactor.js index efb792ed59b..a11578ae871 100644 --- a/plugins/chat/assets/javascripts/discourse/lib/textarea-interactor.js +++ b/plugins/chat/assets/javascripts/discourse/lib/textarea-interactor.js @@ -49,37 +49,33 @@ export default class TextareaInteractor extends EmberObject { @bind blur() { next(() => { - schedule("afterRender", () => { - this.textarea.blur(); - }); + this.textarea.blur(); }); } @bind focus(opts = { ensureAtEnd: false, refreshHeight: true, addText: null }) { next(() => { - schedule("afterRender", () => { - if (opts.refreshHeight) { - this.refreshHeight(); - } + if (opts.refreshHeight) { + this.refreshHeight(); + } - if (opts.ensureAtEnd) { - this.ensureCaretAtEnd(); - } + if (opts.ensureAtEnd) { + this.ensureCaretAtEnd(); + } - if (this.capabilities.isIpadOS || this.site.mobileView) { - return; - } + if (this.capabilities.isIpadOS || this.site.mobileView) { + return; + } - if (opts.addText) { - this.textManipulation.addText( - this.textManipulation.getSelected(), - opts.addText - ); - } + if (opts.addText) { + this.textManipulation.addText( + this.textManipulation.getSelected(), + opts.addText + ); + } - this.textManipulation.blurAndFocus(); - }); + this.textManipulation.blurAndFocus(); }); }