_run return T or void depending on boolean

This commit is contained in:
RMidhunSuresh 2021-11-17 13:22:19 +05:30
parent 07a1130db3
commit d01271fb15
3 changed files with 22 additions and 18 deletions

View file

@ -20,8 +20,6 @@ import {LogLevel, LogFilter} from "./LogFilter";
import type {FilterCreator, LabelOrValues, LogCallback, ILogItem} from "./LogItem"; import type {FilterCreator, LabelOrValues, LogCallback, ILogItem} from "./LogItem";
import type {Platform} from "../platform/web/Platform.js"; import type {Platform} from "../platform/web/Platform.js";
type RunResult<T> = T | void | Promise<T | void>;
export abstract class BaseLogger { export abstract class BaseLogger {
protected _openItems: Set<ILogItem> = new Set(); protected _openItems: Set<ILogItem> = new Set();
protected _platform: Platform; protected _platform: Platform;
@ -37,7 +35,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. */ /** if item is a log item, wrap the callback in a child of it, otherwise start a new root log item. */
wrapOrRun<T>(item: ILogItem, labelOrValues: LabelOrValues, callback: LogCallback<T>, logLevel?: LogLevel, filterCreator?: FilterCreator): RunResult<T> { wrapOrRun<T>(item: ILogItem, labelOrValues: LabelOrValues, callback: LogCallback<T>, logLevel?: LogLevel, filterCreator?: FilterCreator): T {
if (item) { if (item) {
return item.wrap(labelOrValues, callback, logLevel, filterCreator); return item.wrap(labelOrValues, callback, logLevel, filterCreator);
} else { } else {
@ -62,7 +60,7 @@ export abstract class BaseLogger {
/** run a callback wrapped in a log operation. /** run a callback wrapped in a log operation.
Errors and duration are transparently logged, also for async operations. Errors and duration are transparently logged, also for async operations.
Whatever the callback returns is returned here. */ Whatever the callback returns is returned here. */
run<T>(labelOrValues: LabelOrValues, callback: LogCallback<T>, logLevel?: LogLevel, filterCreator?: FilterCreator): RunResult<T> { run<T>(labelOrValues: LabelOrValues, callback: LogCallback<T>, logLevel?: LogLevel, filterCreator?: FilterCreator): T {
if (!logLevel) { if (!logLevel) {
logLevel = LogLevel.Info; logLevel = LogLevel.Info;
} }
@ -70,7 +68,9 @@ export abstract class BaseLogger {
return this._run(item, callback, logLevel!, true, filterCreator); return this._run(item, callback, logLevel!, true, filterCreator);
} }
_run<T>(item: ILogItem, callback: LogCallback<T>, logLevel: LogLevel, shouldThrow: boolean, filterCreator?: FilterCreator): RunResult<T> { _run<T>(item: ILogItem, callback: LogCallback<T>, logLevel: LogLevel, wantResult: true, filterCreator?: FilterCreator): T;
_run<T>(item: ILogItem, callback: LogCallback<T>, logLevel: LogLevel, wantResult: false, filterCreator?: FilterCreator): void;
_run<T>(item: ILogItem, callback: LogCallback<T>, logLevel: LogLevel, wantResult: boolean, filterCreator?: FilterCreator): T | void {
this._openItems.add(item); this._openItems.add(item);
const finishItem = () => { const finishItem = () => {
@ -94,24 +94,28 @@ export abstract class BaseLogger {
}; };
try { try {
const result = item.run(callback); let result = item.run(callback);
if (result instanceof Promise) { if (result instanceof Promise) {
return result.then(promiseResult => { result = result.then(promiseResult => {
finishItem(); finishItem();
return promiseResult; return promiseResult;
}, err => { }, err => {
finishItem(); finishItem();
if (shouldThrow) { if (wantResult) {
throw err; throw err;
} }
}); }) as unknown as T;
} else { if (wantResult) {
return result;
}
}
if(wantResult) {
finishItem(); finishItem();
return result; return result;
} }
} catch (err) { } catch (err) {
finishItem(); finishItem();
if (shouldThrow) { if (wantResult) {
throw err; throw err;
} }
} }

View file

@ -42,10 +42,10 @@ export interface ILogItem {
children?: Array<ILogItem>; children?: Array<ILogItem>;
values: LogItemValues; values: LogItemValues;
error?: Error; error?: Error;
wrap<T>(labelOrValues: LabelOrValues, callback: LogCallback<T>, logLevel?: LogLevel, filterCreator?: FilterCreator): T | Promise<T>; wrap<T>(labelOrValues: LabelOrValues, callback: LogCallback<T>, logLevel?: LogLevel, filterCreator?: FilterCreator): T;
log(labelOrValues: LabelOrValues, logLevel?: LogLevel): void; log(labelOrValues: LabelOrValues, logLevel?: LogLevel): void;
set(key: string | object, value: unknown): void; set(key: string | object, value: unknown): void;
run<T>(callback: LogCallback<T>): T | Promise<T>; run<T>(callback: LogCallback<T>): T;
runDetached(labelOrValues: LabelOrValues, callback: LogCallback<unknown>, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem; runDetached(labelOrValues: LabelOrValues, callback: LogCallback<unknown>, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem;
wrapDetached(labelOrValues: LabelOrValues, callback: LogCallback<unknown>, logLevel?: LogLevel, filterCreator?: FilterCreator): void; wrapDetached(labelOrValues: LabelOrValues, callback: LogCallback<unknown>, logLevel?: LogLevel, filterCreator?: FilterCreator): void;
refDetached(logItem: ILogItem, logLevel?: LogLevel): void; refDetached(logItem: ILogItem, logLevel?: LogLevel): void;
@ -115,7 +115,7 @@ export class LogItem implements ILogItem {
/** /**
* Creates a new child item and runs it in `callback`. * Creates a new child item and runs it in `callback`.
*/ */
wrap<T>(labelOrValues: LabelOrValues, callback: LogCallback<T>, logLevel?: LogLevel, filterCreator?: FilterCreator): T | Promise<T> { wrap<T>(labelOrValues: LabelOrValues, callback: LogCallback<T>, logLevel?: LogLevel, filterCreator?: FilterCreator): T {
const item = this.child(labelOrValues, logLevel, filterCreator); const item = this.child(labelOrValues, logLevel, filterCreator);
return item.run(callback); return item.run(callback);
} }
@ -235,7 +235,7 @@ export class LogItem implements ILogItem {
* @param {Function} callback [description] * @param {Function} callback [description]
* @return {[type]} [description] * @return {[type]} [description]
*/ */
run<T>(callback: LogCallback<T>): T | Promise<T> { run<T>(callback: LogCallback<T>): T {
if (this.end) { if (this.end) {
console.trace("log item is finished, additional logs will likely not be recorded"); console.trace("log item is finished, additional logs will likely not be recorded");
} }
@ -248,7 +248,7 @@ export class LogItem implements ILogItem {
return promiseResult; return promiseResult;
}, err => { }, err => {
throw this.catch(err); throw this.catch(err);
}); }) as unknown as T;
} else { } else {
this.finish(); this.finish();
return result; return result;

View file

@ -59,7 +59,7 @@ export class NullLogItem implements ILogItem {
this.logger = logger; this.logger = logger;
} }
wrap<T>(_: LabelOrValues, callback: LogCallback<T>): T | Promise<T> { wrap<T>(_: LabelOrValues, callback: LogCallback<T>): T {
return callback(this); return callback(this);
} }
@ -75,7 +75,7 @@ export class NullLogItem implements ILogItem {
return this.refDetached(); return this.refDetached();
} }
run<T>(callback: LogCallback<T>): T | Promise<T> { run<T>(callback: LogCallback<T>): T {
return callback(this); return callback(this);
} }