Skip to content

A platform for community discussion. Free, open, simple.

DEV: refactor scrolling mixin logic into scroll-manager service (#32212)

Kelv1557e425c667
diff --git a/app/assets/javascripts/discourse/app/components/discourse-topic.js b/app/assets/javascripts/discourse/app/components/discourse-topic.js
index 9186c9b6c5a..ab071588a2f 100644
--- a/app/assets/javascripts/discourse/app/components/discourse-topic.js
+++ b/app/assets/javascripts/discourse/app/components/discourse-topic.js
@@ -2,6 +2,7 @@ import Component from "@ember/component";
import { alias } from "@ember/object/computed";
import { getOwner } from "@ember/owner";
import { schedule, scheduleOnce } from "@ember/runloop";
+import { service } from "@ember/service";
import { isBlank } from "@ember/utils";
import { classNameBindings } from "@ember-decorators/component";
import { observes } from "@ember-decorators/object";
@@ -9,7 +10,6 @@ import $ from "jquery";
import ClickTrack from "discourse/lib/click-track";
import { bind } from "discourse/lib/decorators";
import { highlightPost } from "discourse/lib/utilities";
-import Scrolling from "discourse/mixins/scrolling";
@classNameBindings(
"multiSelect",
@@ -18,7 +18,9 @@ import Scrolling from "discourse/mixins/scrolling";
"topic.category.read_restricted:read_restricted",
"topic.deleted:deleted-topic"
)
-export default class DiscourseTopic extends Component.extend(Scrolling) {
+export default class DiscourseTopic extends Component {
+ @service scrollManager;
+
@alias("topic.userFilters") userFilters;
@alias("topic.postStream") postStream;
@@ -60,7 +62,7 @@ export default class DiscourseTopic extends Component.extend(Scrolling) {
didInsertElement() {
super.didInsertElement(...arguments);
- this.bindScrolling();
+ this.scrollManager.bindScrolling(this);
window.addEventListener("resize", this.scrolled);
$(this.element).on(
"click.discourse-redirect",
@@ -72,7 +74,7 @@ export default class DiscourseTopic extends Component.extend(Scrolling) {
willDestroyElement() {
super.willDestroyElement(...arguments);
- this.unbindScrolling();
+ this.scrollManager.unbindScrolling(this);
window.removeEventListener("resize", this.scrolled);
// Unbind link tracking
diff --git a/app/assets/javascripts/discourse/app/mixins/load-more.js b/app/assets/javascripts/discourse/app/mixins/load-more.js
index 91ce89caf0a..e88d14a09c0 100644
--- a/app/assets/javascripts/discourse/app/mixins/load-more.js
+++ b/app/assets/javascripts/discourse/app/mixins/load-more.js
@@ -1,10 +1,12 @@
import { on } from "@ember/object/evented";
import Mixin from "@ember/object/mixin";
+import { service } from "@ember/service";
import Eyeline from "discourse/lib/eyeline";
-import Scrolling from "discourse/mixins/scrolling";
// Provides the ability to load more items for a view which is scrolled to the bottom.
-export default Mixin.create(Scrolling, {
+export default Mixin.create({
+ scrollManager: service(),
+
scrolled() {
return this.eyeline?.update();
},
@@ -18,10 +20,10 @@ export default Mixin.create(Scrolling, {
eyeline.on("sawBottom", () => this.send("loadMore"));
eyeline.update(); // update once to consider current position
- this.bindScrolling();
+ this.scrollManager.bindScrolling(this);
}),
_removeEyeline: on("willDestroyElement", function () {
- this.unbindScrolling();
+ this.scrollManager.unbindScrolling(this);
}),
});
diff --git a/app/assets/javascripts/discourse/app/services/scroll-manager.js b/app/assets/javascripts/discourse/app/services/scroll-manager.js
new file mode 100644
index 00000000000..be3eb43f54e
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/services/scroll-manager.js
@@ -0,0 +1,91 @@
+import { scheduleOnce, throttle } from "@ember/runloop";
+import Service, { service } from "@ember/service";
+import { disableImplicitInjections } from "discourse/lib/implicit-injections";
+
+/**
+ * Service for managing scroll event handling across the application
+ *
+ * This service provides methods for binding and unbinding scroll events
+ * on window/document, with throttling to prevent performance issues.
+ * Components can use this service to respond to user scrolling.
+ *
+ * @class ScrollManager
+ */
+@disableImplicitInjections
+export default class ScrollManager extends Service {
+ @service router;
+
+ listeners = new Map();
+
+ /**
+ * Binds scroll events to a component and calls its 'scrolled' method when scrolling occurs
+ *
+ * @method bindScrolling
+ * @param {Object} target The component that will receive 'scrolled' method calls
+ * @param {Object} [opts={}] Configuration options
+ * @param {Number} [opts.throttle=100] Throttle time in milliseconds; this is the time interval between every call of the scroll event
+ * @public
+ *
+ * @example
+ * ```javascript
+ * // In a component:
+ * @service scrollManager;
+ *
+ * didInsertElement() {
+ * super.didInsertElement(...arguments);
+ * this.scrollManager.bindScrolling(this);
+ * }
+ *
+ * willDestroyElement() {
+ * super.willDestroyElement(...arguments);
+ * this.scrollManager.unbindScrolling(this);
+ * }
+ *
+ * scrolled() {
+ * // Handle scroll event
+ * }
+ * ```
+ */
+ bindScrolling(target, opts = {}) {
+ const throttleMs = opts.throttle || 100;
+
+ // So we can not call the scrolled event while transitioning. There is no public API for this :'(
+ // eslint-disable-next-line ember/no-private-routing-service
+ const microLib = this.router._router._routerMicrolib;
+
+ const scheduleScrolled = () => {
+ if (microLib.activeTransition) {
+ return;
+ }
+
+ return scheduleOnce("afterRender", target, "scrolled");
+ };
+
+ const onScrollMethod = throttleMs
+ ? () => throttle(target, scheduleScrolled, throttleMs, false)
+ : scheduleScrolled;
+
+ // Store the handler reference for unbinding later
+ this.listeners.set(target, onScrollMethod);
+
+ // Bind scroll events
+ document.addEventListener("touchmove", onScrollMethod, { passive: true });
+ window.addEventListener("scroll", onScrollMethod, { passive: true });
+ }
+
+ /**
+ * Unbinds scroll events from a component
+ *
+ * @method unbindScrolling
+ * @param {Object} target The component to unbind scroll events from
+ * @public
+ */
+ unbindScrolling(target) {
+ const handler = this.listeners.get(target);
+ if (handler) {
+ document.removeEventListener("touchmove", handler);
+ window.removeEventListener("scroll", handler);
+ this.listeners.delete(target);
+ }
+ }
+}
diff --git a/app/assets/javascripts/discourse/tests/acceptance/topic-discovery-test.js b/app/assets/javascripts/discourse/tests/acceptance/topic-discovery-test.js
index 32ccfa06b05..bcbfcf792d3 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/topic-discovery-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/topic-discovery-test.js
@@ -1,8 +1,8 @@
+import { getOwner } from "@ember/owner";
import { click, currentURL, find, settled, visit } from "@ember/test-helpers";
import { skip, test } from "qunit";
import { configureEyeline } from "discourse/lib/eyeline";
import { cloneJSON } from "discourse/lib/object";
-import { ScrollingDOMMethods } from "discourse/mixins/scrolling";
import discoveryFixtures from "discourse/tests/fixtures/discovery-fixtures";
import topFixtures from "discourse/tests/fixtures/top-fixtures";
import {
@@ -180,7 +180,9 @@ acceptance("Topic Discovery", function (needs) {
acceptance("Topic Discovery | Footer", function (needs) {
needs.hooks.beforeEach(function () {
- ScrollingDOMMethods.bindOnScroll.restore();
+ const scrollManager = getOwner(this).lookup("service:scroll-manager");
+ scrollManager.bindScrolling.restore();
+
configureEyeline({
skipUpdate: false,
rootElement: "#ember-testing",
diff --git a/app/assets/javascripts/discourse/tests/setup-tests.js b/app/assets/javascripts/discourse/tests/setup-tests.js
index 0a614998e97..ba74767e41a 100644
--- a/app/assets/javascripts/discourse/tests/setup-tests.js
+++ b/app/assets/javascripts/discourse/tests/setup-tests.js
@@ -306,6 +306,11 @@ export default function setupTests(config) {
sinon.stub(ScrollingDOMMethods, "bindOnScroll");
sinon.stub(ScrollingDOMMethods, "unbindOnScroll");
+
+ // Access the container that's set up in createApplication
+ const scrollManager = app.__container__.lookup("service:scroll-manager");
+ sinon.stub(scrollManager, "bindScrolling");
+ sinon.stub(scrollManager, "unbindScrolling");
});
QUnit.testDone(function () {