This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/app/mixins/docking.js
David Taylor df7cda40b7
DEV: Skip timers when loading topic route in tests (#15500)
The test environment will wait for all timers to settle before continuing. These timers were causing all tests involving `/t/*` routes to spend 500ms doing nothing.

Fun fact: we load the topic route 214 times during the core test suite. That means that this commit saves a total of around 107s across the whole suite. On my machine, that's a 30% improvement in runtime.
2022-01-10 10:51:18 +00:00

56 lines
1.4 KiB
JavaScript

import Mixin from "@ember/object/mixin";
import discourseDebounce from "discourse-common/lib/debounce";
import { cancel, later } from "@ember/runloop";
import { isTesting } from "discourse-common/config/environment";
const INITIAL_DELAY_MS = isTesting() ? 0 : 50;
const DEBOUNCE_MS = isTesting() ? 0 : 5;
export default Mixin.create({
queueDockCheck: null,
_initialTimer: null,
_queuedTimer: null,
init() {
this._super(...arguments);
this.queueDockCheck = () => {
this._queuedTimer = discourseDebounce(
this,
this.safeDockCheck,
DEBOUNCE_MS
);
};
},
safeDockCheck() {
if (this.isDestroyed || this.isDestroying) {
return;
}
this.dockCheck();
},
didInsertElement() {
this._super(...arguments);
window.addEventListener("scroll", this.queueDockCheck, { passive: true });
document.addEventListener("touchmove", this.queueDockCheck, {
passive: true,
});
// dockCheck might happen too early on full page refresh
this._initialTimer = later(this, this.safeDockCheck, INITIAL_DELAY_MS);
},
willDestroyElement() {
this._super(...arguments);
if (this._queuedTimer) {
cancel(this._queuedTimer);
}
cancel(this._initialTimer);
window.removeEventListener("scroll", this.queueDockCheck);
document.removeEventListener("touchmove", this.queueDockCheck);
},
});