ts-lib-extended
Version:
Additional types and tools for typescript
106 lines (105 loc) • 2.66 kB
TypeScript
import { Disposable } from './disposable/index.js';
/**
* @export
* @template T
* @since 4.0.0
*/
export type InstanceScopeVariants<T> = {
/**
* list of all variants in scope
*
* @type {T[]}
* @since 4.0.0
*/
variants: T[];
};
/**
* instance scope that can be split into different variants
*
* @export
* @template T
* @template {string} Variant instance variants (e.g. 'dark' | 'light')
* @since 4.0.0
*/
export type InstanceScope<T = any, Variant extends string = string> = {
readonly [key in Variant]: T;
} & InstanceScopeVariants<T>;
/**
* scopes cache
*
* @export
* @template {InstanceScope} S
* @since 4.0.0
*/
export type ScopesSource<S extends InstanceScope> = Map<PropertyKey, S>;
/**
* @export
* @template {InstanceScope} S
* @since 4.0.0
*/
export type ScopedInstance<S extends InstanceScope> = {
/**
* get an instance scope by id
*
* @param {PropertyKey} scopeId_
* @returns {S}
* @since 4.0.0
*/
scope(scopeId_: PropertyKey): S;
/**
* list of all scopes
*
* @type {S[]}
* @since 4.0.0
*/
scopes: S[];
};
/**
* extend from this core class to get quick scope support
*
* @export
* @abstract
* @class ScopedInstanceCore
* @template {InstanceScope} S
* @extends {Disposable}
* @implements {ScopedInstance<S>}
* @since 4.0.0
*/
export declare abstract class ScopedInstanceCore<S extends InstanceScope> extends Disposable implements ScopedInstance<S> {
private readonly _source;
constructor();
protected abstract disposeScope(scope_: S): void;
protected abstract createScope(id_: PropertyKey): S;
scope(id_: PropertyKey): S;
get scopes(): S[];
}
/**
* scope variants cache
*
* @export
* @template T
* @template {string} Variant instance variants (e.g. 'dark' | 'light')
* @since 4.0.0
*/
export type ScopeVariantsSource<T, Variant extends string> = Map<Variant, T>;
/**
* use this as scope base to access core features
*
* @export
* @abstract
* @class InstanceScopeCore
* @template T
* @template {string} Variant instance variants (e.g. 'dark' | 'light')
* @extends {Disposable}
* @implements {InstanceScopeVariants<T>}
* @since 4.0.0
*/
export declare abstract class InstanceScopeCore<T, Variant extends string> extends Disposable implements InstanceScopeVariants<T> {
readonly scopeId: PropertyKey;
private _source;
constructor(scopeId: PropertyKey);
get variants(): T[];
protected getOrCreateInstance(variant_: Variant): T;
protected abstract createInstance(variant_: Variant): T;
protected abstract disposeInstance(instance_: T): void;
}