Skip to content

Commit

Permalink
v0.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayMakhonin committed Mar 25, 2023
1 parent 8525792 commit 56556eb
Show file tree
Hide file tree
Showing 56 changed files with 3,007 additions and 614 deletions.
512 changes: 512 additions & 0 deletions dist/bundle/browser.js

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions dist/lib/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var pool_Pool = require('./pool/Pool.cjs');
var pool_Pools = require('./pool/Pools.cjs');
var pool_PoolRunner = require('./pool/PoolRunner.cjs');
var pool_PoolWrapper = require('./pool/PoolWrapper.cjs');
var pool_PoolHoldError = require('./pool/PoolHoldError.cjs');
var pool_poolRunWait = require('./pool/poolRunWait.cjs');
var pool_poolRunThrow = require('./pool/poolRunThrow.cjs');
var objectPool_StackPool = require('./object-pool/StackPool.cjs');
var objectPool_ObjectPool = require('./object-pool/ObjectPool.cjs');
var objectPool_ObjectPoolWrapper = require('./object-pool/ObjectPoolWrapper.cjs');
var timeLimit_TimeLimitPool = require('./time-limit/TimeLimitPool.cjs');
require('tslib');
require('@flemist/async-utils');
require('@flemist/priority-queue');
require('@flemist/time-controller');



exports.Pool = pool_Pool.Pool;
exports.Pools = pool_Pools.Pools;
exports.PoolRunner = pool_PoolRunner.PoolRunner;
exports.PoolWrapper = pool_PoolWrapper.PoolWrapper;
exports.PoolHoldError = pool_PoolHoldError.PoolHoldError;
exports.poolRunWait = pool_poolRunWait.poolRunWait;
exports.poolRunThrow = pool_poolRunThrow.poolRunThrow;
exports.StackPool = objectPool_StackPool.StackPool;
exports.ObjectPool = objectPool_ObjectPool.ObjectPool;
exports.ObjectPoolWrapper = objectPool_ObjectPoolWrapper.ObjectPoolWrapper;
exports.TimeLimitPool = timeLimit_TimeLimitPool.TimeLimitPool;
3 changes: 3 additions & 0 deletions dist/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./pool";
export * from "./object-pool";
export * from "./time-limit";
15 changes: 15 additions & 0 deletions dist/lib/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export { Pool } from './pool/Pool.mjs';
export { Pools } from './pool/Pools.mjs';
export { PoolRunner } from './pool/PoolRunner.mjs';
export { PoolWrapper } from './pool/PoolWrapper.mjs';
export { PoolHoldError } from './pool/PoolHoldError.mjs';
export { poolRunWait } from './pool/poolRunWait.mjs';
export { poolRunThrow } from './pool/poolRunThrow.mjs';
export { StackPool } from './object-pool/StackPool.mjs';
export { ObjectPool } from './object-pool/ObjectPool.mjs';
export { ObjectPoolWrapper } from './object-pool/ObjectPoolWrapper.mjs';
export { TimeLimitPool } from './time-limit/TimeLimitPool.mjs';
import 'tslib';
import '@flemist/async-utils';
import '@flemist/priority-queue';
import '@flemist/time-controller';
170 changes: 170 additions & 0 deletions dist/lib/object-pool/ObjectPool.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var objectPool_StackPool = require('./StackPool.cjs');
var pool_Pool = require('../pool/Pool.cjs');
var pool_Pools = require('../pool/Pools.cjs');
require('@flemist/time-controller');
var asyncUtils = require('@flemist/async-utils');
require('@flemist/priority-queue');

class ObjectPool {
constructor({ pool, availableObjects, holdObjects, destroy, create, }) {
this._allocatePool = new pool_Pool.Pool(pool.maxSize);
this._pool = new pool_Pools.Pools(pool, this._allocatePool);
this._availableObjects = availableObjects || new objectPool_StackPool.StackPool();
this._holdObjects = holdObjects === true
? new Set()
: holdObjects || null;
this._create = create;
this._destroy = destroy;
}
get pool() {
return this._pool;
}
get availableObjects() {
return this._availableObjects.objects;
}
/** which objects are taken and not released to the pool */
get holdObjects() {
return this._holdObjects;
}
get(count) {
const objects = this._availableObjects.get(count);
if (this._holdObjects && objects) {
for (let i = 0, len = objects.length; i < len; i++) {
this._holdObjects.add(objects[i]);
}
}
return objects;
}
release(objects, start, end) {
return tslib.__awaiter(this, void 0, void 0, function* () {
if (start == null) {
start = 0;
}
if (end == null) {
end = objects.length;
}
const tryReleaseCount = end - start;
const releasedCount = yield this._pool.release(tryReleaseCount);
end = Math.min(objects.length, releasedCount);
this._availableObjects.release(objects, start, end);
if (this._holdObjects) {
for (let i = start; i < end; i++) {
const obj = objects[i];
if (obj != null) {
if (this._holdObjects) {
this._holdObjects.delete(obj);
}
}
}
}
return releasedCount;
});
}
tick(abortSignal) {
return this._pool.tick();
}
getWait(count, priority, abortSignal, awaitPriority) {
return tslib.__awaiter(this, void 0, void 0, function* () {
yield this._pool.holdWait(count, priority, abortSignal, awaitPriority);
return this.get(count);
});
}
use(count, func, priority, abortSignal, awaitPriority) {
return tslib.__awaiter(this, void 0, void 0, function* () {
let objects = yield this.getWait(count, priority, abortSignal, awaitPriority);
if (!this._create) {
throw new Error('You should specify create function in the constructor');
}
let start;
if (!objects) {
objects = new Array(count);
start = 0;
}
else {
start = objects.length;
}
for (let i = start; i < count; i++) {
const obj = yield this._create();
if (obj == null) {
throw new Error('create function should return not null object');
}
if (this._holdObjects) {
this._holdObjects.add(obj);
}
objects[i] = obj;
}
try {
const result = yield func(objects, abortSignal);
return result;
}
finally {
const releasedCount = yield this.release(objects);
if (this._destroy) {
for (let i = releasedCount, len = objects.length; i < len; i++) {
const obj = objects[i];
yield this._destroy(obj);
}
}
}
});
}
allocate(size) {
if (!this._create) {
throw new Error('You should specify create function in the constructor');
}
const promises = [];
let tryHoldCount = this._allocatePool.size - this._availableObjects.size;
if (size != null && size < tryHoldCount) {
tryHoldCount = size;
}
if (tryHoldCount < 0) {
throw new Error('Unexpected behavior: tryHoldCount < 0');
}
const holdCount = this._allocatePool.hold(tryHoldCount) ? tryHoldCount : 0;
let allocatedCount = 0;
const _this = this;
function releasePromiseObject(objectPromise) {
return tslib.__awaiter(this, void 0, void 0, function* () {
let obj;
try {
obj = yield objectPromise;
}
catch (err) {
yield _this._allocatePool.release(1);
throw err;
}
const count = yield _this.release([obj]);
allocatedCount += count;
});
}
function releasePromise(promise) {
return tslib.__awaiter(this, void 0, void 0, function* () {
const count = yield promise;
allocatedCount += count;
});
}
for (let i = 0; i < holdCount; i++) {
const objectOrPromise = this._create();
if (asyncUtils.isPromiseLike(objectOrPromise)) {
promises.push(releasePromiseObject(objectOrPromise));
}
else {
const promise = this.release([objectOrPromise]);
if (asyncUtils.isPromiseLike(promise)) {
promises.push(releasePromise(promise));
}
}
}
if (promises.length) {
return Promise.all(promises).then(o => allocatedCount);
}
return allocatedCount;
}
}

exports.ObjectPool = ObjectPool;
46 changes: 46 additions & 0 deletions dist/lib/object-pool/ObjectPool.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { IAbortSignalFast } from '@flemist/abort-controller-fast';
import { IStackPool } from "./StackPool";
import { IPool } from "../pool";
import { Priority, AwaitPriority } from '@flemist/priority-queue';
export interface IObjectPool<TObject extends object> {
readonly pool: IPool;
readonly availableObjects: ReadonlyArray<TObject>;
readonly holdObjects?: ReadonlySet<TObject>;
get(count: number): TObject[];
/** it returns false if the obj cannot be pushed into the object pool (if size >= maxSize) */
release(objects: TObject[], start?: number, count?: number): Promise<number> | number;
/** it will resolve when size > 0 */
tick(abortSignal?: IAbortSignalFast): Promise<void> | void;
/** wait available > 0 and get, use this for concurrency get */
getWait(count: number, priority?: Priority, abortSignal?: IAbortSignalFast, awaitPriority?: AwaitPriority): Promise<TObject[]>;
use<TResult>(count: number, func: (objects: ReadonlyArray<TObject>, abortSignal?: IAbortSignalFast) => Promise<TResult> | TResult, priority?: Priority, abortSignal?: IAbortSignalFast, awaitPriority?: AwaitPriority): Promise<TResult>;
allocate(size?: number): Promise<number> | number;
}
export declare type ObjectPoolArgs<TObject extends object> = {
pool: IPool;
/** custom availableObjects */
availableObjects?: IStackPool<TObject>;
/** use holdObjects so that you can know which objects are taken and not released to the pool */
holdObjects?: boolean | Set<TObject>;
create: () => Promise<TObject> | TObject;
destroy?: (obj: TObject) => Promise<void> | void;
};
export declare class ObjectPool<TObject extends object> implements IObjectPool<TObject> {
private readonly _pool;
private readonly _allocatePool;
private readonly _availableObjects;
private readonly _holdObjects;
private readonly _create?;
private readonly _destroy?;
constructor({ pool, availableObjects, holdObjects, destroy, create, }: ObjectPoolArgs<TObject>);
get pool(): IPool;
get availableObjects(): ReadonlyArray<TObject>;
/** which objects are taken and not released to the pool */
get holdObjects(): ReadonlySet<TObject>;
get(count: number): TObject[];
release(objects: TObject[], start?: number, end?: number): Promise<number>;
tick(abortSignal?: IAbortSignalFast): Promise<void> | void;
getWait(count: number, priority?: Priority, abortSignal?: IAbortSignalFast, awaitPriority?: AwaitPriority): Promise<TObject[]>;
use<TResult>(count: number, func: (objects: ReadonlyArray<TObject>, abortSignal?: IAbortSignalFast) => Promise<TResult> | TResult, priority?: Priority, abortSignal?: IAbortSignalFast, awaitPriority?: AwaitPriority): Promise<TResult>;
allocate(size?: number): Promise<number> | number;
}
Loading

0 comments on commit 56556eb

Please sign in to comment.