Fixing some problems with the adapter API
This commit is contained in:
parent
94d9731cdd
commit
9182efe7e7
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hibas123/logging",
|
"name": "@hibas123/logging",
|
||||||
"version": "2.2.2",
|
"version": "2.3.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "out/index.js",
|
"main": "out/index.js",
|
||||||
"types": "out/index.d.ts",
|
"types": "out/index.d.ts",
|
||||||
|
34
src/base.ts
34
src/base.ts
@ -42,6 +42,7 @@ export interface LoggingBaseOptions {
|
|||||||
console: boolean;
|
console: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const adapterCache = new WeakMap<Adapter, number>();
|
||||||
export class LoggingBase {
|
export class LoggingBase {
|
||||||
private _formatMap: FormatConfig = new DefaultFormatConfig();
|
private _formatMap: FormatConfig = new DefaultFormatConfig();
|
||||||
|
|
||||||
@ -110,8 +111,12 @@ export class LoggingBase {
|
|||||||
addAdapter(adapter: Adapter) {
|
addAdapter(adapter: Adapter) {
|
||||||
if (!this.adapter.has(adapter)) {
|
if (!this.adapter.has(adapter)) {
|
||||||
this.adapter.add(adapter);
|
this.adapter.add(adapter);
|
||||||
|
|
||||||
|
let cached = adapterCache.get(adapter) || 0;
|
||||||
|
adapterCache.set(adapter, cached + 1);
|
||||||
|
|
||||||
let prms = Promise.resolve(
|
let prms = Promise.resolve(
|
||||||
adapter.init(this.messageObservable.getPublicApi(), this._name)
|
adapter.init(this.messageObservable.getPublicApi())
|
||||||
);
|
);
|
||||||
this.adapter_init.push(prms);
|
this.adapter_init.push(prms);
|
||||||
}
|
}
|
||||||
@ -129,10 +134,29 @@ export class LoggingBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private $closed = false;
|
||||||
|
|
||||||
public close() {
|
public close() {
|
||||||
this.adapter.forEach((adapter) =>
|
if (this.$closed) return;
|
||||||
adapter.close ? adapter.close() : undefined
|
this.$closed = true;
|
||||||
);
|
|
||||||
|
this.adapter.forEach((adapter) => {
|
||||||
|
let cached = adapterCache.get(adapter);
|
||||||
|
|
||||||
|
if (cached) {
|
||||||
|
cached--;
|
||||||
|
if (cached <= 0) {
|
||||||
|
adapterCache.delete(adapter);
|
||||||
|
adapter.close();
|
||||||
|
} else adapterCache.set(adapter, cached);
|
||||||
|
}
|
||||||
|
|
||||||
|
adapter.close ? adapter.close() : undefined;
|
||||||
|
});
|
||||||
|
|
||||||
|
this.adapter = undefined;
|
||||||
|
|
||||||
|
this.messageObservable.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public waitForSetup() {
|
public waitForSetup() {
|
||||||
@ -234,6 +258,8 @@ export class LoggingBase {
|
|||||||
message: any[],
|
message: any[],
|
||||||
caller?: { file: string; line: number }
|
caller?: { file: string; line: number }
|
||||||
) {
|
) {
|
||||||
|
if (this.$closed) return;
|
||||||
|
|
||||||
let date = new Date().toISOString().replace(/T/, " ").replace(/\..+/, "");
|
let date = new Date().toISOString().replace(/T/, " ").replace(/\..+/, "");
|
||||||
|
|
||||||
let file_raw = caller || getCallerFile();
|
let file_raw = caller || getCallerFile();
|
||||||
|
14
src/types.ts
14
src/types.ts
@ -107,13 +107,19 @@ export interface Message {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface Adapter {
|
export interface Adapter {
|
||||||
init(
|
/**
|
||||||
observable: ObservableInterface<Message>,
|
* This function initialises the Adapter. It might be called multiple times, when added to multiple instances
|
||||||
name?: string
|
* @param observable An observable to subscribe to messages
|
||||||
): void | Promise<void>;
|
*/
|
||||||
|
init(observable: ObservableInterface<Message>): void | Promise<void>;
|
||||||
|
|
||||||
flush(sync: true): void;
|
flush(sync: true): void;
|
||||||
flush(sync: false): void | Promise<void>;
|
flush(sync: false): void | Promise<void>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When a close function is available, it will be called when no logging instance references it anymore.
|
||||||
|
*
|
||||||
|
* WARNING: The adapter might be reinitialised, when it is added to a new Logging instance
|
||||||
|
*/
|
||||||
close?(): void;
|
close?(): void;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user