diff --git a/assets/router.js b/assets/router.js index fdfc789..a3e8715 100644 --- a/assets/router.js +++ b/assets/router.js @@ -177,7 +177,7 @@ class Router { } - async handlePopState() { + async handlePopState(event) { if (this.disablePopHandler) { return; } @@ -187,7 +187,7 @@ class Router { try { window.dispatchEvent(new CustomEvent("popstateHandlerEntered")); /** @type {State} */ - const state = history.state; + const state = event.state; if (!state) { await this.handleFallbackHome(); diff --git a/assets/utils.js b/assets/utils.js index 55ded29..2f9490b 100644 --- a/assets/utils.js +++ b/assets/utils.js @@ -41,31 +41,41 @@ async function sleep(ms) { } class Semaphore { - constructor(max) { - this.max = max; - this.current = 0; - this.queue = []; - } + /** + * @param {number} [maxConcurrency] + */ + constructor(maxConcurrency = 1) { + /** @type {{resolve: () => void, promise: Promise}[]} */ + this.queue = []; + this.maxConcurrency = maxConcurrency; + } - acquire() { - if (this.current < this.max) { - this.current++; - return Promise.resolve(); - } else { - return new Promise((resolve) => this.queue.push(resolve)); - } - } + acquire() { + let resolver; + const promise = new Promise((resolve) => { + resolver = resolve; + }); - release() { - if (this.queue.length > 0) { - const resolve = this.queue.shift(); - resolve(); - } else { - this.current--; - } - } + this.queue.push({ resolve: resolver, promise: promise }); - awaitCurrentQueue() { - return Promise.all(this.queue); - } -} + if (this.queue.length <= this.maxConcurrency) { + return Promise.resolve(); + } + + return promise; + } + + release() { + if (this.queue.length === 0) { + return; + } + + this.queue.shift().resolve(); + } + + + awaitCurrentQueue() { + const promises = this.queue.map(item => item.promise); + return Promise.all(promises); + } +} \ No newline at end of file