Support the once option, when registering EventBus listeners

This follows the same principle as the `once` option that exists in the native `addEventListener` method, and will thus automatically remove an `EventBus` listener when it's invoked; see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Parameters

Finally, this patch also tweaks some the existing `EventBus`-code to use modern features such as optional chaining and logical assignment operators.
This commit is contained in:
Jonas Jenwald 2020-12-29 16:36:58 +01:00
parent 3e34281e3b
commit 739d7c6d77
4 changed files with 60 additions and 26 deletions

View file

@ -76,11 +76,13 @@ class PDFHistory {
this.eventBus._on("pagesinit", () => {
this._isPagesLoaded = false;
const onPagesLoaded = evt => {
this.eventBus._off("pagesloaded", onPagesLoaded);
this._isPagesLoaded = !!evt.pagesCount;
};
this.eventBus._on("pagesloaded", onPagesLoaded);
this.eventBus._on(
"pagesloaded",
evt => {
this._isPagesLoaded = !!evt.pagesCount;
},
{ once: true }
);
});
}