Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update return type of validate function #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
15 changes: 6 additions & 9 deletions src/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface PoolOptions<T> {
createRetryIntervalMillis?: number;
reapIntervalMillis?: number;
log?: (msg: string) => any;
validate?: (resource: T) => boolean;
validate?: (resource: T) => Promise<boolean> | boolean;
propagateCreateError?: boolean;
}

Expand All @@ -41,7 +41,7 @@ export class Pool<T> {
protected log: (msg: string, level: 'warn') => any;
protected creator: CallbackOrPromise<T>;
protected destroyer: (resource: T) => any;
protected validate: (resource: T) => boolean;
protected validate: (resource: T) => Promise<boolean> | boolean;
protected eventId: number;
protected emitter = new EventEmitter();

Expand Down Expand Up @@ -474,13 +474,10 @@ export class Pool<T> {
return this.free.length > 0 && this.pendingAcquires.length > 0;
}

_validateResource(resource: T) {
try {
return Promise.resolve(this.validate(resource));
} catch (err) {
// prevent leaking of sync exception
return Promise.reject(err);
}
async _validateResource(resource: T) {
const result = await this.validate(resource);

return result;
}

_shouldCreateMoreResources() {
Expand Down
4 changes: 2 additions & 2 deletions src/tarn.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Pool } from './Pool';
import { Pool, PoolOptions } from './Pool';
import { TimeoutError } from './TimeoutError';

export { Pool, TimeoutError };
export { Pool, PoolOptions, TimeoutError };

module.exports = {
Pool,
Expand Down