"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var StoreEntity_1 = require("./Core/StoreEntity");
var RealtimeSubscriptions = /** @class */ (function () {
    function RealtimeSubscriptions(session) {
        var _this = this;
        this.subscribe = function (eventType, callback) {
            var raiseOnEvent = function (entity) { return _this.onEvent(eventType, entity); };
            if (!_this.subscriptions.has(eventType)) {
                _this.realTime.on(eventType, raiseOnEvent);
                _this.subscriptions.set(eventType, new Set());
            }
            _this.subscriptions.get(eventType).add(callback);
            return function () {
                _this.subscriptions.get(eventType).delete(callback);
                if (_this.subscriptions.get(eventType).size === 0) {
                    _this.realTime.off(eventType, raiseOnEvent);
                    _this.subscriptions.delete(eventType);
                }
            };
        };
        this.onEvent = function (eventType, entity) {
            if (_this.subscriptions.get(eventType)) {
                return Array.from(_this.subscriptions.get(eventType)).map(function (fn) { return fn(entity); });
            }
        };
        this.realTime = new StoreEntity_1.RealTimeHandler(session.persistentConnection);
        this.subscriptions = new Map();
    }
    return RealtimeSubscriptions;
}());
exports.default = RealtimeSubscriptions;
//# sourceMappingURL=realtime-subscriptions.js.map