Skip to content

Commit

Permalink
refactor: router internal state
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Jul 11, 2023
1 parent 3e8a21d commit 01dc63f
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,6 @@ type ListenOptions = ListenOptionsBase | ListenOptionsSecure;
interface InternalState {
closed: boolean;
closing: boolean;
handling: Set<Promise<Response>>;
server: ServerConstructor;
}

Expand Down Expand Up @@ -789,6 +788,7 @@ export interface RouterHandleInit {
* ```
*/
export class Router extends EventTarget {
#handling: Set<Promise<Response>> = new Set();
#keys?: KeyRing;
#preferJson: boolean;
#routes = new Set<Route>();
Expand Down Expand Up @@ -878,7 +878,7 @@ export class Router extends EventTarget {
const uid = this.#uid++;
performance.mark(`${HANDLE_START} ${uid}`);
const deferred = new Deferred<Response>();
this.#state?.handling.add(deferred.promise);
this.#handling.add(deferred.promise);
requestEvent.respondWith(deferred.promise).catch((error) =>
this.#error(requestEvent.request, error, false)
);
Expand Down Expand Up @@ -941,7 +941,7 @@ export class Router extends EventTarget {
response = this.#error(request, error, true);
}
deferred.resolve(response);
this.#state?.handling.delete(deferred.promise);
this.#handling.delete(deferred.promise);
const measure = performance.measure(
`handle ${uid}`,
`${HANDLE_START} ${uid}`,
Expand All @@ -963,7 +963,7 @@ export class Router extends EventTarget {
response,
);
deferred.resolve(result ?? response);
this.#state?.handling.delete(deferred.promise);
this.#handling.delete(deferred.promise);
const measure = performance.measure(
`handle ${uid}`,
`${HANDLE_START} ${uid}`,
Expand All @@ -981,7 +981,7 @@ export class Router extends EventTarget {
response = this.#notFound(request);
}
deferred.resolve(response);
this.#state?.handling.delete(deferred.promise);
this.#handling.delete(deferred.promise);
const measure = performance.measure(
`handle ${uid}`,
`${HANDLE_START} ${uid}`,
Expand Down Expand Up @@ -1430,15 +1430,14 @@ export class Router extends EventTarget {
this.#state = {
closed: false,
closing: false,
handling: new Set(),
server: Server,
};
this.#secure = secure;
if (signal) {
signal.addEventListener("abort", async () => {
assert(this.#state, "router state should exist");
this.#state.closing = true;
await Promise.all(this.#state.handling);
await Promise.all(this.#handling);
await server.close();
this.#state.closed = true;
});
Expand All @@ -1457,7 +1456,7 @@ export class Router extends EventTarget {
for await (const [requestEvent, addr] of server) {
this.#handle(requestEvent, addr);
}
await Promise.all(this.#state.handling);
await Promise.all(this.#handling);
} catch (error) {
const message = error instanceof Error ? error.message : "Internal error";
this.dispatchEvent(new RouterErrorEvent({ message, error }));
Expand Down

0 comments on commit 01dc63f

Please sign in to comment.