ste-core
Version:
Core files for the Strongly Typed Events project.
88 lines (87 loc) • 2.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HandlingBase = void 0;
/**
* Base class that implements event handling. With a an
* event list this base class will expose events that can be
* subscribed to. This will give your class generic events.
*
* @export
* @abstract
* @class HandlingBase
* @template TEventHandler The type of event handler.
* @template TDispatcher The type of dispatcher.
* @template TList The type of event list.
*/
class HandlingBase {
/**
* Creates an instance of HandlingBase.
* @param {TList} events The event list. Used for event management.
*
* @memberOf HandlingBase
*/
constructor(events) {
this.events = events;
}
/**
* Subscribes once to the event with the specified name.
* @param {string} name The name of the event.
* @param {TEventHandler} fn The event handler.
*
* @memberOf HandlingBase
*/
one(name, fn) {
this.events.get(name).one(fn);
}
/**
* Checks it the event has a subscription for the specified handler.
* @param {string} name The name of the event.
* @param {TEventHandler} fn The event handler.
*
* @memberOf HandlingBase
*/
has(name, fn) {
return this.events.get(name).has(fn);
}
/**
* Subscribes to the event with the specified name.
* @param {string} name The name of the event.
* @param {TEventHandler} fn The event handler.
*
* @memberOf HandlingBase
*/
subscribe(name, fn) {
this.events.get(name).subscribe(fn);
}
/**
* Subscribes to the event with the specified name.
* @param {string} name The name of the event.
* @param {TEventHandler} fn The event handler.
*
* @memberOf HandlingBase
*/
sub(name, fn) {
this.subscribe(name, fn);
}
/**
* Unsubscribes from the event with the specified name.
* @param {string} name The name of the event.
* @param {TEventHandler} fn The event handler.
*
* @memberOf HandlingBase
*/
unsubscribe(name, fn) {
this.events.get(name).unsubscribe(fn);
}
/**
* Unsubscribes from the event with the specified name.
* @param {string} name The name of the event.
* @param {TEventHandler} fn The event handler.
*
* @memberOf HandlingBase
*/
unsub(name, fn) {
this.unsubscribe(name, fn);
}
}
exports.HandlingBase = HandlingBase;