entangle.ts
Version:
A declarative, event-driven framework for orchestrating business logic in TypeScript & Node.js applications.
78 lines (77 loc) • 3.61 kB
TypeScript
import { Aether } from '../aether';
import { ErrorHandler } from '../errors/ErrorHandler';
import { EventHorizon } from '../event-horizon/EventHorizon';
import { HiggsField } from '../higgs-field/HiggsField';
import { Event } from '../shared/types/Events.types';
import { Interaction } from '../shared/types/Interactions.types';
import { ParticleProperties } from '../shared/types/Particles.types';
import { MethodKeys } from '../shared/types/Utils.types';
import { GatewayBuilder } from './builders/Gateway.builder';
/**
* In quantum mechanics, Superposition is the principle that a system can exist in
* multiple potential states at once, collapsing into a single reality upon observation.
*
* In Entangle.ts, the `Superposition` class is the embodiment of this principle.
* It is the central orchestrator—the "laws of physics" for your application.
* It listens for events from the Aether, consults the EventHorizon for historical
* context, and executes rule-based interactions, creating and manipulating
* particles within the HiggsField and its temporary scopes.
*
* @class Superposition
* @param aether The communication medium, responsible for event transport.
* @param higgs The foundational field, the main container for service particles.
* @param horizon The historical record, providing context of all past events.
*/
export declare class Superposition {
private readonly aether;
private readonly higgs;
private readonly horizon;
private readonly errorHandler?;
readonly particlesContracts: Map<number, ParticleProperties<unknown, unknown[]>>;
readonly contracts: ParticleProperties<any, any>[];
readonly interactions: Interaction<any, any, any>[];
private readonly logger;
constructor(aether: Aether, higgs: HiggsField, horizon: EventHorizon, errorHandler?: ErrorHandler | undefined);
/**
* Begins the declarative definition of a rule that is triggered by a specific event.
* This is the entry point for defining any "law" in the application.
* @param event The name of the event that triggers the rule.
* @returns A `GatewayBuilder` instance to continue defining the rule by choosing an
* action (`.build()` or `.use()`).
*/
upon(event: Event): GatewayBuilder;
/**
* Check if there are any particles that can be created
*/
private checkForParticlesCreation;
/**
* Checks if a particle creation rule can be executed based on its `when` clause.
* @internal
*/
private canCreateAParticle;
/**
* Creates a particle instance and registers it within the specified scope
* (or the main HiggsField if no scope is provided), making it available for
* other interactions.
* @internal
*/
private createAParticle;
/**
* Registers a particle creation contract, setting up a listener for its trigger event.
* This method is intended to be called by a builder.
* @internal
*/
addContract<TParticle, TArgs extends unknown[]>(contract: ParticleProperties<TParticle, TArgs>): this;
/**
* Registers an interaction contract, setting up a listener if it's event-driven.
* This method is intended to be called by a builder.
* @internal
*/
addInteraction<TParticle extends object, TArgs extends unknown[], TMethodName extends MethodKeys<TParticle>>(interaction: Interaction<TParticle, TArgs, TMethodName>): this;
/**
* Checks for and executes all interaction rules triggered by a specific event.
* @internal
*/
private checkForParticlesInteractions;
private interact;
}