-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #975 from neet/ts-5.2.0
Add experimental support for explicit resource management
- Loading branch information
Showing
58 changed files
with
4,583 additions
and
4,474 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,3 @@ | ||
type UseFn<T, U> = (client: T) => Promise<U>; | ||
type UseFnMany<T, U> = (client: T[]) => Promise<U>; | ||
|
||
export interface Pool<T> { | ||
acquire(n?: 1 | undefined): Promise<T>; | ||
acquire(n: number): Promise<T[]>; | ||
release(token: T | T[]): Promise<void>; | ||
use<U>(fn: UseFn<T, U>): Promise<U>; | ||
use<U>(n: number, fn: UseFnMany<T, U>): Promise<U>; | ||
} | ||
|
||
export abstract class BasePool<T extends object> implements Pool<T> { | ||
protected abstract acquireOne(): Promise<T>; | ||
protected abstract releaseOne(client: T): Promise<void>; | ||
|
||
async acquire(n?: 1 | undefined): Promise<T>; | ||
async acquire(n: number): Promise<T[]>; | ||
async acquire(n = 1): Promise<T | T[]> { | ||
if (n === 1) { | ||
return this.acquireOne(); | ||
} | ||
return Promise.all(Array.from({ length: n }).map(() => this.acquireOne())); | ||
} | ||
|
||
async release(clients: T | T[]): Promise<void> { | ||
await (Array.isArray(clients) | ||
? Promise.all(clients.map((client) => this.releaseOne(client))) | ||
: this.releaseOne(clients)); | ||
} | ||
|
||
async use<U>(fn: UseFn<T, U>): Promise<T>; | ||
async use<U>(n: number, fn: UseFnMany<T, U>): Promise<T>; | ||
async use<U>( | ||
fnOrNumber: number | UseFn<T, U>, | ||
fnOrUndefined?: UseFnMany<T, U>, | ||
): Promise<U> { | ||
if (typeof fnOrNumber === "function" && fnOrUndefined == undefined) { | ||
const fn = fnOrNumber; | ||
const client = await this.acquire(1); | ||
|
||
try { | ||
return await fn(client); | ||
} finally { | ||
await this.release(client); | ||
} | ||
} | ||
|
||
if (typeof fnOrNumber === "number" && typeof fnOrUndefined === "function") { | ||
const n = fnOrNumber; | ||
const fn = fnOrUndefined; | ||
const clients = await this.acquire(n); | ||
|
||
try { | ||
return await fn(clients); | ||
} finally { | ||
await this.release(clients); | ||
} | ||
} | ||
|
||
throw new Error("Invalid arguments"); | ||
} | ||
acquire(): Promise<T>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.