Use generics over returning unknown

This commit is contained in:
RMidhunSuresh 2021-11-15 22:47:38 +05:30
parent fe69f84c85
commit 4c1d7a8f2d
3 changed files with 19 additions and 18 deletions

View file

@ -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> = T | void | Promise<T | void>;
export abstract class BaseLogger {
protected _openItems: Set<ILogItem> = 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<T>(item: ILogItem, labelOrValues: LabelOrValues, callback: LogCallback<T>, logLevel?: LogLevel, filterCreator?: FilterCreator): RunResult<T> {
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<unknown>, 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<T>(labelOrValues: LabelOrValues, callback: LogCallback<T>, logLevel?: LogLevel, filterCreator?: FilterCreator): RunResult<T> {
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<T>(item: ILogItem, callback: LogCallback<T>, logLevel: LogLevel, shouldThrow: boolean, filterCreator?: FilterCreator): RunResult<T> {
this._openItems.add(item);
const finishItem = () => {

View file

@ -42,12 +42,12 @@ export interface ILogItem {
children?: Array<ILogItem>;
values: LogItemValues;
error?: Error;
wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): unknown;
wrap<T>(labelOrValues: LabelOrValues, callback: LogCallback<T>, logLevel?: LogLevel, filterCreator?: FilterCreator): T | Promise<T>;
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<T>(callback: LogCallback<T>): T | Promise<T>;
runDetached(labelOrValues: LabelOrValues, callback: LogCallback<unknown>, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem;
wrapDetached(labelOrValues: LabelOrValues, callback: LogCallback<unknown>, 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<T> = (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<unknown>, 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<unknown>, 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<T>(labelOrValues: LabelOrValues, callback: LogCallback<T>, logLevel?: LogLevel, filterCreator?: FilterCreator): T | Promise<T> {
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<T>(callback: LogCallback<T>): T | Promise<T> {
if (this.end) {
console.trace("log item is finished, additional logs will likely not be recorded");
}
let result: unknown;
let result: T | Promise<T>;
try {
result = callback(this);
if (result instanceof Promise) {

View file

@ -60,22 +60,22 @@ export class NullLogItem implements ILogItem {
this.logger = logger;
}
wrap(_: LabelOrValues, callback: LogCallback): unknown {
wrap<T>(_: LabelOrValues, callback: LogCallback<T>): T | Promise<T> {
return callback(this);
}
log(): void {}
set(): void {}
runDetached(_: LabelOrValues, callback: LogCallback): ILogItem {
runDetached(_: LabelOrValues, callback: LogCallback<unknown>): ILogItem {
new Promise(r => r(callback(this))).then(noop, noop);
return this;
}
wrapDetached(_: LabelOrValues, _callback: LogCallback): void {
wrapDetached(_: LabelOrValues, _callback: LogCallback<unknown>): void {
return this.refDetached();
}
run(callback: LogCallback): unknown {
run<T>(callback: LogCallback<T>): T | Promise<T> {
return callback(this);
}