From 54a655d4f13870ff2cf15e35f6f9cf5628de9fe7 Mon Sep 17 00:00:00 2001 From: hzgotb Date: Mon, 10 Nov 2025 14:54:26 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=5Fclose=E7=AD=BE?= =?UTF-8?q?=E5=90=8D=E4=BE=BF=E4=BA=8E=E6=B4=BE=E7=94=9F=E7=B1=BBoverride?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 2761c45..e78670a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,7 +17,8 @@ export abstract class Base extends ReadyEventEmitter { options: BaseOptions; #closed = false; #localStorage: AsyncLocalStorage; - + /* @ts-expect-error just a placeholder, will be implemented in subclass */ + protected _close(): Promise; constructor(options?: BaseOptions) { super(); From e1477df9d88102e5ced0099fac5fd076e70f7a11 Mon Sep 17 00:00:00 2001 From: hzgotb Date: Mon, 10 Nov 2025 14:59:05 +0800 Subject: [PATCH 2/3] =?UTF-8?q?refactor:=20=E5=B0=86Base=E7=B1=BB=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E6=B3=9B=E5=9E=8B=E4=BB=A5=E6=94=AF=E6=8C=81=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E9=80=89=E9=A1=B9=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使Base类能够接受自定义的选项类型参数,提高类型安全性和灵活性 --- src/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index e78670a..4756229 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,13 +13,13 @@ export interface BaseOptions { [key: string]: any; } -export abstract class Base extends ReadyEventEmitter { - options: BaseOptions; +export abstract class Base = BaseOptions> extends ReadyEventEmitter { + options: O; #closed = false; #localStorage: AsyncLocalStorage; /* @ts-expect-error just a placeholder, will be implemented in subclass */ protected _close(): Promise; - constructor(options?: BaseOptions) { + constructor(options?: O) { super(); if (options?.initMethod) { @@ -44,7 +44,7 @@ export abstract class Base extends ReadyEventEmitter { }); }); } - this.options = options ?? {}; + this.options = options ?? ({} as O); this.#localStorage = this.options.localStorage ?? getAsyncLocalStorage(); super.on('error', err => { this._defaultErrorHandler(err); From 45ac3f92ddf565147ba4f39329b80ac3e42fd57e Mon Sep 17 00:00:00 2001 From: hzgotb Date: Mon, 10 Nov 2025 15:06:59 +0800 Subject: [PATCH 3/3] =?UTF-8?q?refactor:=20=E5=B0=86=5Fclose=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E6=94=B9=E4=B8=BA=E5=8F=AF=E9=80=89=E5=B9=B6=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 移除对Reflect.get的使用,直接检查并调用_close方法 --- src/index.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 4756229..d8bf958 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,8 +17,8 @@ export abstract class Base = BaseOptions> e options: O; #closed = false; #localStorage: AsyncLocalStorage; - /* @ts-expect-error just a placeholder, will be implemented in subclass */ - protected _close(): Promise; + /* just a method signature, will be implemented in subclass */ + protected _close?(): Promise; constructor(options?: O) { super(); @@ -100,13 +100,12 @@ export abstract class Base = BaseOptions> e return; } this.#closed = true; - const closeMethod = Reflect.get(this, '_close') as () => Promise; - if (typeof closeMethod !== 'function') { + if (typeof this._close !== 'function') { return; } try { - await closeMethod.apply(this); + await this._close(); } catch (err) { this.emit('error', err); }