lightning-pool 4.6.0 → 4.6.1

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.
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AbortError = void 0;
4
+ class AbortError extends Error {
5
+ }
6
+ exports.AbortError = AbortError;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ResourceState = exports.PoolState = void 0;
4
+ var PoolState;
5
+ (function (PoolState) {
6
+ PoolState[PoolState["IDLE"] = 0] = "IDLE";
7
+ PoolState[PoolState["STARTED"] = 1] = "STARTED";
8
+ PoolState[PoolState["CLOSING"] = 2] = "CLOSING";
9
+ PoolState[PoolState["CLOSED"] = 3] = "CLOSED";
10
+ })(PoolState || (exports.PoolState = PoolState = {}));
11
+ var ResourceState;
12
+ (function (ResourceState) {
13
+ ResourceState[ResourceState["IDLE"] = 0] = "IDLE";
14
+ ResourceState[ResourceState["ACQUIRED"] = 1] = "ACQUIRED";
15
+ ResourceState[ResourceState["VALIDATION"] = 2] = "VALIDATION";
16
+ })(ResourceState || (exports.ResourceState = ResourceState = {}));
package/cjs/index.js ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createPool = createPool;
4
+ const tslib_1 = require("tslib");
5
+ const pool_js_1 = require("./pool.js");
6
+ tslib_1.__exportStar(require("./abort-error.js"), exports);
7
+ tslib_1.__exportStar(require("./definitions.js"), exports);
8
+ tslib_1.__exportStar(require("./pool.js"), exports);
9
+ function createPool(factory, config) {
10
+ return new pool_js_1.Pool(factory, config);
11
+ }
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PoolOptions = void 0;
4
+ const events_1 = require("events");
5
+ const defaultValues = {
6
+ acquireMaxRetries: 0,
7
+ acquireRetryWait: 2000,
8
+ acquireTimeoutMillis: 0,
9
+ fifo: true,
10
+ idleTimeoutMillis: 30000,
11
+ houseKeepInterval: 1000,
12
+ min: 0,
13
+ minIdle: 0,
14
+ max: 10,
15
+ maxQueue: 1000,
16
+ validation: true,
17
+ };
18
+ class PoolOptions extends events_1.EventEmitter {
19
+ constructor(pool) {
20
+ super();
21
+ this.pool = pool;
22
+ this._acquireMaxRetries = defaultValues.acquireMaxRetries;
23
+ this._acquireRetryWait = defaultValues.acquireRetryWait;
24
+ this._acquireTimeoutMillis = defaultValues.acquireTimeoutMillis;
25
+ this._fifo = defaultValues.fifo;
26
+ this._idleTimeoutMillis = defaultValues.idleTimeoutMillis;
27
+ this._houseKeepInterval = defaultValues.houseKeepInterval;
28
+ this._min = defaultValues.min;
29
+ this._minIdle = defaultValues.minIdle;
30
+ this._max = defaultValues.max;
31
+ this._maxQueue = defaultValues.maxQueue;
32
+ this._validation = defaultValues.validation;
33
+ this.pool = pool;
34
+ }
35
+ get acquireMaxRetries() {
36
+ return this._acquireMaxRetries;
37
+ }
38
+ set acquireMaxRetries(val) {
39
+ this._acquireMaxRetries = val >= 0 ? val : defaultValues.acquireMaxRetries;
40
+ this.emit('change', 'acquireMaxRetries', this._acquireMaxRetries);
41
+ }
42
+ get acquireRetryWait() {
43
+ return this._acquireRetryWait;
44
+ }
45
+ set acquireRetryWait(val) {
46
+ this._acquireRetryWait = val >= 0 ? val : defaultValues.acquireRetryWait;
47
+ this.emit('change', 'acquireRetryWait', this._acquireRetryWait);
48
+ }
49
+ get acquireTimeoutMillis() {
50
+ return this._acquireTimeoutMillis;
51
+ }
52
+ set acquireTimeoutMillis(val) {
53
+ this._acquireTimeoutMillis =
54
+ val >= 0 ? val : defaultValues.acquireTimeoutMillis;
55
+ this.emit('change', 'acquireTimeoutMillis', this._acquireTimeoutMillis);
56
+ }
57
+ get fifo() {
58
+ return this._fifo;
59
+ }
60
+ set fifo(val) {
61
+ // noinspection PointlessBooleanExpressionJS
62
+ this._fifo = !!val;
63
+ this.emit('change', 'fifo', this.fifo);
64
+ }
65
+ get idleTimeoutMillis() {
66
+ return this._idleTimeoutMillis;
67
+ }
68
+ set idleTimeoutMillis(val) {
69
+ this._idleTimeoutMillis = val >= 0 ? val : defaultValues.idleTimeoutMillis;
70
+ this.emit('change', 'idleTimeoutMillis', this._idleTimeoutMillis);
71
+ }
72
+ get houseKeepInterval() {
73
+ return this._houseKeepInterval;
74
+ }
75
+ set houseKeepInterval(val) {
76
+ this._houseKeepInterval = val >= 0 ? val : defaultValues.houseKeepInterval;
77
+ this.emit('change', 'houseKeepInterval', this._houseKeepInterval);
78
+ }
79
+ get min() {
80
+ return this._min;
81
+ }
82
+ set min(val) {
83
+ this._min = val >= 0 ? val : defaultValues.min;
84
+ if (this._min > this._max)
85
+ this._max = this._min;
86
+ this.emit('change', 'min', this._min);
87
+ }
88
+ get minIdle() {
89
+ return this._minIdle;
90
+ }
91
+ set minIdle(val) {
92
+ this._minIdle = val >= 0 ? val : defaultValues.minIdle;
93
+ this.emit('change', 'minIdle', this.minIdle);
94
+ }
95
+ get max() {
96
+ return this._max;
97
+ }
98
+ set max(val) {
99
+ this._max = val >= 0 ? Math.max(val, 1) : defaultValues.max;
100
+ if (this._min > this._max)
101
+ this._min = this._max;
102
+ this.emit('change', 'max', this._max);
103
+ }
104
+ get maxQueue() {
105
+ return this._maxQueue;
106
+ }
107
+ set maxQueue(val) {
108
+ this._maxQueue = val >= 0 ? Math.max(val, 1) : defaultValues.maxQueue;
109
+ this.emit('change', 'maxQueue', this._maxQueue);
110
+ }
111
+ get validation() {
112
+ return this._validation;
113
+ }
114
+ set validation(val) {
115
+ // noinspection PointlessBooleanExpressionJS
116
+ this._validation = !!val;
117
+ this.emit('change', 'validation', this._validation);
118
+ }
119
+ assign(values) {
120
+ const proto = Object.getPrototypeOf(this);
121
+ for (const k of Object.keys(values)) {
122
+ const desc = Object.getOwnPropertyDescriptor(proto, k);
123
+ if (desc && desc.set)
124
+ this[k] = values[k];
125
+ }
126
+ }
127
+ }
128
+ exports.PoolOptions = PoolOptions;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PoolRequest = void 0;
4
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
5
+ function noop() { }
6
+ class PoolRequest {
7
+ constructor(pool, callback, options) {
8
+ this.timedOut = false;
9
+ this.created = Date.now();
10
+ this.callback = callback || noop;
11
+ this.options = options;
12
+ if (pool.options.acquireTimeoutMillis) {
13
+ this.timeoutHandle = setTimeout(() => {
14
+ this.timedOut = true;
15
+ this.stopTimout();
16
+ pool.emit('request-timeout');
17
+ this.callback(new Error('Request timed out'));
18
+ }, pool.options.acquireTimeoutMillis);
19
+ }
20
+ }
21
+ stopTimout() {
22
+ if (this.timeoutHandle)
23
+ clearTimeout(this.timeoutHandle);
24
+ }
25
+ }
26
+ exports.PoolRequest = PoolRequest;
package/cjs/pool.js ADDED
@@ -0,0 +1,476 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Pool = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const doublylinked_1 = tslib_1.__importDefault(require("doublylinked"));
6
+ const events_1 = require("events");
7
+ const putil_promisify_1 = tslib_1.__importDefault(require("putil-promisify"));
8
+ const abort_error_js_1 = require("./abort-error.js");
9
+ const definitions_js_1 = require("./definitions.js");
10
+ const pool_options_js_1 = require("./pool-options.js");
11
+ const pool_request_js_1 = require("./pool-request.js");
12
+ const resource_item_js_1 = require("./resource-item.js");
13
+ class Pool extends events_1.EventEmitter {
14
+ constructor(factory, config) {
15
+ super();
16
+ this._requestQueue = new doublylinked_1.default();
17
+ this._allResources = new Map();
18
+ this._acquiredResources = new doublylinked_1.default();
19
+ this._idleResources = new doublylinked_1.default();
20
+ this._creating = 0;
21
+ this._requestsProcessing = 0;
22
+ this._state = definitions_js_1.PoolState.IDLE;
23
+ if (typeof factory !== 'object') {
24
+ throw new TypeError('You must provide `factory` object');
25
+ }
26
+ if (typeof factory.create !== 'function') {
27
+ throw new TypeError('factory.create must be a function');
28
+ }
29
+ if (typeof factory.destroy !== 'function') {
30
+ throw new TypeError('factory.destroy must be a function');
31
+ }
32
+ if (factory.validate && typeof factory.validate !== 'function') {
33
+ throw new TypeError('factory.validate can be a function');
34
+ }
35
+ if (factory.reset && typeof factory.reset !== 'function') {
36
+ throw new TypeError('factory.reset can be a function');
37
+ }
38
+ const opts = (this._options = new pool_options_js_1.PoolOptions(this));
39
+ opts.on('change', (prop, val) => {
40
+ if (prop === 'houseKeepInterval')
41
+ this._setHouseKeep(val);
42
+ if (prop === 'min' || prop === 'minIdle')
43
+ this._ensureMin();
44
+ });
45
+ if (config)
46
+ this.options.assign(config);
47
+ this._factory = factory;
48
+ }
49
+ /**
50
+ * Returns Pool options
51
+ */
52
+ get options() {
53
+ return this._options;
54
+ }
55
+ /**
56
+ * Returns number of resources that are currently acquired
57
+ */
58
+ get acquired() {
59
+ return this._acquiredResources.length;
60
+ }
61
+ /**
62
+ * Returns number of unused resources in the pool
63
+ */
64
+ get available() {
65
+ return this._idleResources.length;
66
+ }
67
+ /**
68
+ * Returns number of resources currently creating
69
+ */
70
+ get creating() {
71
+ return this._creating;
72
+ }
73
+ /**
74
+ * Returns number of callers waiting to acquire a resource
75
+ */
76
+ get pending() {
77
+ return this._requestQueue.length + this._requestsProcessing;
78
+ }
79
+ /**
80
+ * Returns number of resources in the pool
81
+ * regardless of whether they are idle or in use
82
+ */
83
+ get size() {
84
+ return this._allResources.size;
85
+ }
86
+ /**
87
+ * Returns state of the pool
88
+ */
89
+ get state() {
90
+ return this._state;
91
+ }
92
+ /**
93
+ * Starts the pool and begins creating of resources, starts house keeping and any other internal logic.
94
+ * Note: This method is not need to be called. Pool instance will automatically be started when acquire() method is called
95
+ */
96
+ start() {
97
+ if (this._state === definitions_js_1.PoolState.STARTED)
98
+ return;
99
+ if (this._state >= definitions_js_1.PoolState.CLOSING) {
100
+ throw new Error('Closed pool can not be started again');
101
+ }
102
+ this._state = definitions_js_1.PoolState.STARTED;
103
+ this._setHouseKeep(this.options.houseKeepInterval);
104
+ this._ensureMin();
105
+ this.emit('start');
106
+ }
107
+ close(arg0, arg1) {
108
+ let terminateWait = Infinity;
109
+ let callback;
110
+ if (typeof arg0 === 'function')
111
+ callback = arg0;
112
+ else {
113
+ terminateWait = typeof arg0 === 'number' ? arg0 : arg0 ? 0 : Infinity;
114
+ callback = arg1;
115
+ }
116
+ if (!callback) {
117
+ return putil_promisify_1.default.fromCallback(cb => this.close(terminateWait, cb));
118
+ }
119
+ if (this._state === definitions_js_1.PoolState.CLOSED || this._state === definitions_js_1.PoolState.IDLE) {
120
+ return callback();
121
+ }
122
+ if (this._state === definitions_js_1.PoolState.CLOSING) {
123
+ this.once('close', callback);
124
+ return;
125
+ }
126
+ this.emit('closing');
127
+ if (this._houseKeepTimer)
128
+ clearTimeout(this._houseKeepTimer);
129
+ this._state = definitions_js_1.PoolState.CLOSING;
130
+ this._requestQueue.forEach(t => t.stopTimout());
131
+ this._requestQueue = new doublylinked_1.default();
132
+ this._requestsProcessing = 0;
133
+ if (terminateWait <= 0) {
134
+ this._acquiredResources.forEach(t => this.destroy(t.resource));
135
+ }
136
+ else {
137
+ const startTime = Date.now();
138
+ this._closeWaitTimer = setInterval(() => {
139
+ if (!this._allResources.size) {
140
+ clearInterval(this._closeWaitTimer);
141
+ this._closeWaitTimer = undefined;
142
+ return;
143
+ }
144
+ if (Date.now() > startTime + terminateWait) {
145
+ clearInterval(this._closeWaitTimer);
146
+ this._closeWaitTimer = undefined;
147
+ this._acquiredResources.forEach(t => this.release(t.resource));
148
+ this.emit('terminate');
149
+ }
150
+ }, 50);
151
+ }
152
+ this._setHouseKeep(5);
153
+ this.once('close', callback);
154
+ }
155
+ closeAsync(arg0) {
156
+ return putil_promisify_1.default.fromCallback(cb => this.close(arg0, cb));
157
+ }
158
+ acquire(callback) {
159
+ if (!callback)
160
+ return putil_promisify_1.default.fromCallback(cb => this.acquire(cb));
161
+ try {
162
+ this.start();
163
+ }
164
+ catch (e) {
165
+ return callback(e);
166
+ }
167
+ if (this.options.maxQueue && this.pending >= this.options.maxQueue) {
168
+ return callback(new Error('Pool queue is full'));
169
+ }
170
+ this._requestQueue.push(new pool_request_js_1.PoolRequest(this, callback));
171
+ this._processNextRequest();
172
+ }
173
+ /**
174
+ * Releases an allocated `resource` and let it back to pool.
175
+ */
176
+ release(resource, callback) {
177
+ const item = this._allResources.get(resource);
178
+ if (item && item.state !== definitions_js_1.ResourceState.IDLE) {
179
+ this._itemSetIdle(item, callback);
180
+ }
181
+ this._processNextRequest();
182
+ }
183
+ /**
184
+ * Async version of release().
185
+ */
186
+ releaseAsync(resource) {
187
+ return putil_promisify_1.default.fromCallback(cb => this.release(resource, cb));
188
+ }
189
+ /**
190
+ * Releases, destroys and removes any `resource` from `Pool`.
191
+ */
192
+ destroy(resource, callback) {
193
+ try {
194
+ const item = this._allResources.get(resource);
195
+ if (item)
196
+ this._itemDestroy(item, callback);
197
+ else if (callback)
198
+ callback();
199
+ }
200
+ finally {
201
+ this._processNextRequest();
202
+ }
203
+ }
204
+ /**
205
+ * Async version of destroy().
206
+ */
207
+ destroyAsync(resource) {
208
+ return putil_promisify_1.default.fromCallback(cb => this.destroy(resource, cb));
209
+ }
210
+ /**
211
+ * Returns if a `resource` has been acquired from the pool and not yet released or destroyed.
212
+ */
213
+ isAcquired(resource) {
214
+ const item = this._allResources.get(resource);
215
+ return !!(item && item.acquiredNode);
216
+ }
217
+ /**
218
+ * Returns if the pool contains a `resource`
219
+ */
220
+ includes(resource) {
221
+ return this._allResources.has(resource);
222
+ }
223
+ _processNextRequest() {
224
+ if (this._state !== definitions_js_1.PoolState.STARTED ||
225
+ this._requestsProcessing >= this.options.max - this.acquired) {
226
+ return;
227
+ }
228
+ const request = this._requestQueue.shift();
229
+ if (!request)
230
+ return;
231
+ this._requestsProcessing++;
232
+ const handleCallback = (err, item) => {
233
+ this._requestsProcessing--;
234
+ request.stopTimout();
235
+ try {
236
+ if (item) {
237
+ /* istanbul ignore next : Hard to simulate */
238
+ if (this._state !== definitions_js_1.PoolState.STARTED) {
239
+ this._itemDestroy(item);
240
+ return;
241
+ }
242
+ this._itemSetAcquired(item);
243
+ this._ensureMin();
244
+ request.callback(undefined, item.resource);
245
+ this.emit('acquire', item.resource);
246
+ }
247
+ else
248
+ request.callback(err);
249
+ }
250
+ catch (ignored) {
251
+ // ignored
252
+ }
253
+ this._processNextRequest();
254
+ };
255
+ const item = this._idleResources.shift();
256
+ if (item) {
257
+ /* Validate resource */
258
+ if (this.options.validation && this._factory.validate) {
259
+ this._itemValidate(item, (err) => {
260
+ /* Destroy resource on validation error */
261
+ if (err) {
262
+ this._itemDestroy(item);
263
+ this.emit('validate-error', err, item.resource);
264
+ this._requestsProcessing--;
265
+ this._requestQueue.unshift(request);
266
+ this._processNextRequest();
267
+ }
268
+ else
269
+ handleCallback(undefined, item);
270
+ });
271
+ return;
272
+ }
273
+ return handleCallback(undefined, item);
274
+ }
275
+ /** There is no idle resource. We need to create new one **/
276
+ this._createResource(request, handleCallback);
277
+ }
278
+ emit(event, ...args) {
279
+ // Prevents errors while calling emit()
280
+ try {
281
+ return super.emit(event, ...args);
282
+ }
283
+ catch (e) {
284
+ return true;
285
+ }
286
+ }
287
+ /**
288
+ * Creates new resource
289
+ */
290
+ _createResource(request, callback) {
291
+ const maxRetries = this.options.acquireMaxRetries;
292
+ let tries = 0;
293
+ this._creating++;
294
+ const handleCallback = (err, obj) => {
295
+ if (request && request.timedOut)
296
+ return;
297
+ if (err || !obj) {
298
+ tries++;
299
+ this.emit('error', err, {
300
+ requestTime: request ? request.created : Date.now(),
301
+ tries,
302
+ maxRetries: this.options.acquireMaxRetries,
303
+ });
304
+ if (err instanceof abort_error_js_1.AbortError || tries >= maxRetries) {
305
+ this._creating--;
306
+ return callback && callback(err);
307
+ }
308
+ return setTimeout(() => tryCreate(), this.options.acquireRetryWait);
309
+ }
310
+ this._creating--;
311
+ if (this._allResources.has(obj)) {
312
+ return (callback &&
313
+ callback(new Error('Factory error. Resource already in pool')));
314
+ }
315
+ const item = new resource_item_js_1.ResourceItem(obj);
316
+ this._itemSetIdle(item);
317
+ this._allResources.set(obj, item);
318
+ if (callback)
319
+ callback(undefined, item);
320
+ this.emit('create', obj);
321
+ };
322
+ const tryCreate = () => {
323
+ try {
324
+ const o = this._factory.create({ tries, maxRetries });
325
+ /* istanbul ignore next */
326
+ if (!o) {
327
+ return handleCallback(new abort_error_js_1.AbortError('Factory returned no resource'));
328
+ }
329
+ putil_promisify_1.default.await(o, handleCallback);
330
+ }
331
+ catch (e) {
332
+ handleCallback(e);
333
+ }
334
+ };
335
+ tryCreate();
336
+ }
337
+ _setHouseKeep(ms) {
338
+ if (this._houseKeepTimer)
339
+ clearInterval(this._houseKeepTimer);
340
+ this._houseKeepTimer = undefined;
341
+ if ((ms > 0 && this.state === definitions_js_1.PoolState.STARTED) ||
342
+ this.state === definitions_js_1.PoolState.CLOSING) {
343
+ this._houseKeepTimer = setInterval(() => this._houseKeep(), ms);
344
+ }
345
+ }
346
+ _houseKeep() {
347
+ const isClosing = this._state === definitions_js_1.PoolState.CLOSING;
348
+ const now = Date.now();
349
+ let m = this._allResources.size - this.options.min;
350
+ let n = this._idleResources.length - this.options.minIdle;
351
+ if (isClosing || (m > 0 && n > 0)) {
352
+ this._idleResources.every((item) => {
353
+ if (isClosing || item.idleTime + this.options.idleTimeoutMillis < now) {
354
+ this._itemDestroy(item);
355
+ return isClosing || !!(--n && --m);
356
+ }
357
+ return false;
358
+ });
359
+ }
360
+ if (isClosing) {
361
+ /* Check again 5 ms later */
362
+ if (this._allResources.size)
363
+ return;
364
+ clearInterval(this._houseKeepTimer);
365
+ this._state = definitions_js_1.PoolState.CLOSED;
366
+ this._requestsProcessing = 0;
367
+ this.emit('close');
368
+ }
369
+ }
370
+ _ensureMin() {
371
+ process.nextTick(() => {
372
+ let k = Math.max(this.options.min - this._allResources.size, this.options.minIdle - this._idleResources.length) - this.creating;
373
+ while (k-- > 0)
374
+ this._createResource();
375
+ });
376
+ }
377
+ _itemSetAcquired(item) {
378
+ if (item.state !== definitions_js_1.ResourceState.ACQUIRED) {
379
+ this._itemDetach(item);
380
+ item.state = definitions_js_1.ResourceState.ACQUIRED;
381
+ this._acquiredResources.push(item);
382
+ item.acquiredNode = this._acquiredResources.tail;
383
+ }
384
+ }
385
+ _itemDetach(item) {
386
+ switch (item.state) {
387
+ case definitions_js_1.ResourceState.IDLE:
388
+ item.idleTime = 0;
389
+ /* istanbul ignore next*/
390
+ if (item.idleNode)
391
+ item.idleNode.remove();
392
+ item.idleNode = undefined;
393
+ break;
394
+ case definitions_js_1.ResourceState.ACQUIRED:
395
+ case definitions_js_1.ResourceState.VALIDATION:
396
+ /* istanbul ignore next*/
397
+ if (item.acquiredNode)
398
+ item.acquiredNode.remove();
399
+ item.acquiredNode = undefined;
400
+ break;
401
+ default:
402
+ break;
403
+ }
404
+ }
405
+ _itemSetIdle(item, callback) {
406
+ const isAcquired = item.state === definitions_js_1.ResourceState.ACQUIRED;
407
+ const handleCallback = (err) => {
408
+ if (err)
409
+ return this._itemDestroy(item, callback);
410
+ this._itemDetach(item);
411
+ item.idleTime = Date.now();
412
+ item.state = definitions_js_1.ResourceState.IDLE;
413
+ if (this.options.fifo) {
414
+ this._idleResources.push(item);
415
+ item.idleNode = this._idleResources.tail;
416
+ }
417
+ else {
418
+ this._idleResources.unshift(item);
419
+ item.idleNode = this._idleResources.head;
420
+ }
421
+ if (isAcquired)
422
+ this.emit('return', item.resource);
423
+ if (callback)
424
+ callback();
425
+ // noinspection JSAccessibilityCheck
426
+ this._processNextRequest();
427
+ };
428
+ if (isAcquired && this._factory.reset) {
429
+ try {
430
+ const o = this._factory.reset(item.resource);
431
+ putil_promisify_1.default.await(o, handleCallback);
432
+ }
433
+ catch (e) {
434
+ handleCallback(e);
435
+ }
436
+ }
437
+ else
438
+ handleCallback();
439
+ }
440
+ _itemDestroy(item, callback) {
441
+ this._itemDetach(item);
442
+ const handleCallback = (err) => {
443
+ if (err) {
444
+ this.emit('destroy-error', err, item.resource);
445
+ /* istanbul ignore next */
446
+ return callback && callback(err);
447
+ }
448
+ this.emit('destroy', item.resource);
449
+ item.destroyed = true;
450
+ if (callback)
451
+ callback();
452
+ };
453
+ try {
454
+ this._allResources.delete(item.resource);
455
+ this._processNextRequest();
456
+ const o = this._factory.destroy(item.resource);
457
+ putil_promisify_1.default.await(o, handleCallback);
458
+ }
459
+ catch (e) {
460
+ handleCallback(e);
461
+ }
462
+ }
463
+ _itemValidate(item, callback) {
464
+ item.state = definitions_js_1.ResourceState.VALIDATION;
465
+ try {
466
+ const o = this._factory.validate?.(item.resource);
467
+ // @ts-ignore
468
+ putil_promisify_1.default.await(o, callback);
469
+ }
470
+ catch (e) {
471
+ if (callback)
472
+ callback(e);
473
+ }
474
+ }
475
+ }
476
+ exports.Pool = Pool;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ResourceItem = void 0;
4
+ const definitions_js_1 = require("./definitions.js");
5
+ class ResourceItem {
6
+ constructor(resource) {
7
+ this.state = definitions_js_1.ResourceState.IDLE;
8
+ this.idleTime = 0;
9
+ this.destroyed = false;
10
+ this.resource = resource;
11
+ }
12
+ }
13
+ exports.ResourceItem = ResourceItem;
@@ -0,0 +1,2 @@
1
+ export class AbortError extends Error {
2
+ }