sub-events
Version:
Lightweight, strongly-typed events, with monitored subscriptions.
97 lines (96 loc) • 2.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventConsumer = void 0;
var utils_1 = require("./utils");
/**
* Private-property implementation.
*
* @hidden
*/
var pp = new utils_1.Private();
/**
* 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.
* }
* }
* ```
*/
var EventConsumer = /** @class */ (function () {
/**
* Class Constructor.
*
* @param event
* Event object to be encapsulated.
*/
function EventConsumer(event) {
pp.set(this, event);
}
Object.defineProperty(EventConsumer.prototype, "count", {
/**
* Forwards into {@link SubEvent.count} of the contained event.
*/
get: function () {
return pp.get(this).count;
},
enumerable: false,
configurable: true
});
Object.defineProperty(EventConsumer.prototype, "maxSubs", {
/**
* Forwards into {@link SubEvent.maxSubs} of the contained event.
*/
get: function () {
return pp.get(this).maxSubs;
},
enumerable: false,
configurable: true
});
/**
* Forwards into {@link SubEvent.subscribe} of the contained event.
*/
EventConsumer.prototype.subscribe = function (cb, options) {
return pp.get(this).subscribe(cb, options);
};
/**
* Forwards into {@link SubEvent.once} of the contained event.
*/
EventConsumer.prototype.once = function (cb, options) {
return pp.get(this).once(cb, options);
};
/**
* Forwards into {@link SubEvent.toPromise} of the contained event.
*/
EventConsumer.prototype.toPromise = function (options) {
return pp.get(this).toPromise(options);
};
/**
* Forwards into {@link SubEvent.getStat} of the contained event.
*/
EventConsumer.prototype.getStat = function (options) {
return pp.get(this).getStat(options);
};
return EventConsumer;
}());
exports.EventConsumer = EventConsumer;