mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-12 13:00:19 +00:00
Add type definitions for generic-pool (#18741)
This commit is contained in:
committed by
Mohamed Hegazy
parent
b98d0d3152
commit
91fab979fc
@@ -0,0 +1,60 @@
|
||||
import * as genericPool from "generic-pool";
|
||||
|
||||
class Connection {
|
||||
connected: boolean;
|
||||
}
|
||||
|
||||
const factory = {
|
||||
create: (): Promise<Connection> => {
|
||||
return new Promise<Connection>(resolve => {
|
||||
let conn = new Connection();
|
||||
resolve(conn);
|
||||
});
|
||||
},
|
||||
destroy: (conn: Connection): Promise<undefined> => {
|
||||
return new Promise<undefined>(resolve => {
|
||||
conn.connected = false;
|
||||
resolve();
|
||||
});
|
||||
},
|
||||
validate: (conn: Connection): Promise<boolean> => {
|
||||
return new Promise<boolean>(resolve => {
|
||||
return conn.connected;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
let opts = {
|
||||
max: 10,
|
||||
min: 2,
|
||||
maxWaitingClients: 2,
|
||||
testOnBorrow: true,
|
||||
acquireTimeoutMillis: 100,
|
||||
fifo: true,
|
||||
priorityRange: 5,
|
||||
autostart: false,
|
||||
evictionRunIntervalMillis: 200,
|
||||
numTestsPerRun: 3,
|
||||
softIdleTimeoutMillis: 100,
|
||||
idleTimeoutMillis: 5000
|
||||
};
|
||||
|
||||
let pool = genericPool.createPool<Connection>(factory, opts);
|
||||
|
||||
pool.acquire()
|
||||
.then((conn: Connection) => {
|
||||
return pool.release(conn);
|
||||
}).then(() => {
|
||||
return pool.acquire(5);
|
||||
}).then((conn: Connection) => {
|
||||
return pool.destroy(conn);
|
||||
}).then(() => {
|
||||
return pool.clear();
|
||||
}).then((results: undefined[]) => {
|
||||
});
|
||||
|
||||
pool.on('factoryCreateError', (err: Error) => {
|
||||
});
|
||||
|
||||
pool.on('factoryDestroyError', (err: Error) => {
|
||||
});
|
||||
Vendored
+47
@@ -0,0 +1,47 @@
|
||||
// Type definitions for generic-pool 3.1
|
||||
// Project: https://github.com/coopernurse/node-pool#readme
|
||||
// Definitions by: Jerray Fu <https://github.com/jerray>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
import { EventEmitter } from "events";
|
||||
|
||||
export class Pool<T> extends EventEmitter {
|
||||
spareResourceCapacity: number;
|
||||
size: number;
|
||||
available: number;
|
||||
borrowed: number;
|
||||
pending: number;
|
||||
max: number;
|
||||
min: number;
|
||||
|
||||
acquire(priority?: number): Promise<T>;
|
||||
release(resource: T): void;
|
||||
destroy(resource: T): void;
|
||||
drain(): Promise<undefined>;
|
||||
clear(): Promise<undefined[]>;
|
||||
}
|
||||
|
||||
export interface Factory<T> {
|
||||
create(): Promise<T>;
|
||||
destroy(client: T): Promise<undefined>;
|
||||
validate?(client: T): Promise<boolean>;
|
||||
}
|
||||
|
||||
export interface Options {
|
||||
max?: number;
|
||||
min?: number;
|
||||
maxWaitingClients?: number;
|
||||
testOnBorrow?: boolean;
|
||||
acquireTimeoutMillis?: number;
|
||||
fifo?: boolean;
|
||||
priorityRange?: number;
|
||||
autostart?: boolean;
|
||||
evictionRunIntervalMillis?: number;
|
||||
numTestsPerRun?: number;
|
||||
softIdleTimeoutMillis?: number;
|
||||
idleTimeoutMillis?: number;
|
||||
}
|
||||
|
||||
export function createPool<T>(factory: Factory<T>, opts?: Options): Pool<T>;
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"generic-pool-tests.ts"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user