sub-events
Version:
Lightweight, strongly-typed events, with monitored subscriptions.
70 lines (69 loc) • 2.25 kB
TypeScript
import { ISubOptions, ISubStat, SubEvent, SubFunction } from './event';
import { Subscription } from './sub';
/**
* Encapsulates an event object, in order to hide its methods {@link SubEvent.emit} and {@link SubEvent.cancelAll},
* so the event consumer can only receive the event, but cannot emit it, or cancel other subscriptions.
*
* It is a non-extendable class, with the same signature as {@link SubEvent}, minus {@link SubEvent.emit emit} and {@link SubEvent.cancelAll cancelAll}.
*
* ```ts
* // Example of using EventConsumer inside a component.
*
* import {SubEvent, EventConsumer} from 'sub-events';
*
* class MyComponent {
*
* private event: SubEvent<string> = new SubEvent(); // internal, send-receive event
*
* readonly safeEvent: EventConsumer<string>; // public, receive-only event container
*
* constructor() {
* this.safeEvent = new EventConsumer(this.event);
*
* // or even simpler:
* // this.safeEvent = this.event.toConsumer();
*
* // clients can only receive data from such "safeEvent",
* // they cannot emit data or cancel other subscriptions.
* }
* }
* ```
*/
export declare class EventConsumer<T = unknown, E extends SubEvent<T> = SubEvent<T>> {
/**
* Class Constructor.
*
* @param event
* Event object to be encapsulated.
*/
constructor(event: E);
/**
* Forwards into {@link SubEvent.count} of the contained event.
*/
get count(): number;
/**
* Forwards into {@link SubEvent.maxSubs} of the contained event.
*/
get maxSubs(): number;
/**
* Forwards into {@link SubEvent.subscribe} of the contained event.
*/
subscribe(cb: SubFunction<T>, options?: ISubOptions): Subscription;
/**
* Forwards into {@link SubEvent.once} of the contained event.
*/
once(cb: SubFunction<T>, options?: ISubOptions): Subscription;
/**
* Forwards into {@link SubEvent.toPromise} of the contained event.
*/
toPromise(options?: {
name?: string;
timeout?: number;
}): Promise<T>;
/**
* Forwards into {@link SubEvent.getStat} of the contained event.
*/
getStat(options?: {
minUse?: number;
}): ISubStat;
}