all files / src/ async-event-dispatcher.js

97.78% Statements 44/45
100% Branches 17/17
100% Functions 18/18
100% Lines 36/36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124  15× 18× 15×                                           12×                 10×                                                                             21× 10× 21×                                        
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const generic_event_1 = require("./generic-event");
/**
 * The AsyncEventDispatcher is the central point of RxStack's event listener system.
 */
class AsyncEventDispatcher {
    constructor() {
        /**
         * Holds all events and its listeners
         *
         * @type {Map<any, any>}
         */
        this.stack = new Map();
    }
    /**
     * Adds an event listener that listens on the specified events.
     *
     * @param {string} eventName
     * @param {EventCallable} callable
     * @param {number} priority
     */
    addListener(eventName, callable, priority = 0) {
        this.getNamedStack(eventName).push({ name: eventName, callable: callable, priority: priority });
    }
    /**
     * Gets the listeners of a specific event sorted by descending priority.
     *
     * @param {string} eventName
     * @returns {EventCallable[]}
     */
    getListeners(eventName) {
        const listeners = this.getNamedStack(eventName);
        return listeners
            .sort((a, b) => a.priority - b.priority)
            .map((item) => item.callable);
    }
    /**
     * Checks whether an event has any registered listeners.
     *
     * @param {string} eventName
     * @returns {boolean}
     */
    hasListeners(eventName) {
        return this.stack.has(eventName);
    }
    /**
     *  Removes event listeners from the specified event.
     *
     * @param {string} eventName
     */
    removeListeners(eventName) {
        this.stack.delete(eventName);
    }
    /**
     * Removes all event listeners
     */
    reset() {
        this.stack.clear();
    }
    /**
     * Dispatches an event to all registered listeners.
     *
     * @param {string} eventName
     * @param {GenericEvent} event
     * @returns {Promise<GenericEvent>}
     */
    dispatch(eventName, event) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!event)
                event = new generic_event_1.GenericEvent();
            console.log('dddddd');
            const listeners = this.getListeners(eventName);
            if (listeners.length > 0)
                yield this.doDispatch(listeners, event);
            return event;
        });
    }
    /**
     * Gets a stack of a particular event
     *
     * @param {string} name
     * @returns {Observer[]}
     */
    getNamedStack(name) {
        if (!this.stack.has(name))
            this.stack.set(name, []);
        return this.stack.get(name);
    }
    /**
     * Triggers the listeners of an event.
     *
     * @param {EventCallable[]} listeners
     * @param {GenericEvent} event
     * @returns {Promise<GenericEvent>}
     */
    doDispatch(listeners, event) {
        return __awaiter(this, void 0, void 0, function* () {
            return listeners.reduce((currrent, next) => {
                return currrent.then(() => __awaiter(this, void 0, void 0, function* () {
                    if (event.isPropagationStopped())
                        return event;
                    return next.call(this, event);
                }));
            }, Promise.resolve(event));
        });
    }
}
exports.AsyncEventDispatcher = AsyncEventDispatcher;
/**
 * Exports single instance of AsyncEventDispatcher
 *
 * @type {AsyncEventDispatcher}
 */
exports.asyncEventDispatcher = new AsyncEventDispatcher();
//# sourceMappingURL=async-event-dispatcher.js.map