entangle.ts
Version:
A declarative, event-driven framework for orchestrating business logic in TypeScript & Node.js applications.
47 lines (46 loc) • 2.18 kB
TypeScript
import { Particle, ParticleProperties } from '../shared/types/Particles.types';
/**
* The Higgs Field gives "mass" (substance) to particle classes,
* turning them into real, usable instances. It acts as a hierarchical
* dependency injection container that manages object lifecycles and scopes.
*/
export declare class HiggsField {
private readonly parent?;
private readonly particles;
private readonly factories;
constructor(parent?: HiggsField | undefined);
/**
* Registers a factory function that defines how to create a particle.
* @template T The type of the particle instance.
* @param particleClass The class constructor for the particle.
* @param factory The function that returns a new instance of the particle.
* @param options Lifecycle options for the particle (e.g., scope).
*/
register<TParticle, TArgs extends unknown[]>(particleClass: Particle<TParticle, TArgs> | string, factory: () => TParticle, options?: ParticleOptions): void;
/**
* Retrieves an instance of a particle, respecting its scope and the scope hierarchy.
* @template T The expected type of the particle instance.
* @param particleClass The class used to register the particle.
* @returns An instance of the particle.
* @throws {Error} If the particle is not registered in this scope or any parent scope.
*/
get<TParticle, TArgs extends unknown[]>(particleClass: Particle<TParticle, TArgs>): TParticle;
/**
* Destroy a particle
*/
destroy(particleClass: Particle): void;
/**
* Helper method used to get a particle object of options defined upon creation
*/
getParticleOptions(particleClass: Particle): ParticleOptions | undefined;
/**
* Creates a new child scope (a "bubble universe") of this Higgs Field.
* This new scope is isolated but can resolve dependencies from its parent.
* @returns A new, scoped `HiggsField` instance.
*/
createScope(): HiggsField;
}
/**
* Options defining a particle's lifecycle and behavior within the HiggsField.
*/
export type ParticleOptions = Pick<ParticleProperties, 'lifecycle' | 'destroyOnInteraction'>;