Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
john-tornblom committed Jun 2, 2024
1 parent 9c10656 commit 023272a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
4 changes: 2 additions & 2 deletions assets/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class Router {

}

async handlePopState() {
async handlePopState(event) {
if (this.disablePopHandler) {
return;
}
Expand All @@ -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();
Expand Down
60 changes: 35 additions & 25 deletions assets/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>}[]} */
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);
}
}

0 comments on commit 023272a

Please sign in to comment.