Skip to content

Commit

Permalink
IntfLogger.expectInstance() (#446)
Browse files Browse the repository at this point in the history
This adds convenient logger type check methods
`IntfLogger.expectInstance()` and `IntfLogger.expectInstanceOrNull()` to
help avoid ad-hoc checking.
  • Loading branch information
danfuzz authored Dec 9, 2024
2 parents 5451dff + 792fcf8 commit 11e1ed1
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 9 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ Breaking changes:
* `BaseComponent`: Renamed `CONFIG_CLASS` to `configClass`.

Other notable changes:
* None.
* `loggy-intf`:
* `LoggyIntf`: New static methods `expectInstance()` and
`expectInstanceOrNull()`, to avoid more ad-hoc checks.

### v0.8.5 -- 2024-12-06

Expand Down
43 changes: 42 additions & 1 deletion src/loggy-intf/export/IntfLogger.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2022-2024 the Lactoserv Authors (Dan Bornstein et alia).
// SPDX-License-Identifier: Apache-2.0

import { Methods } from '@this/typey';
import { AskIf, Methods } from '@this/typey';

import { IntfLoggingEnvironment } from '#x/IntfLoggingEnvironment';
import { LogPayload } from '#x/LogPayload';
Expand Down Expand Up @@ -79,6 +79,47 @@ export class IntfLogger {
return Methods.abstract();
}


//
// Static members
//

/**
* Returns the given value if it is an instance of this interface. Throws an
* error if not.
*
* **Note:** Because of JavaScript's loosey-goosey nature, this method is, as
* a practical matter, overly accepting of values as instances.
*
* @param {*} logger (Alleged) logger instance.
* @returns {IntfLogger} `logger` if it is a logger.
* @throws {Error} Thrown if `logger` is not actually a logger.
*/
static expectInstance(logger) {
if (logger instanceof IntfLogger) {
return logger;
} else if (AskIf.callableFunction(logger) && logger.$env) {
return logger;
}

throw new Error(`Not a logger: ${logger}`);
}

/**
* Returns the given value if it is an instance of this interface or is
* `null`. Throws an error if not either.
*
* **Note:** Because of JavaScript's loosey-goosey nature, this method is, as
* a practical matter, overly accepting of values as instances.
*
* @param {*} logger (Alleged) logger instance.
* @returns {?IntfLogger} `logger` if it is a logger or `null`.
* @throws {Error} Thrown if `logger` is not actually a logger or `null`.
*/
static expectInstanceOrNull(logger) {
return (logger === null) ? null : this.expectInstance(logger);
}

/**
* Metainformation about a logger. Instances of this interface are returned
* when accessing the property `$meta` on logger instances.
Expand Down
2 changes: 1 addition & 1 deletion src/metacomp/export/LimitedLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class LimitedLoader {
*/
constructor(context = null, logger = null) {
this.#context = context;
this.#logger = logger;
this.#logger = IntfLogger.expectInstanceOrNull(logger);

if (context && !vm.isContext(context)) {
vm.createContext(context);
Expand Down
2 changes: 1 addition & 1 deletion src/net-protocol/export/ProtocolWrangler.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export class ProtocolWrangler {
* logging.
*/
async init(logger) {
this.#logger = logger;
this.#logger = IntfLogger.expectInstanceOrNull(logger);

// Confusion alert!: This is not the same as the `requestLogger` (a "request
// logger") per se) passed in as an option. This is the sub-logger of the
Expand Down
2 changes: 1 addition & 1 deletion src/net-protocol/private/AsyncServerSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class AsyncServerSocket {
// Note: `interface` is a reserved word.
this.#interface = MustBe.instanceOf(iface, InterfaceAddress);
this.#protocol = MustBe.string(protocol);
this.#logger = logger;
this.#logger = IntfLogger.expectInstanceOrNull(logger);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/net-protocol/private/WranglerContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ export class WranglerContext {
ctx.#socket = socket;

if (logger) {
ctx.#connectionLogger = logger;
ctx.#connectionId = logger.$meta.lastContext;
ctx.#connectionLogger = IntfLogger.expectInstanceOrNull(logger);
ctx.#connectionId = logger?.$meta.lastContext ?? null;
}

ctx.bind(socket);
Expand Down
2 changes: 1 addition & 1 deletion src/net-util/export/DispatchInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class DispatchInfo extends IntfDeconstructable {

this.#base = MustBe.instanceOf(base, PathKey);
this.#extra = MustBe.instanceOf(extra, PathKey);
this.#logger = (logger === null) ? null : MustBe.callableFunction(logger);
this.#logger = IntfLogger.expectInstanceOrNull(logger);
}

/** @override */
Expand Down
2 changes: 1 addition & 1 deletion src/webapp-core/export/HostManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class HostManager extends TemplAggregateComponent('HostAggregate', BaseCo
* @param {?IntfLogger} logger Logger to use, if any.
*/
constructor(logger) {
this.#logger = logger;
this.#logger = IntfLogger.expectInstanceOrNull(logger);
}

/**
Expand Down

0 comments on commit 11e1ed1

Please sign in to comment.