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
Jarek Radosz 8304f40f84
FIX: Correctly debounce various functions (#18673)
Debouncing inline anonymous functions does not work.

This fixes all instances of that error by extracting the function or using the new `@debounce(delay)` decorator
2022-10-20 13:28:09 +02:00

59 lines
1.3 KiB
JavaScript

import Mixin from "@ember/object/mixin";
import discourseDebounce from "discourse-common/lib/debounce";
import { cancel } from "@ember/runloop";
import discourseLater from "discourse-common/lib/later";
import { bind } from "discourse-common/utils/decorators";
const INITIAL_DELAY_MS = 50;
const DEBOUNCE_MS = 5;
export default Mixin.create({
_initialTimer: null,
_queuedTimer: null,
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 = discourseLater(
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);
},
@bind
queueDockCheck() {
this._queuedTimer = discourseDebounce(
this,
this.safeDockCheck,
DEBOUNCE_MS
);
},
@bind
safeDockCheck() {
if (this.isDestroyed || this.isDestroying) {
return;
}
this.dockCheck();
},
});