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)"
This commit is contained in:
Jarek Radosz 2025-01-21 11:24:33 +01:00 committed by GitHub
parent db998ee1ab
commit 184ec95d01
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 33 additions and 48 deletions

View File

@ -1,7 +1,7 @@
import Component from "@ember/component"; import Component from "@ember/component";
import EmberObject, { action, computed } from "@ember/object"; import EmberObject, { action, computed } from "@ember/object";
import { getOwner } from "@ember/owner"; import { getOwner } from "@ember/owner";
import { next, schedule, throttle } from "@ember/runloop"; import { schedule, throttle } from "@ember/runloop";
import { service } from "@ember/service"; import { service } from "@ember/service";
import { classNameBindings } from "@ember-decorators/component"; import { classNameBindings } from "@ember-decorators/component";
import { observes, on } from "@ember-decorators/object"; import { observes, on } from "@ember-decorators/object";
@ -803,13 +803,11 @@ export default class ComposerEditor extends Component {
this.appEvents.trigger(`${this.composerEventPrefix}:will-close`); this.appEvents.trigger(`${this.composerEventPrefix}:will-close`);
next(() => { // need to wait a bit for the "slide down" transition of the composer
// need to wait a bit for the "slide down" transition of the composer discourseLater(
discourseLater( () => this.appEvents.trigger(`${this.composerEventPrefix}:closed`),
() => this.appEvents.trigger(`${this.composerEventPrefix}:closed`), 400
400 );
);
});
preview?.removeEventListener("click", this._handleAltTextCancelButtonClick); preview?.removeEventListener("click", this._handleAltTextCancelButtonClick);
preview?.removeEventListener("click", this._handleAltTextEditButtonClick); preview?.removeEventListener("click", this._handleAltTextEditButtonClick);

View File

@ -1,4 +1,4 @@
import { next, schedule } from "@ember/runloop"; import { next } from "@ember/runloop";
import Service, { service } from "@ember/service"; import Service, { service } from "@ember/service";
import { bind } from "discourse/lib/decorators"; import { bind } from "discourse/lib/decorators";
import { isTesting } from "discourse/lib/environment"; 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]; const scrollLocation = this.historyStore.get(STORE_KEY) || [0, 0];
next(() => next(() => this.scrollElement.scrollTo(...scrollLocation));
schedule("afterRender", () =>
this.scrollElement.scrollTo(...scrollLocation)
)
);
} }
#shouldScroll(routeInfo) { #shouldScroll(routeInfo) {

View File

@ -1,5 +1,5 @@
import { tracked } from "@glimmer/tracking"; import { tracked } from "@glimmer/tracking";
import { next, throttle } from "@ember/runloop"; import { throttle } from "@ember/runloop";
import Service, { service } from "@ember/service"; import Service, { service } from "@ember/service";
import discourseDebounce from "discourse/lib/debounce"; import discourseDebounce from "discourse/lib/debounce";
import { bind } from "discourse/lib/decorators"; import { bind } from "discourse/lib/decorators";
@ -49,11 +49,8 @@ export default class ScrollDirection extends Service {
// User hasn't scrolled yet on this route // User hasn't scrolled yet on this route
this.lastScrollDirection = UNSCROLLED; this.lastScrollDirection = UNSCROLLED;
// Wait for the initial DOM render to be done // Allow a bit of extra time for any DOM shifts to settle
next(() => { discourseDebounce(this.unpause, PAUSE_AFTER_TRANSITION_MS);
// Then allow a bit of extra time for any DOM shifts to settle
discourseDebounce(this.unpause, PAUSE_AFTER_TRANSITION_MS);
});
} }
@bind @bind

View File

@ -1,7 +1,7 @@
import Component from "@ember/component"; import Component from "@ember/component";
import EmberObject, { computed, get } from "@ember/object"; import EmberObject, { computed, get } from "@ember/object";
import { guidFor } from "@ember/object/internals"; 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 { service } from "@ember/service";
import { isEmpty, isNone, isPresent } from "@ember/utils"; import { isEmpty, isNone, isPresent } from "@ember/utils";
import { import {
@ -760,13 +760,11 @@ export default class SelectKit extends Component.extend(UtilsMixin) {
_safeAfterRender(fn) { _safeAfterRender(fn) {
next(() => { next(() => {
schedule("afterRender", () => { if (!this.element || this.isDestroyed || this.isDestroying) {
if (!this.element || this.isDestroyed || this.isDestroying) { return;
return; }
}
fn(); fn();
});
}); });
} }

View File

@ -49,37 +49,33 @@ export default class TextareaInteractor extends EmberObject {
@bind @bind
blur() { blur() {
next(() => { next(() => {
schedule("afterRender", () => { this.textarea.blur();
this.textarea.blur();
});
}); });
} }
@bind @bind
focus(opts = { ensureAtEnd: false, refreshHeight: true, addText: null }) { focus(opts = { ensureAtEnd: false, refreshHeight: true, addText: null }) {
next(() => { next(() => {
schedule("afterRender", () => { if (opts.refreshHeight) {
if (opts.refreshHeight) { this.refreshHeight();
this.refreshHeight(); }
}
if (opts.ensureAtEnd) { if (opts.ensureAtEnd) {
this.ensureCaretAtEnd(); this.ensureCaretAtEnd();
} }
if (this.capabilities.isIpadOS || this.site.mobileView) { if (this.capabilities.isIpadOS || this.site.mobileView) {
return; return;
} }
if (opts.addText) { if (opts.addText) {
this.textManipulation.addText( this.textManipulation.addText(
this.textManipulation.getSelected(), this.textManipulation.getSelected(),
opts.addText opts.addText
); );
} }
this.textManipulation.blurAndFocus(); this.textManipulation.blurAndFocus();
});
}); });
} }