Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ export interface BaseOptions<T = any> {
[key: string]: any;
}

export abstract class Base<T = any> extends ReadyEventEmitter {
options: BaseOptions<T>;
export abstract class Base<T = any, O extends BaseOptions<T> = BaseOptions<T>> extends ReadyEventEmitter {
options: O;
#closed = false;
#localStorage: AsyncLocalStorage<T>;

constructor(options?: BaseOptions<T>) {
/* just a method signature, will be implemented in subclass */
protected _close?(): Promise<void>;
constructor(options?: O) {
super();

if (options?.initMethod) {
Expand All @@ -43,7 +44,7 @@ export abstract class Base<T = any> extends ReadyEventEmitter {
});
});
}
this.options = options ?? {};
this.options = options ?? ({} as O);
this.#localStorage = this.options.localStorage ?? getAsyncLocalStorage<T>();
super.on('error', err => {
this._defaultErrorHandler(err);
Expand Down Expand Up @@ -99,13 +100,12 @@ export abstract class Base<T = any> extends ReadyEventEmitter {
return;
}
this.#closed = true;
const closeMethod = Reflect.get(this, '_close') as () => Promise<void>;
if (typeof closeMethod !== 'function') {
if (typeof this._close !== 'function') {
return;
}

try {
await closeMethod.apply(this);
await this._close();
} catch (err) {
this.emit('error', err);
}
Expand Down
Loading