From 4c1d7a8f2d098a0fbfa850fd1f421e52f4d080a6 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Mon, 15 Nov 2021 22:47:38 +0530 Subject: [PATCH] Use generics over returning unknown --- src/logging/BaseLogger.ts | 9 +++++---- src/logging/LogItem.ts | 20 ++++++++++---------- src/logging/NullLogger.ts | 8 ++++---- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/logging/BaseLogger.ts b/src/logging/BaseLogger.ts index 6c377d48..a2463d3a 100644 --- a/src/logging/BaseLogger.ts +++ b/src/logging/BaseLogger.ts @@ -21,6 +21,7 @@ import type {FilterCreator, LabelOrValues, LogCallback, ILogItem} from "./LogIte // todo: should this import be here just for getting the type? should it instead be done when Platform.js --> Platform.ts? import type {Platform} from "../platform/web/Platform.js"; +type RunResult = T | void | Promise; export abstract class BaseLogger { protected _openItems: Set = new Set(); @@ -37,7 +38,7 @@ export abstract class BaseLogger { } /** if item is a log item, wrap the callback in a child of it, otherwise start a new root log item. */ - wrapOrRun(item: ILogItem, labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): unknown { + wrapOrRun(item: ILogItem, labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): RunResult { if (item) { return item.wrap(labelOrValues, callback, logLevel, filterCreator); } else { @@ -50,7 +51,7 @@ export abstract class BaseLogger { Useful to pair with LogItem.refDetached. @return {LogItem} the log item added, useful to pass to LogItem.refDetached */ - runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem { + runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem { // todo: Remove jsdoc type? if (!logLevel) { logLevel = LogLevel.Info; @@ -63,7 +64,7 @@ export abstract class BaseLogger { /** run a callback wrapped in a log operation. Errors and duration are transparently logged, also for async operations. Whatever the callback returns is returned here. */ - run(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): unknown { + run(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): RunResult { if (!logLevel) { logLevel = LogLevel.Info; } @@ -71,7 +72,7 @@ export abstract class BaseLogger { return this._run(item, callback, logLevel!, true, filterCreator); } - _run(item: ILogItem, callback: LogCallback, logLevel: LogLevel, shouldThrow: boolean, filterCreator?: FilterCreator): unknown { + _run(item: ILogItem, callback: LogCallback, logLevel: LogLevel, shouldThrow: boolean, filterCreator?: FilterCreator): RunResult { this._openItems.add(item); const finishItem = () => { diff --git a/src/logging/LogItem.ts b/src/logging/LogItem.ts index 82f785e4..4600b6d5 100644 --- a/src/logging/LogItem.ts +++ b/src/logging/LogItem.ts @@ -42,12 +42,12 @@ export interface ILogItem { children?: Array; values: LogItemValues; error?: Error; - wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): unknown; + wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): T | Promise; log(labelOrValues: LabelOrValues, logLevel?: LogLevel): void; set(key: string | object, value: unknown): void; - run(callback: LogCallback): unknown; - runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem; - wrapDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): void; + run(callback: LogCallback): T | Promise; + runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem; + wrapDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): void; refDetached(logItem: ILogItem, logLevel?: LogLevel): void; ensureRefId(): void; catch(err: Error): Error; @@ -68,7 +68,7 @@ export type LogItemValues = { export type LabelOrValues = string | LogItemValues; export type FilterCreator = ((filter: LogFilter, item: ILogItem) => LogFilter); -export type LogCallback = (item: ILogItem) => unknown; +export type LogCallback = (item: ILogItem) => T; export class LogItem implements ILogItem { public readonly start: number; @@ -90,12 +90,12 @@ export class LogItem implements ILogItem { } /** start a new root log item and run it detached mode, see BaseLogger.runDetached */ - runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem { + runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem { return this._logger.runDetached(labelOrValues, callback, logLevel, filterCreator); } /** start a new detached root log item and log a reference to it from this item */ - wrapDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): void { + wrapDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): void { this.refDetached(this.runDetached(labelOrValues, callback, logLevel, filterCreator)); } @@ -115,7 +115,7 @@ export class LogItem implements ILogItem { /** * Creates a new child item and runs it in `callback`. */ - wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): unknown { + wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): T | Promise { const item = this.child(labelOrValues, logLevel, filterCreator); return item.run(callback); } @@ -237,11 +237,11 @@ export class LogItem implements ILogItem { * @param {Function} callback [description] * @return {[type]} [description] */ - run(callback: LogCallback): unknown { + run(callback: LogCallback): T | Promise { if (this.end) { console.trace("log item is finished, additional logs will likely not be recorded"); } - let result: unknown; + let result: T | Promise; try { result = callback(this); if (result instanceof Promise) { diff --git a/src/logging/NullLogger.ts b/src/logging/NullLogger.ts index 63e91f61..9db1eee4 100644 --- a/src/logging/NullLogger.ts +++ b/src/logging/NullLogger.ts @@ -60,22 +60,22 @@ export class NullLogItem implements ILogItem { this.logger = logger; } - wrap(_: LabelOrValues, callback: LogCallback): unknown { + wrap(_: LabelOrValues, callback: LogCallback): T | Promise { return callback(this); } log(): void {} set(): void {} - runDetached(_: LabelOrValues, callback: LogCallback): ILogItem { + runDetached(_: LabelOrValues, callback: LogCallback): ILogItem { new Promise(r => r(callback(this))).then(noop, noop); return this; } - wrapDetached(_: LabelOrValues, _callback: LogCallback): void { + wrapDetached(_: LabelOrValues, _callback: LogCallback): void { return this.refDetached(); } - run(callback: LogCallback): unknown { + run(callback: LogCallback): T | Promise { return callback(this); }