UNPKG

onix-core

Version:
144 lines 3.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Event1 = exports.Event = void 0; const EventArgs_1 = require("./EventArgs"); const EventHandler_1 = require("./EventHandler"); class Event { /** * constructor */ constructor() { this._type = "Event"; this.OnHandlerAttached = () => { }; this.OnHandlerAttachedContext = null; this.OnHandlerDettached = () => { }; this.OnHandlerDettachedContext = null; this.handlers = []; } /** * Determines whether the specified handler is bound to the event. */ hasBinding(handler) { return this.handlers.indexOf(handler) > -1; } /** * Determines whether the Event has active bindings. */ hasBindings() { return this.handlers.length > 0; } /** * Associates the handler with the Event object. * @param handler Event handler. */ bind(handler) { if (!this.hasBinding(handler)) { this.handlers.push(handler); if (this.OnHandlerAttached !== null) { this.OnHandlerAttached.call(this.OnHandlerAttachedContext); } } } /** * Associates the event handler with the Event object to execute no more than the specified number of times. * After the specified number of times, the action is disconnected from the handler. * @param handler Event handler. * @param triggerCount Number of calls before the handler is disconnected. */ bindFor(handler, triggerCount) { var that = this; var triggers = 0; var wire = new EventHandler_1.EventHandler((args) => { if (++triggers >= triggerCount) { that.unbind(wire); } handler.invoke(args); }, handler.Context); this.handlers.push(wire); } /** * Disconnects the specified handler from the Event. * @param handler Event handler. */ unbind(handler) { for (var i = 0; i < this.handlers.length; i++) { if (this.handlers[i] === handler) { this.handlers.splice(i, 1); return; } } } /** * Executes all linked handlers. */ trigger(args) { var handlers; if (this.hasBindings()) { handlers = this.handlers.slice(0); for (var i = 0; i < handlers.length; i++) { handlers[i].invoke(args); } } } /** * Make arguments and execute * @param sender Event sender * @param data Event data */ fire(sender, data) { this.trigger(new EventArgs_1.EventArgs(sender)); } /** * Deletes an event and deletes the binding of all related handlers. */ dispose() { this.handlers = []; } } exports.Event = Event; class Event1 extends Event { constructor() { super(...arguments); this._type = "Event1"; /** * Event handlers */ this.Handlers = []; } /** * Associates the handler with the Event object. * @param handler Event handler. */ bind(handler) { super.bind(handler); } /** * Disconnects the specified handler from the Event. * @param handler Event handler. */ unbind(handler) { super.unbind(handler); } /** * Determines whether the specified handler is bound to the event. */ hasBinding(handler) { return super.hasBinding(handler); } /** * Executes all linked handlers. */ trigger(args) { super.trigger(args); } /** * Make arguments and execute * @param sender Event sender * @param data Event data */ fire(sender, data) { this.trigger(new EventArgs_1.EventArgs1(sender, data)); } } exports.Event1 = Event1; //# sourceMappingURL=Event.js.map