nope/lib/helpers/lazyMethods.ts

65 lines
1.8 KiB
TypeScript
Raw Normal View History

2020-09-10 16:21:01 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @desc [description]
*/
/**
* Sometimes, the creation of an new Instance is very slow. Therefore this Lazy-Constructor could be used.
* Instead of creating a new Instance, it looks for a not used and returns that Instance. By returning
* the unused Instance it is marked as used. After the usage the instance could marked as unused again.
* If there is no unused Instance available an new Instance is created.
*
* To utilize the Lazy-Constructor a specific create-instance method and a compare function is required.
*
* @export
* @class LazyConstructor
* @template T Args which are required to Construct an Instance
* @template U Type of the Instance
*/
export class LazyConstructor<T> {
2021-12-04 07:25:26 +00:00
private _inactive = new Set<T>();
2020-09-10 16:21:01 +00:00
2021-12-04 07:25:26 +00:00
/**
* Creates an instance of LazyConstructor.
* @param {*} _ctor
* @param {(a:T,b:t)=>boolean} _equals
* @memberof LazyConstructor
*/
constructor(private _ctor: (...arggs) => T) {}
2020-09-10 16:21:01 +00:00
2021-12-04 07:25:26 +00:00
/**
* Creates a new Instance and tries to use an already existing
* one if possible.
*
* @param {T} args An Object containing the args which are required to create an Instance
* @returns {U} new Object
* @memberof LazyConstructor
*/
public createInstance(...args): T {
/** First Loop check whether there is a math in the Keys*/
2020-09-10 16:21:01 +00:00
2021-12-04 07:25:26 +00:00
const _instance = this._inactive[Symbol.iterator]().next();
2020-09-10 16:21:01 +00:00
2021-12-04 07:25:26 +00:00
if (!_instance.done) {
/** Remove the Instance */
this._inactive.delete(_instance.value);
/** Return the Value. */
return _instance.value;
2020-09-10 16:21:01 +00:00
}
2021-12-04 07:25:26 +00:00
return this._ctor(...args);
}
/**
* Releases a used Instance. Thereby it could be
* used again.
*
* @param {U} instance
* @memberof LazyConstructor
*/
public releaseInstance(instance: T) {
this._inactive.add(instance);
}
2020-09-10 16:21:01 +00:00
}