diff options
| author | Fuwn <[email protected]> | 2022-05-15 06:27:30 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-05-15 06:27:30 +0000 |
| commit | a5e101568eb9d981e868bc7bb767e067d838e3a1 (patch) | |
| tree | 3163fd3eb3bff886001ed74653902dd20de0c6d1 | |
| parent | feat(decorators): auto deduce callback type (diff) | |
| download | laurali-a5e101568eb9d981e868bc7bb767e067d838e3a1.tar.xz laurali-a5e101568eb9d981e868bc7bb767e067d838e3a1.zip | |
refactor(laurali): refer to callbacks as hooks
| -rw-r--r-- | README.md | 7 | ||||
| -rw-r--r-- | examples/my_cool_server.ts | 8 | ||||
| -rw-r--r-- | laurali/decorators.ts | 24 | ||||
| -rw-r--r-- | laurali/hooks.ts (renamed from laurali/callbacks.ts) | 4 | ||||
| -rw-r--r-- | laurali/mod.ts | 2 | ||||
| -rw-r--r-- | laurali/server.ts | 24 |
6 files changed, 32 insertions, 37 deletions
@@ -35,12 +35,7 @@ $ deno run --allow-write --allow-run --allow-net --allow-read https://deno.land/ You can then begin to implement your very own Laurali server. ```ts -import { - Callback, - callback, - route, - Server, -} from "https://deno.land/x/laurali/mod.ts"; +import { route, Server } from "https://deno.land/x/laurali/mod.ts"; class MyCoolServer extends Server { /** Visit `/hi` */ diff --git a/examples/my_cool_server.ts b/examples/my_cool_server.ts index 76657a0..793e01f 100644 --- a/examples/my_cool_server.ts +++ b/examples/my_cool_server.ts @@ -16,7 +16,7 @@ // Copyright (C) 2022-2022 Fuwn <[email protected]> // SPDX-License-Identifier: GPL-3.0-only -import { callback, route, Server } from "../mod.ts"; +import { hook, route, Server } from "../mod.ts"; import * as optic from "https://deno.land/x/[email protected]/mod.ts"; /** Implement a new Laurali server */ @@ -50,7 +50,7 @@ class MyCoolServer extends Server { return MyCoolServer.clicks; } - @callback() + @hook() override onPreRoute(ctx: Deno.TlsConn) { MyCoolServer.clicks += 1; @@ -60,12 +60,12 @@ class MyCoolServer extends Server { ); } - @callback() + @hook() override onPostRoute() { MyCoolServer.logger.info("Closed connection."); } - @callback() + @hook() override onError() { return "hi"; } diff --git a/laurali/decorators.ts b/laurali/decorators.ts index b1936c1..54a0c79 100644 --- a/laurali/decorators.ts +++ b/laurali/decorators.ts @@ -16,7 +16,7 @@ // Copyright (C) 2022-2022 Fuwn <[email protected]> // SPDX-License-Identifier: GPL-3.0-only -import { Callback } from "./callbacks.ts"; +import { Hook } from "./hooks.ts"; /** * Mark the function as a route and register it to the `Server`. @@ -36,10 +36,10 @@ export const route = (path?: string) => { }; /** - * Mark the function as a callback and register it to the `Server`. - * @param callback The type of callback which the function will be called for. + * Mark the function as a hook and register it to the `Server`. + * @param hook The type of hook which the function will be called for. */ -export const callback = (callback?: Callback) => { +export const hook = (hook?: Hook) => { return ( // deno-lint-ignore no-explicit-any target: any, @@ -48,35 +48,35 @@ export const callback = (callback?: Callback) => { ) => { let type; - if (callback) { - type = callback; + if (hook) { + type = hook; } else { switch (key) { case "onPreRoute": { - type = Callback.ON_PRE_ROUTE; + type = Hook.ON_PRE_ROUTE; } break; case "onPostRoute": { - type = Callback.ON_POST_ROUTE; + type = Hook.ON_POST_ROUTE; } break; case "onError": { - type = Callback.ON_ERROR; + type = Hook.ON_ERROR; } break; default: { throw new Error( - `Unknown callback type: '${key.toString()}'. Did you forget to ` + - "specify the callback type?`", + `Unknown hook type: '${key.toString()}'. Did you forget to ` + + "specify the hook type?`", ); } } } - target.addCallback(type, descriptor.value); + target.addHook(type, descriptor.value); return descriptor; }; diff --git a/laurali/callbacks.ts b/laurali/hooks.ts index a9f7dd5..70d1803 100644 --- a/laurali/callbacks.ts +++ b/laurali/hooks.ts @@ -16,8 +16,8 @@ // Copyright (C) 2022-2022 Fuwn <[email protected]> // SPDX-License-Identifier: GPL-3.0-only -/** One of few callbacks of a `Server` */ -export const enum Callback { +/** One of few hooks of a `Server` */ +export const enum Hook { /** Called before a connection to a client has been responded to */ ON_PRE_ROUTE = 0, /** Called after a connection to a client has concluded */ diff --git a/laurali/mod.ts b/laurali/mod.ts index 50f5614..39c6389 100644 --- a/laurali/mod.ts +++ b/laurali/mod.ts @@ -17,5 +17,5 @@ // SPDX-License-Identifier: GPL-3.0-only export * from "./decorators.ts"; -export * from "./callbacks.ts"; +export * from "./hooks.ts"; export * from "./server.ts"; diff --git a/laurali/server.ts b/laurali/server.ts index 3e8cc53..71f0fd7 100644 --- a/laurali/server.ts +++ b/laurali/server.ts @@ -16,7 +16,7 @@ // Copyright (C) 2022-2022 Fuwn <[email protected]> // SPDX-License-Identifier: GPL-3.0-only -import { Callback } from "./callbacks.ts"; +import { Hook } from "./hooks.ts"; export interface ServerConfiguration { port?: number; @@ -34,8 +34,8 @@ export abstract class Server { /** All registered route functions of the `Server` */ // deno-lint-ignore no-explicit-any static #routes: Map<string, (ctx: Deno.TlsConn) => any> = new Map(); - /** All registered callback functions of the `Server` */ - static #callbacks: Map<Callback, (ctx: Deno.TlsConn) => void> = new Map(); + /** All registered hook functions of the `Server` */ + static #hooks: Map<Hook, (ctx: Deno.TlsConn) => void> = new Map(); /** The port of the `Server` */ static #port: number; /** The hostname of the `Server` */ @@ -65,9 +65,9 @@ export abstract class Server { addRoute(route: string, handler: () => any) { Server.#routes.set(route, handler); } - /** Add a callback function to the `Server` */ - addCallback(callback: Callback, handler: () => void) { - Server.#callbacks.set(callback, handler); + /** Add a hook function to the `Server` */ + addHook(hook: Hook, handler: () => void) { + Server.#hooks.set(hook, handler); } /** Get the `port` of the `Server` */ @@ -98,7 +98,7 @@ export abstract class Server { /** Start listening and responding to client connections */ async listen() { - // If the `Server` has an `onListen` callback, call it. + // If the `Server` has an `onListen` hook, call it. if (this.onListen) this.onListen(); // Listen for connections and handle them. @@ -115,9 +115,9 @@ export abstract class Server { continue; } - const onPreRoute = Server.#callbacks.get(Callback.ON_PRE_ROUTE); - const onPostRoute = Server.#callbacks.get(Callback.ON_POST_ROUTE); - const onError = Server.#callbacks.get(Callback.ON_ERROR); + const onPreRoute = Server.#hooks.get(Hook.ON_PRE_ROUTE); + const onPostRoute = Server.#hooks.get(Hook.ON_POST_ROUTE); + const onError = Server.#hooks.get(Hook.ON_ERROR); // Make sure that the client has sent a request. if (n === null) { @@ -132,7 +132,7 @@ export abstract class Server { "", ).replace(/gemini:\/\//, ""); - // If the `Server` has an `onPreRoute` callback, call it. + // If the `Server` has an `onPreRoute` hook, call it. if (onPreRoute) onPreRoute(r); // Respond to index requests. @@ -178,7 +178,7 @@ export abstract class Server { r.close(); } - // If the `Server` has an `onPostRoute` callback, call it. + // If the `Server` has an `onPostRoute` hook, call it. if (onPostRoute) onPostRoute(r); continue; |