ste-core
Version:
Core files for the Strongly Typed Events project.
47 lines (46 loc) • 1.37 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.Subscription = void 0;
/**
* Stores a handler. Manages execution meta data.
* @class Subscription
* @template TEventHandler
*/
class Subscription {
/**
* Creates an instance of Subscription.
*
* @param {TEventHandler} handler The handler for the subscription.
* @param {boolean} isOnce Indicates if the handler should only be executed once.
*/
constructor(handler, isOnce) {
this.handler = handler;
this.isOnce = isOnce;
/**
* Indicates if the subscription has been executed before.
*/
this.isExecuted = false;
}
/**
* Executes the handler.
*
* @param {boolean} executeAsync True if the even should be executed async.
* @param {*} scope The scope the scope of the event.
* @param {IArguments} args The arguments for the event.
*/
execute(executeAsync, scope, args) {
if (!this.isOnce || !this.isExecuted) {
this.isExecuted = true;
var fn = this.handler;
if (executeAsync) {
setTimeout(() => {
fn.apply(scope, args);
}, 1);
}
else {
fn.apply(scope, args);
}
}
}
}
exports.Subscription = Subscription;
;