@orkestrel/pool 0.0.9 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -12
- package/dist/src/core/index.cjs +49 -26
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +289 -231
- package/dist/src/core/index.d.ts +289 -231
- package/dist/src/core/index.js +49 -26
- package/dist/src/core/index.js.map +1 -1
- package/package.json +11 -13
package/dist/src/core/index.d.ts
CHANGED
|
@@ -1,254 +1,312 @@
|
|
|
1
|
-
import { EmitterErrorHandler } from '@orkestrel/emitter';
|
|
2
|
-
import { EmitterHooks } from '@orkestrel/emitter';
|
|
3
|
-
import { EmitterInterface } from '@orkestrel/emitter';
|
|
1
|
+
import type { EmitterErrorHandler } from '@orkestrel/emitter';
|
|
2
|
+
import type { EmitterHooks } from '@orkestrel/emitter';
|
|
3
|
+
import type { EmitterInterface } from '@orkestrel/emitter';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Creates a distinct {@link PoolInterface} from resource lifecycle hooks, with optional bounded
|
|
7
|
+
* capacity, unique ownership, and FIFO settlement.
|
|
7
8
|
*
|
|
8
9
|
* @remarks
|
|
9
10
|
* Concurrent create and validation hooks may overlap, while acquire promises settle in
|
|
10
11
|
* request order. `clear` owns its idle snapshot; `destroy` returns one stable barrier and
|
|
11
|
-
* waits for every in-flight
|
|
12
|
+
* waits for every in-flight create, validation, and destroy attempt before destroying the
|
|
13
|
+
* emitter last.
|
|
12
14
|
*
|
|
13
15
|
* @typeParam T - The pooled resource type
|
|
14
16
|
* @param options - Lifecycle hooks, optional positive safe `max`, and observation hooks
|
|
15
17
|
* @returns A working {@link PoolInterface}
|
|
16
|
-
*
|
|
17
|
-
|
|
18
|
-
* ```ts
|
|
19
|
-
* import { createPool } from '@orkestrel/pool'
|
|
20
|
-
*
|
|
21
|
-
* const pool = createPool<Connection>({
|
|
22
|
-
* create: () => connect(),
|
|
23
|
-
* destroy: (connection) => connection.close(),
|
|
24
|
-
* validate: (connection) => connection.alive,
|
|
25
|
-
* max: 8,
|
|
26
|
-
* })
|
|
27
|
-
*
|
|
28
|
-
* const token = await pool.acquire()
|
|
29
|
-
* try {
|
|
30
|
-
* await token.value.query('select 1')
|
|
31
|
-
* } finally {
|
|
32
|
-
* token.release()
|
|
33
|
-
* }
|
|
34
|
-
* ```
|
|
35
|
-
*/
|
|
36
|
-
export declare function createPool<T>(options: PoolOptions<T>): PoolInterface<T>;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Test whether an unknown value is a {@link PoolError}, returning `false` for hostile proxies.
|
|
40
|
-
*
|
|
41
|
-
* @param value - The unknown boundary value
|
|
42
|
-
* @returns Whether the value is a real `PoolError` instance
|
|
43
|
-
*
|
|
44
|
-
* @example
|
|
45
|
-
* ```ts
|
|
46
|
-
* isPoolError(new PoolError({ code: 'destroyed' })) // true
|
|
47
|
-
* isPoolError(new Error('other')) // false
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
|
-
export declare function isPoolError(value: unknown): value is PoolError;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Test whether a value is a valid finite pool maximum.
|
|
54
|
-
*
|
|
55
|
-
* @param value - The unknown maximum candidate
|
|
56
|
-
* @returns Whether the value is a positive safe integer
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```ts
|
|
60
|
-
* isPoolMax(8) // true
|
|
61
|
-
* isPoolMax(Infinity) // false
|
|
62
|
-
* ```
|
|
63
|
-
*/
|
|
64
|
-
export declare function isPoolMax(value: unknown): value is number;
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Test whether a value is a native `AbortSignal`, returning `false` for hostile proxies.
|
|
68
|
-
*
|
|
69
|
-
* @param value - The unknown signal candidate
|
|
70
|
-
* @returns Whether the value is a native `AbortSignal`
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* ```ts
|
|
74
|
-
* isPoolSignal(new AbortController().signal) // true
|
|
75
|
-
* isPoolSignal({ aborted: false }) // false
|
|
76
|
-
* ```
|
|
77
|
-
*/
|
|
78
|
-
export declare function isPoolSignal(value: unknown): value is AbortSignal;
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* A capacity-aware resource pool whose opaque ownership records preserve FIFO settlement,
|
|
82
|
-
* cancellation, exact lease release, and deterministic teardown under concurrent hooks.
|
|
83
|
-
*
|
|
84
|
-
* @typeParam T - The pooled resource value
|
|
85
|
-
*
|
|
86
|
-
* @example
|
|
87
|
-
* ```ts
|
|
88
|
-
* import { Pool } from '@orkestrel/pool'
|
|
89
|
-
*
|
|
90
|
-
* const pool = new Pool({ create: () => new Uint8Array(64), max: 2 })
|
|
91
|
-
* const token = await pool.acquire()
|
|
92
|
-
* try {
|
|
93
|
-
* consume(token.value)
|
|
94
|
-
* } finally {
|
|
95
|
-
* token.release()
|
|
96
|
-
* }
|
|
97
|
-
* await pool.destroy()
|
|
98
|
-
* ```
|
|
99
|
-
*/
|
|
100
|
-
export declare class Pool<T> implements PoolInterface<T> {
|
|
101
|
-
#private;
|
|
102
|
-
/**
|
|
103
|
-
* Construct a pool and synchronously validate its capacity contract.
|
|
104
|
-
*
|
|
105
|
-
* @param options - Resource hooks, observation hooks, and optional positive safe `max`
|
|
106
|
-
*/
|
|
107
|
-
constructor(options: PoolOptions<T>);
|
|
108
|
-
/** The typed synchronous lifecycle observation surface. */
|
|
109
|
-
get emitter(): EmitterInterface<PoolEventMap>;
|
|
110
|
-
/** All owned records, including records validating or destroying. */
|
|
111
|
-
get size(): number;
|
|
112
|
-
/** Records immediately available without validation work. */
|
|
113
|
-
get idle(): number;
|
|
114
|
-
/** Records represented by unsettled released-once lease tokens. */
|
|
115
|
-
get active(): number;
|
|
116
|
-
/**
|
|
117
|
-
* Queue and lease one resource in FIFO settlement order.
|
|
18
|
+
* @throws {@link PoolError} Thrown when `options.max` is present and is not a positive safe
|
|
19
|
+
* integer, with `code: 'invalid'`. Construction validates it synchronously, before the pool exists.
|
|
118
20
|
*
|
|
119
|
-
* @
|
|
120
|
-
*
|
|
121
|
-
|
|
122
|
-
acquire(signal?: AbortSignal): Promise<PoolToken<T>>;
|
|
123
|
-
/**
|
|
124
|
-
* Destroy the records that are idle at this call's synchronous snapshot.
|
|
21
|
+
* @example Create a pool
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { createPool } from '@orkestrel/pool'
|
|
125
24
|
*
|
|
126
|
-
*
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
*
|
|
25
|
+
* const pool = createPool<Connection>({
|
|
26
|
+
* create: () => connect(),
|
|
27
|
+
* destroy: (connection) => connection.close(),
|
|
28
|
+
* validate: (connection) => connection.alive,
|
|
29
|
+
* max: 8,
|
|
30
|
+
* })
|
|
131
31
|
*
|
|
132
|
-
*
|
|
32
|
+
* const token = await pool.acquire()
|
|
33
|
+
* try {
|
|
34
|
+
* await token.value.query('select 1')
|
|
35
|
+
* } finally {
|
|
36
|
+
* token.release()
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
133
39
|
*/
|
|
134
|
-
|
|
135
|
-
}
|
|
40
|
+
export declare function createPool<T>(options: PoolOptions<T>): PoolInterface<T>;
|
|
136
41
|
|
|
137
|
-
/**
|
|
138
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Tests whether an unknown value is a {@link PoolError}, returning `false` for hostile proxies.
|
|
44
|
+
*
|
|
45
|
+
* @param value - The unknown boundary value
|
|
46
|
+
* @returns True if the value is a real `PoolError` instance; false otherwise
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* isPoolError(new PoolError({ code: 'destroyed' })) // true
|
|
51
|
+
* isPoolError(new Error('other')) // false
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare function isPoolError(value: unknown): value is PoolError;
|
|
139
55
|
|
|
140
|
-
/**
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Tests whether a value is a positive safe integer, the only valid explicit pool maximum.
|
|
58
|
+
*
|
|
59
|
+
* @param value - The unknown maximum candidate
|
|
60
|
+
* @returns True if the value is a positive safe integer; false otherwise
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* isPoolMax(8) // true
|
|
65
|
+
* isPoolMax(Infinity) // false
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare function isPoolMax(value: unknown): value is number;
|
|
147
69
|
|
|
148
|
-
/**
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
export declare class PoolError extends Error {
|
|
163
|
-
/** Stable machine-readable failure category. */
|
|
164
|
-
readonly code: PoolCode;
|
|
165
|
-
/** Optional structured input or aggregate-cleanup details. */
|
|
166
|
-
readonly context: PoolContext | undefined;
|
|
167
|
-
/**
|
|
168
|
-
* Create a pool failure without coercing a hostile thrown value.
|
|
169
|
-
*
|
|
170
|
-
* @param options - Stable code plus optional cause and structured context
|
|
171
|
-
*/
|
|
172
|
-
constructor(options: PoolErrorOptions);
|
|
173
|
-
}
|
|
70
|
+
/**
|
|
71
|
+
* Tests whether a value is a native `AbortSignal` for the acquire boundary, returning `false`
|
|
72
|
+
* for hostile proxies.
|
|
73
|
+
*
|
|
74
|
+
* @param value - The unknown signal candidate
|
|
75
|
+
* @returns True if the value is a native `AbortSignal`; false otherwise
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* isPoolSignal(new AbortController().signal) // true
|
|
80
|
+
* isPoolSignal({ aborted: false }) // false
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export declare function isPoolSignal(value: unknown): value is AbortSignal;
|
|
174
84
|
|
|
175
|
-
/**
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
85
|
+
/**
|
|
86
|
+
* Represents a capacity-aware resource pool whose opaque ownership records preserve FIFO
|
|
87
|
+
* settlement, cancellation, exact lease release, and deterministic teardown under concurrent
|
|
88
|
+
* hooks.
|
|
89
|
+
*
|
|
90
|
+
* @typeParam T - The pooled resource value
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* import { Pool } from '@orkestrel/pool'
|
|
95
|
+
*
|
|
96
|
+
* const pool = new Pool({ create: () => new Uint8Array(64), max: 2 })
|
|
97
|
+
* const token = await pool.acquire()
|
|
98
|
+
* try {
|
|
99
|
+
* consume(token.value)
|
|
100
|
+
* } finally {
|
|
101
|
+
* token.release()
|
|
102
|
+
* }
|
|
103
|
+
* await pool.destroy()
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export declare class Pool<T> implements PoolInterface<T> {
|
|
107
|
+
#private;
|
|
108
|
+
/**
|
|
109
|
+
* Constructs a pool and synchronously validates its capacity contract.
|
|
110
|
+
*
|
|
111
|
+
* @param options - Resource hooks, observation hooks, and optional positive safe `max`
|
|
112
|
+
* @throws {@link PoolError} Thrown when `options.max` is present and is not a positive safe
|
|
113
|
+
* integer, with `code: 'invalid'`.
|
|
114
|
+
*/
|
|
115
|
+
constructor(options: PoolOptions<T>);
|
|
116
|
+
/** Holds the typed synchronous lifecycle observation surface. */
|
|
117
|
+
get emitter(): EmitterInterface<PoolEventMap>;
|
|
118
|
+
/** Counts all owned records, including records validating or destroying. */
|
|
119
|
+
get size(): number;
|
|
120
|
+
/** Counts the records immediately available without validation work. */
|
|
121
|
+
get idle(): number;
|
|
122
|
+
/** Counts the records represented by unsettled released-once lease tokens. */
|
|
123
|
+
get active(): number;
|
|
124
|
+
/**
|
|
125
|
+
* Queues and leases one resource in FIFO settlement order.
|
|
126
|
+
*
|
|
127
|
+
* @param signal - Optional native cancellation signal
|
|
128
|
+
* @returns A promise for the unique resource lease
|
|
129
|
+
* @throws {@link PoolError} Thrown when `signal` is present and is not a native `AbortSignal`,
|
|
130
|
+
* with `code: 'invalid'`. This throw is synchronous rather than a rejected promise, so a caller
|
|
131
|
+
* that handles failures with `.catch()` alone misses it.
|
|
132
|
+
* @throws {@link PoolError} Thrown as a rejection when `destroy()` has already begun, with
|
|
133
|
+
* `code: 'destroyed'`; when the create hook fails, with `code: 'create'` and the hook's thrown
|
|
134
|
+
* value as `cause`; and when an invalid record's cleanup fails, with `code: 'cleanup'`. A
|
|
135
|
+
* `signal` that aborts rejects with the caller's exact `signal.reason` instead.
|
|
136
|
+
*/
|
|
137
|
+
acquire(signal?: AbortSignal): Promise<PoolToken<T>>;
|
|
138
|
+
/**
|
|
139
|
+
* Destroys the records that are idle at this call's synchronous snapshot.
|
|
140
|
+
*
|
|
141
|
+
* @returns A promise that settles after every snapshot cleanup attempt
|
|
142
|
+
* @throws {@link PoolError} Thrown when `destroy()` has already begun, with `code: 'destroyed'`.
|
|
143
|
+
* @throws {@link PoolError} Thrown when a claimed record's destroy hook fails, with
|
|
144
|
+
* `code: 'cleanup'` and every distinct failure in `context.failures`. Each arrives as a rejected
|
|
145
|
+
* promise rather than a synchronous throw.
|
|
146
|
+
*/
|
|
147
|
+
clear(): Promise<void>;
|
|
148
|
+
/**
|
|
149
|
+
* Tears down the pool permanently and returns its stable completion barrier.
|
|
150
|
+
*
|
|
151
|
+
* @returns The exact promise shared by every destroy call
|
|
152
|
+
* @throws {@link PoolError} Thrown when a destroy hook failed during teardown, with
|
|
153
|
+
* `code: 'cleanup'` and every distinct failure in `context.failures`. The barrier rejects; it
|
|
154
|
+
* never throws synchronously, and a repeat call receives the same rejected promise.
|
|
155
|
+
*/
|
|
156
|
+
destroy(): Promise<void>;
|
|
157
|
+
}
|
|
184
158
|
|
|
185
|
-
/**
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
/** A token settled successfully and its exact resource became leased. */
|
|
190
|
-
readonly acquire: readonly [];
|
|
191
|
-
/** A released resource became immediately idle. */
|
|
192
|
-
readonly release: readonly [];
|
|
193
|
-
/** A resource cleanup hook completed or was attempted when absent. */
|
|
194
|
-
readonly destroy: readonly [];
|
|
195
|
-
};
|
|
159
|
+
/**
|
|
160
|
+
* Names the machine-readable failure codes produced by {@link PoolError}.
|
|
161
|
+
*/
|
|
162
|
+
export declare type PoolCode = 'invalid' | 'destroyed' | 'create' | 'cleanup';
|
|
196
163
|
|
|
197
|
-
/**
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Queue and lease one resource in FIFO settlement order.
|
|
209
|
-
*
|
|
210
|
-
* @param signal - Optional native cancellation signal
|
|
211
|
-
* @returns A promise for the unique resource lease
|
|
212
|
-
*/
|
|
213
|
-
acquire(signal?: AbortSignal): Promise<PoolToken<T>>;
|
|
214
|
-
/**
|
|
215
|
-
* Destroy the records that are idle at this call's synchronous snapshot.
|
|
216
|
-
*
|
|
217
|
-
* @returns A promise that settles after every snapshot cleanup attempt
|
|
218
|
-
*/
|
|
219
|
-
clear(): Promise<void>;
|
|
220
|
-
/**
|
|
221
|
-
* Permanently tear down the pool and return its stable completion barrier.
|
|
222
|
-
*
|
|
223
|
-
* @returns The exact promise shared by every destroy call
|
|
224
|
-
*/
|
|
225
|
-
destroy(): Promise<void>;
|
|
226
|
-
}
|
|
164
|
+
/**
|
|
165
|
+
* Represents the structured context attached to a {@link PoolError}: the rejected input, or the
|
|
166
|
+
* distinct destroy-hook failures an aggregate cleanup collected.
|
|
167
|
+
*/
|
|
168
|
+
export declare interface PoolContext {
|
|
169
|
+
/** Holds the rejected public input, when the failure is an input-validation error. */
|
|
170
|
+
readonly value?: unknown;
|
|
171
|
+
/** Holds distinct destroy-hook failures collected by `clear()` or `destroy()`. */
|
|
172
|
+
readonly failures?: readonly unknown[];
|
|
173
|
+
}
|
|
227
174
|
|
|
228
|
-
/**
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
175
|
+
/**
|
|
176
|
+
* Represents a stable, machine-readable pool failure that retains the original thrown value as
|
|
177
|
+
* its cause without unsafe coercion, alongside structured context.
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* import { PoolError, isPoolError } from '@orkestrel/pool'
|
|
182
|
+
*
|
|
183
|
+
* try {
|
|
184
|
+
* await pool.acquire()
|
|
185
|
+
* } catch (error: unknown) {
|
|
186
|
+
* if (isPoolError(error)) console.error(error.code, error.cause)
|
|
187
|
+
* }
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
export declare class PoolError extends Error {
|
|
191
|
+
/** Holds the stable machine-readable failure category. */
|
|
192
|
+
readonly code: PoolCode;
|
|
193
|
+
/** Holds optional structured input or aggregate destroy-hook failure details. */
|
|
194
|
+
readonly context: PoolContext | undefined;
|
|
195
|
+
/**
|
|
196
|
+
* Creates a pool failure without coercing a hostile thrown value.
|
|
197
|
+
*
|
|
198
|
+
* @param options - Stable code plus optional cause and structured context
|
|
199
|
+
*/
|
|
200
|
+
constructor(options: PoolErrorOptions);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Represents the construction options for {@link PoolError}: the stable code, an optional cause,
|
|
205
|
+
* and optional structured context.
|
|
206
|
+
*/
|
|
207
|
+
export declare interface PoolErrorOptions {
|
|
208
|
+
/** Holds the stable machine-readable failure category. */
|
|
209
|
+
readonly code: PoolCode;
|
|
210
|
+
/** Holds the original thrown value, retained without unsafe string coercion. */
|
|
211
|
+
readonly cause?: unknown;
|
|
212
|
+
/** Holds optional structured failure details. */
|
|
213
|
+
readonly context?: PoolContext;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Represents the observable resource lifecycle events emitted by a {@link PoolInterface}.
|
|
218
|
+
*/
|
|
219
|
+
export declare type PoolEventMap = {
|
|
220
|
+
/** Signals that a created resource entered pool ownership. */
|
|
221
|
+
readonly create: readonly [];
|
|
222
|
+
/** Signals that a token settled successfully and its exact resource became leased. */
|
|
223
|
+
readonly acquire: readonly [];
|
|
224
|
+
/** Signals that a released resource became immediately idle. */
|
|
225
|
+
readonly release: readonly [];
|
|
226
|
+
/** Signals that a resource destroy hook completed or was attempted when absent. */
|
|
227
|
+
readonly destroy: readonly [];
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Represents a FIFO resource pool with optional bounded capacity and deterministic teardown,
|
|
232
|
+
* exposing its record counts and a typed lifecycle emitter.
|
|
233
|
+
*/
|
|
234
|
+
export declare interface PoolInterface<T> {
|
|
235
|
+
/** Holds the typed synchronous lifecycle observation surface. */
|
|
236
|
+
readonly emitter: EmitterInterface<PoolEventMap>;
|
|
237
|
+
/** Counts all owned records, including records validating or destroying. */
|
|
238
|
+
readonly size: number;
|
|
239
|
+
/** Counts the records immediately available without validation work. */
|
|
240
|
+
readonly idle: number;
|
|
241
|
+
/** Counts the records represented by unsettled released-once lease tokens. */
|
|
242
|
+
readonly active: number;
|
|
243
|
+
/**
|
|
244
|
+
* Queues the caller in FIFO order, validates an idle record or creates one, and settles queued
|
|
245
|
+
* acquires in that same order.
|
|
246
|
+
*
|
|
247
|
+
* @param signal - Optional native cancellation signal
|
|
248
|
+
* @returns A promise for the unique resource lease
|
|
249
|
+
* @throws {@link PoolError} Thrown when `signal` is present and is not a native `AbortSignal`,
|
|
250
|
+
* with `code: 'invalid'`. This throw is synchronous rather than a rejected promise, so a caller
|
|
251
|
+
* that handles failures with `.catch()` alone misses it.
|
|
252
|
+
* @throws {@link PoolError} Thrown as a rejection when `destroy()` has already begun, with
|
|
253
|
+
* `code: 'destroyed'`; when the create hook fails, with `code: 'create'` and the hook's thrown
|
|
254
|
+
* value as `cause`; and when an invalid record's cleanup fails, with `code: 'cleanup'`. A
|
|
255
|
+
* `signal` that aborts rejects with the caller's exact `signal.reason` instead.
|
|
256
|
+
*/
|
|
257
|
+
acquire(signal?: AbortSignal): Promise<PoolToken<T>>;
|
|
258
|
+
/**
|
|
259
|
+
* Destroys the records that are idle at this call's synchronous snapshot.
|
|
260
|
+
*
|
|
261
|
+
* @returns A promise that settles after every snapshot cleanup attempt
|
|
262
|
+
* @throws {@link PoolError} Thrown when `destroy()` has already begun, with `code: 'destroyed'`.
|
|
263
|
+
* @throws {@link PoolError} Thrown when a claimed record's destroy hook fails, with
|
|
264
|
+
* `code: 'cleanup'` and every distinct failure in `context.failures`. Each arrives as a rejected
|
|
265
|
+
* promise rather than a synchronous throw.
|
|
266
|
+
*/
|
|
267
|
+
clear(): Promise<void>;
|
|
268
|
+
/**
|
|
269
|
+
* Tears down the pool permanently and returns its stable completion barrier.
|
|
270
|
+
*
|
|
271
|
+
* @returns The exact promise shared by every destroy call
|
|
272
|
+
* @throws {@link PoolError} Thrown when a destroy hook failed during teardown, with
|
|
273
|
+
* `code: 'cleanup'` and every distinct failure in `context.failures`. The barrier rejects; it
|
|
274
|
+
* never throws synchronously, and a repeat call receives the same rejected promise.
|
|
275
|
+
*/
|
|
276
|
+
destroy(): Promise<void>;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Represents the resource lifecycle options for {@link Pool} and `createPool`: creation,
|
|
281
|
+
* destruction, validation, capacity, and observation.
|
|
282
|
+
*
|
|
283
|
+
* @remarks
|
|
284
|
+
* `create` lazily produces resources. `destroy` tears down a claimed resource.
|
|
285
|
+
* `validate` checks a previously owned resource before reuse. `max` is a positive
|
|
286
|
+
* safe integer; omission is the only unbounded form. `on` installs initial emitter
|
|
287
|
+
* listeners and `error` receives isolated listener failures.
|
|
288
|
+
*/
|
|
289
|
+
export declare interface PoolOptions<T> {
|
|
290
|
+
readonly on?: EmitterHooks<PoolEventMap>;
|
|
291
|
+
readonly error?: EmitterErrorHandler;
|
|
292
|
+
readonly create: () => Promise<T> | T;
|
|
293
|
+
readonly destroy?: (value: T) => Promise<void> | void;
|
|
294
|
+
readonly validate?: (value: T) => Promise<boolean> | boolean;
|
|
295
|
+
readonly max?: number;
|
|
296
|
+
}
|
|
245
297
|
|
|
246
|
-
/**
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
298
|
+
/**
|
|
299
|
+
* Represents a unique lease over one pool-owned resource record, exposing that record as a readonly
|
|
300
|
+
* `value` and returning it through an idempotent `release`.
|
|
301
|
+
*/
|
|
302
|
+
export declare interface PoolToken<T> {
|
|
303
|
+
/** Holds the leased value. Duplicate values still belong to independent records. */
|
|
304
|
+
readonly value: T;
|
|
305
|
+
/**
|
|
306
|
+
* Gives this exact record back to the pool once; a repeat call, and a call after teardown took
|
|
307
|
+
* ownership, are no-ops.
|
|
308
|
+
*/
|
|
309
|
+
release(): void;
|
|
310
|
+
}
|
|
253
311
|
|
|
254
|
-
export { }
|
|
312
|
+
export { }
|