From 9182efe7e73f70fca43824022f79108fb9d73a43 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Thu, 9 Apr 2020 18:00:59 +0200 Subject: [PATCH] Fixing some problems with the adapter API --- package.json | 2 +- src/base.ts | 34 ++++++++++++++++++++++++++++++---- src/types.ts | 14 ++++++++++---- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index b562c4b..041e11d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hibas123/logging", - "version": "2.2.2", + "version": "2.3.0", "description": "", "main": "out/index.js", "types": "out/index.d.ts", diff --git a/src/base.ts b/src/base.ts index 71e010c..12e067e 100644 --- a/src/base.ts +++ b/src/base.ts @@ -42,6 +42,7 @@ export interface LoggingBaseOptions { console: boolean; } +const adapterCache = new WeakMap(); export class LoggingBase { private _formatMap: FormatConfig = new DefaultFormatConfig(); @@ -110,8 +111,12 @@ export class LoggingBase { addAdapter(adapter: Adapter) { if (!this.adapter.has(adapter)) { this.adapter.add(adapter); + + let cached = adapterCache.get(adapter) || 0; + adapterCache.set(adapter, cached + 1); + let prms = Promise.resolve( - adapter.init(this.messageObservable.getPublicApi(), this._name) + adapter.init(this.messageObservable.getPublicApi()) ); this.adapter_init.push(prms); } @@ -129,10 +134,29 @@ export class LoggingBase { } } + private $closed = false; + public close() { - this.adapter.forEach((adapter) => - adapter.close ? adapter.close() : undefined - ); + if (this.$closed) return; + 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() { @@ -234,6 +258,8 @@ export class LoggingBase { message: any[], caller?: { file: string; line: number } ) { + if (this.$closed) return; + let date = new Date().toISOString().replace(/T/, " ").replace(/\..+/, ""); let file_raw = caller || getCallerFile(); diff --git a/src/types.ts b/src/types.ts index 4fa019e..ba3c641 100644 --- a/src/types.ts +++ b/src/types.ts @@ -107,13 +107,19 @@ export interface Message { } export interface Adapter { - init( - observable: ObservableInterface, - name?: string - ): void | Promise; + /** + * This function initialises the Adapter. It might be called multiple times, when added to multiple instances + * @param observable An observable to subscribe to messages + */ + init(observable: ObservableInterface): void | Promise; flush(sync: true): void; flush(sync: false): void | Promise; + /** + * 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; }