@ayanaware/bento
Version:
Modular runtime framework designed to solve complex tasks
189 lines (188 loc) • 6.47 kB
TypeScript
import type { Bento } from '../../Bento';
import { EventEmitterLike } from '../../interfaces/EventEmitterLike';
import { VariableDefinition } from '../../variables/interfaces/VariableDefinition';
import type { Component } from '../interfaces/Component';
import { Entity } from '../interfaces/Entity';
import type { Plugin } from '../interfaces/Plugin';
import { ComponentReference } from '../types/ComponentReference';
import { EntityReference } from '../types/EntityReference';
import { PluginReference } from '../types/PluginReference';
/**
* Shared functions for ComponentAPI and PluginAPI
*/
export declare class EntityAPI {
protected readonly bento: Bento;
/**
* Owner entity of this API instance
*/
protected entity: Entity;
/**
* Currently existing subscriptions of this component.
* The key is the namespace where a subscription was added,
* the value is an array of subscription ids on that namespace.
*/
private readonly subscriptions;
constructor(bento: Bento);
/**
* Get the semantic version string of the bento instance attached to this component api
* @returns Semantic version string (https://semver.org)
*/
getBentoVersion(): string;
/**
* Check if bento has a given property
* @param name name of property
*
* @returns boolean
*/
hasProperty(name: string): boolean;
/**
* Fetch the value of given application property
* @param name name of application property
*
* @returns Property value
*/
getProperty<T>(name: string): T;
/**
* Check if bento has a given variable
* @param name name of variable
*
* @returns boolean
*/
hasVariable(name: string): boolean;
/**
* Gets the value of a variable
* @param definition Variable name or definition
*
* @returns Variable value
*/
getVariable<T>(definition: VariableDefinition | string): T;
/**
* Check if Entity exists
* @param reference EntityReference
*
* @returns boolean
*/
hasEntity(reference: EntityReference): boolean;
/**
* Get Entity
* @param reference EntityReference
*
* @returns Entity
*/
getEntity<T extends Entity>(reference: EntityReference<T>): T;
/**
* Check if Plugin exists
* @param reference PluginReference
*
* @returns boolean
*/
hasPlugin(reference: PluginReference): boolean;
/**
* Get Plugin
* @param reference PluginReference
*
* @returns Plugin
*/
getPlugin<T extends Plugin>(reference: PluginReference<T>): T;
/**
* Check if Component exists
* @param reference ComponentReference
*
* @returns boolean
*/
hasComponent(reference: ComponentReference): boolean;
/**
* Get Component
* @param reference ComponentReference
*
* @returns Component
*/
getComponent<T extends Component>(reference: ComponentReference<T>): T;
/**
* Check Plugin Depend on Component
* @param reference EntityReference
*
* @throws APIError if this.entity is plugin and reference is component
*/
protected checkPdc(reference: EntityReference): boolean;
/**
* Inject an entity into invoking entity
* @param reference Entity name or reference
* @param injectName Property name to inject to
*/
injectEntity(reference: EntityReference, injectName: string | symbol): void;
/**
* Inject plugin into invoking entity
* @param reference Plugin name or reference
* @param injectName property name to inject into
*/
injectPlugin(reference: PluginReference, injectName: string | symbol): void;
/**
* Inject component dependency into invoking entity
* @param reference Component name or reference
* @param injectName property name to inject into
*/
injectComponent(reference: ComponentReference, injectName: string | symbol): void;
/**
* Defines and attaches a variable to component
* @param definition Variable definition
* @param injectName property name to inject into
*/
injectVariable(definition: VariableDefinition, injectName?: string | symbol): void;
/**
* Emit a event on Bento Events
* @param eventName Name of event
* @param args Ordered Array of args to emit
*/
emit(eventName: string, ...args: Array<any>): void;
/**
* Emit subject event on Bento Events
* @param eventName Name of event
* @param args Ordered Array of args to emit
*/
emitSubject(eventName: string, ...args: Array<any>): void;
/**
* Re-emits events from a standard event emitter into Bento events.
*
* @param fromEmitter Emitter to re-emit from
* @param events Events to watch for
*
* @throws IllegalArgumentError if fromEmitter is not an EventEmitter or events is not an array
*/
forwardEvents(fromEmitter: EventEmitterLike, events: Array<string>): void;
/**
* Subscribe to Bento events
* @param reference Entity Reference / Name
* @param event Name of the event
* @param handler The function to be called
* @param context Optional `this` context for prior handler function
*
* @returns Subscription ID
*/
subscribe(reference: EntityReference, event: string, handler: (...args: Array<any>) => void, context?: unknown): number;
/**
* Alias for subscribe with normal event
* @param reference Entity Reference / Name
* @param event Name of the event
* @param handler The function to be called
* @param context Optional `this` context for above handler function
*
* @deprecated
*
* @returns Subscription ID
*/
subscribeEvent(reference: EntityReference, event: string, handler: (...args: Array<any>) => void, context?: unknown): number;
/**
* Ubsubscribe from a Component Event
* @param reference - Component Reference / Name
* @param id - subscription id provided by subscribe
*/
unsubscribe(reference: EntityReference, id: number): void;
/**
* Unsubscribes from all events in a namespace or all events alltogether.
* This will automatically get called when your component gets unloaded
*
* @param reference - Optional. A namespace where all events should be unsubscribed
*/
unsubscribeAll(reference?: EntityReference): void;
}