/*! * V4Fire Core * https://github.com/V4Fire/Core * * Released under the MIT license * https://github.com/V4Fire/Core/blob/master/LICENSE */ import type Pool from 'core/pool'; import { hashVal, borrowCounter } from 'core/pool/const'; export type Resource = T & { [hashVal]: string; [borrowCounter]: number; }; export interface WrappedResource { /** * Value from the pool */ value: T; /** * Returns the resource to the pool * @param args - extra arguments to pass to the `onFree` hook handler */ free(...args: unknown[]): void; /** * Destroys the resource instead of returning it to the pool */ destroy(): void; } export interface OptionalWrappedResource extends WrappedResource> {} export interface PoolOptions { /** * Number of resources to create at pull initialization * @default `0` */ size?: number; /** * The maximum number of resources that the pool can contain * @default `Infinity` */ maxSize?: number; /** * A function to destroy one resource from the pool */ resourceDestructor?: ResourceDestructor; /** * A function to calculate a hash string for the specified arguments */ hashFn?: HashFn; /** * Handler: taking some resource via `take` methods */ onTake?: ResourceHook; /** * Handler: taking some resource via `borrow` methods */ onBorrow?: ResourceHook; /** * Handler: releasing of some resource */ onFree?: ResourceHook; /** * Handler: clearing of all pool resources */ onClear?: PoolHook; } export interface ArgsFn { (i: number): unknown[]; } export type Args = unknown[] | ArgsFn; export interface HashFn { (...args: unknown[]): string; } export interface ResourceFactory { (...args: unknown[]): T; } export interface ResourceDestructor { (resource: T): void; } export interface ResourceHook { (resource: T, pool: Pool, ...args: unknown[]): void; } export interface PoolHook { (pool: Pool, ...args: unknown[]): void; }