define("clientkernelEventbus", ["clientkernelLoaderApi", "clientkernelEventbusApi", "postal", "clientkernelModulesApi", "clientkernelLoggerApi"],
    function(clientkernelLoaderApi, clientkernelEventbusApi, postal, clientkernelModulesApi, clientkernelLoggerApi) {
        "use strict";

        function $iterator(o) {
            if (o instanceof Array) return function() {
                return {
                    cur: 0,
                    arr: o,
                    hasNext: function() {
                        return this.cur < this.arr.length;
                    },
                    next: function() {
                        return this.arr[this.cur++];
                    }
                };
            };
            return typeof(o.iterator) == 'function' ? $bind(o, o.iterator) : o.iterator;
        };;
        var $fid = 0;

        function $bind(o, m) {
            if (m == null) return null;
            if (m.__id__ == null) m.__id__ = $fid++;
            var f;
            if (o.hx__closures__ == null) o.hx__closures__ = {};
            else f = o.hx__closures__[m.__id__];
            if (f == null) {
                f = function() {
                    return f.method.apply(f.scope, arguments);
                };
                f.scope = o;
                f.method = m;
                o.hx__closures__[m.__id__] = f;
            }
            return f;
        };
        var $_;
        var $estr = function() {
            return _hcmn_.js_Boot.__string_rec(this, '');
        };
        if (typeof clientkernelEventbus == 'undefined') {
            var clientkernelEventbus = {};
        };
        clientkernelEventbus.Activator = function() {
            this.timeoutTime = 6000;
        };
        clientkernelEventbus.Activator = ck_implements(clientkernelEventbus.Activator, clientkernelModulesApi.IModuleActivator);
        clientkernelEventbus.Activator.__interfaces__ = [clientkernelModulesApi.IModuleActivator];
        clientkernelEventbus.Activator.__name__ = ["clientkernelEventbus", "Activator"];
        clientkernelEventbus.Activator.prototype.signalReadyTimer = null;
        clientkernelEventbus.Activator.prototype.timeoutTime = null;
        clientkernelEventbus.Activator.prototype.context = null;
        clientkernelEventbus.Activator.prototype.lf = null;
        clientkernelEventbus.Activator.prototype.psetup = null;
        clientkernelEventbus.Activator.prototype.start = function(context) {
            var _g = this;
            this.context = context;
            context.getServiceTracker([clientkernelLoggerApi.ILoggerFactory, clientkernelLoaderApi.IVariableRegistry]).then(function(context1) {
                _g.lf = context1.getAnyService(clientkernelLoggerApi.ILoggerFactory, null);
                _g.psetup = postal;
                if (window.location.search.indexOf("debug=0") == -1) {
                    postal.addWireTap(function(data, envelope, pubInProgress) {
                        try {
                            console.debug('topic:' + envelope.topic + ';channel:' + envelope.channel + ';data=' + JSON.stringify(envelope.data));
                        } catch (e) {
                            console.debug('topic:' + envelope.topic + ';channel:' + envelope.channel);
                        }
                    });
                }
                if (window.location.search.indexOf("pf=0") != -1) {
                    context1.registerService(new clientkernelEventbus.EventBus(_g.lf.getLogger("EventBus")), clientkernelEventbusApi.IEventBus, null);
                    return;
                }
                var varRegistry = context1.getAnyService(clientkernelLoaderApi.IVariableRegistry);
                if (varRegistry.get("postal.timeout") != null) _g.timeoutTime = _hcmn_.Std.parseInt(varRegistry.get("postal.timeout"));
                _g.psetup.instanceId("Game");
                _g.psetup.fedx.transports.xframe.configure({
                    allowedOrigins: varRegistry.get("postal.allowedOrigins"),
                    defaultOriginUrl: varRegistry.get("postal.defaultOrigin")
                });
                _g.waitForSignalReady();
                _g.psetup.fedx.addFilter([{
                    channel: "ConsoleService",
                    topic: "#",
                    direction: "both"
                }, {
                    channel: "ClientService",
                    topic: "#",
                    direction: "both"
                }, {
                    channel: "Kernel",
                    topic: "#",
                    direction: "both"
                }, {
                    channel: "Game",
                    topic: "#",
                    direction: "both"
                }, {
                    channel: "postal.request-response",
                    topic: "#",
                    direction: "both"
                }]);
            });
        };
        clientkernelEventbus.Activator.prototype.waitForSignalReady = function() {
            var _g = this;
            this.signalReadyTimer = new _hcmn_.haxe_Timer(this.timeoutTime);
            this.signalReadyTimer.run = $bind(this, this.signalReadyTimeout);
            this.psetup.fedx.signalReady(function(data) {
                _g.context.registerService(new clientkernelEventbus.EventBus(_g.lf.getLogger("EventBus")), clientkernelEventbusApi.IEventBus, null);
                _g.signalReadyTimer.stop();
            });
        };
        clientkernelEventbus.Activator.prototype.signalReadyTimeout = function() {
            this.signalReadyTimer.stop();
            throw new Error("psetup.fedx.signalReady timeout after " + this.timeoutTime + " msecs");
        };
        clientkernelEventbus.Activator.prototype.stop = function(context) {};
        clientkernelEventbus.Activator.main = function() {};
        clientkernelEventbus.Activator.prototype.__class__ = clientkernelEventbus.Activator;
        clientkernelEventbus.Event = function(envelope) {
            this.envelope = envelope;
        };
        clientkernelEventbus.Event = ck_implements(clientkernelEventbus.Event, clientkernelEventbusApi.IEvent);
        clientkernelEventbus.Event.__interfaces__ = [clientkernelEventbusApi.IEvent];
        clientkernelEventbus.Event.__name__ = ["clientkernelEventbus", "Event"];
        clientkernelEventbus.Event.prototype.envelope = null;
        clientkernelEventbus.Event.prototype.getChannel = function() {
            return this.envelope.channel;
        };
        clientkernelEventbus.Event.prototype.getPayload = function() {
            return this.envelope.data;
        };
        clientkernelEventbus.Event.prototype.getTopic = function() {
            return this.envelope.topic;
        };
        clientkernelEventbus.Event.prototype.__class__ = clientkernelEventbus.Event;
        clientkernelEventbus.EventBus = function(logger) {
            this.logger = logger;
            this.channels = new _hcmn_.haxe_ds_StringMap();
        };
        clientkernelEventbus.EventBus = ck_implements(clientkernelEventbus.EventBus, clientkernelModulesApi.IService);
        clientkernelEventbus.EventBus = ck_implements(clientkernelEventbus.EventBus, clientkernelEventbusApi.IEventBus);
        clientkernelEventbus.EventBus.__interfaces__ = [clientkernelModulesApi.IService, clientkernelEventbusApi.IEventBus];
        clientkernelEventbus.EventBus.__name__ = ["clientkernelEventbus", "EventBus"];
        clientkernelEventbus.EventBus.prototype.channels = null;
        clientkernelEventbus.EventBus.prototype.logger = null;
        clientkernelEventbus.EventBus.prototype.getChannel = function(channel) {
            if (!this.channels.exists(channel)) {
                var value = new clientkernelEventbus.EventBusChannel(channel, this.logger);
                this.channels.set(channel, value);
            }
            return this.channels.get(channel);
        };
        clientkernelEventbus.EventBus.prototype.getChannels = function() {
            var ret = [];
            var $it0 = this.channels.keys();
            while ($it0.hasNext()) {
                var i = $it0.next();
                ret.push(i);
            }
            return ret;
        };
        clientkernelEventbus.EventBus.prototype.__class__ = clientkernelEventbus.EventBus;
        clientkernelEventbus.EventBusChannel = function(name, logger) {
            this.postal_channel = postal.channel(name);
            this.logger = logger;
            this.name = name;
        };
        clientkernelEventbus.EventBusChannel = ck_implements(clientkernelEventbus.EventBusChannel, clientkernelEventbusApi.IEventBusChannel);
        clientkernelEventbus.EventBusChannel.__interfaces__ = [clientkernelEventbusApi.IEventBusChannel];
        clientkernelEventbus.EventBusChannel.__name__ = ["clientkernelEventbus", "EventBusChannel"];
        clientkernelEventbus.EventBusChannel.prototype.postal_channel = null;
        clientkernelEventbus.EventBusChannel.prototype.logger = null;
        clientkernelEventbus.EventBusChannel.prototype.name = null;
        clientkernelEventbus.EventBusChannel.prototype.publish = function(topic, payload) {
            return new clientkernelEventbus.Event(this.postal_channel.publish(topic, payload));
        };
        clientkernelEventbus.EventBusChannel.prototype.subscribe = function(topic, callback, payloadType) {
            return new clientkernelEventbus.Subscription(this.postal_channel, topic, callback, payloadType);
        };
        clientkernelEventbus.EventBusChannel.prototype.subscribeToRequest = function(topic, callback, payloadType) {
            return new clientkernelEventbus.RequestSubscription(this.postal_channel, topic, callback, payloadType);
        };
        clientkernelEventbus.EventBusChannel.prototype.request = function(topic, payload, timeout) {
            var jsPromise = this.postal_channel.request(new clientkernelEventbus.RequestOptions(topic, payload, timeout));
            var result;
            result = _hcmn_.js_Boot.__cast(jsPromise, Promise);
            return result;
        };
        clientkernelEventbus.EventBusChannel.prototype.__class__ = clientkernelEventbus.EventBusChannel;
        clientkernelEventbus.RequestEvent = function(envelope) {
            clientkernelEventbus.Event.call(this, envelope);
        };
        clientkernelEventbus.RequestEvent = ck_implements(clientkernelEventbus.RequestEvent, clientkernelEventbusApi.IEvent);
        clientkernelEventbus.RequestEvent = ck_implements(clientkernelEventbus.RequestEvent, clientkernelEventbusApi.IRequestEvent);
        clientkernelEventbus.RequestEvent.__interfaces__ = [clientkernelEventbusApi.IEvent, clientkernelEventbusApi.IRequestEvent];
        clientkernelEventbus.RequestEvent.__name__ = ["clientkernelEventbus", "RequestEvent"];
        clientkernelEventbus.RequestEvent.__super__ = clientkernelEventbus.Event;
        for (var k in clientkernelEventbus.Event.prototype) clientkernelEventbus.RequestEvent.prototype[k] = clientkernelEventbus.Event.prototype[k];
        clientkernelEventbus.RequestEvent.prototype.reply = function(data) {
            this.envelope.reply(null, data);
        };
        clientkernelEventbus.RequestEvent.prototype.reject = function(error) {
            this.envelope.reply(error, null);
        };
        clientkernelEventbus.RequestEvent.prototype.__class__ = clientkernelEventbus.RequestEvent;
        clientkernelEventbus.RequestOptions = function(topic, data, timeout) {
            this.topic = topic;
            this.data = data;
            this.timeout = timeout;
        };
        clientkernelEventbus.RequestOptions.__name__ = ["clientkernelEventbus", "RequestOptions"];
        clientkernelEventbus.RequestOptions.prototype.topic = null;
        clientkernelEventbus.RequestOptions.prototype.data = null;
        clientkernelEventbus.RequestOptions.prototype.timeout = null;
        clientkernelEventbus.RequestOptions.prototype.__class__ = clientkernelEventbus.RequestOptions;
        clientkernelEventbus.RequestSubscription = function(postal_channel, topic, callback, payloadType) {
            this.postal_channel = postal_channel;
            this.callback = callback;
            this.payloadType = payloadType;
            this.handle = postal_channel.subscribe(topic, $bind(this, this.checkcallback));
        };
        clientkernelEventbus.RequestSubscription = ck_implements(clientkernelEventbus.RequestSubscription, clientkernelEventbusApi.ISubscription);
        clientkernelEventbus.RequestSubscription.__interfaces__ = [clientkernelEventbusApi.ISubscription];
        clientkernelEventbus.RequestSubscription.__name__ = ["clientkernelEventbus", "RequestSubscription"];
        clientkernelEventbus.RequestSubscription.prototype.callback = null;
        clientkernelEventbus.RequestSubscription.prototype.handle = null;
        clientkernelEventbus.RequestSubscription.prototype.postal_channel = null;
        clientkernelEventbus.RequestSubscription.prototype.payloadType = null;
        clientkernelEventbus.RequestSubscription.prototype.checkcallback = function(data, envelope) {
            if (this.payloadType == null) this.callback(new clientkernelEventbus.RequestEvent(envelope));
            else if (data instanceof payloadType) this.callback(new clientkernelEventbus.RequestEvent(envelope));
            else null;
        };
        clientkernelEventbus.RequestSubscription.prototype.unsubscribe = function() {
            if (this.handle != null) {
                this.handle.unsubscribe();
                this.handle = null;
            }
        };
        clientkernelEventbus.RequestSubscription.prototype.__class__ = clientkernelEventbus.RequestSubscription;
        clientkernelEventbus.Subscription = function(postal_channel, topic, callback, payloadType) {
            this.postal_channel = postal_channel;
            this.callback = callback;
            this.payloadType = payloadType;
            this.handle = postal_channel.subscribe(topic, $bind(this, this.checkcallback));
        };
        clientkernelEventbus.Subscription = ck_implements(clientkernelEventbus.Subscription, clientkernelEventbusApi.ISubscription);
        clientkernelEventbus.Subscription.__interfaces__ = [clientkernelEventbusApi.ISubscription];
        clientkernelEventbus.Subscription.__name__ = ["clientkernelEventbus", "Subscription"];
        clientkernelEventbus.Subscription.prototype.callback = null;
        clientkernelEventbus.Subscription.prototype.handle = null;
        clientkernelEventbus.Subscription.prototype.postal_channel = null;
        clientkernelEventbus.Subscription.prototype.payloadType = null;
        clientkernelEventbus.Subscription.prototype.checkcallback = function(data, envelope) {
            if (this.payloadType == null) this.callback(new clientkernelEventbus.Event(envelope));
            else if (data instanceof payloadType) this.callback(new clientkernelEventbus.Event(envelope));
            else null;
        };
        clientkernelEventbus.Subscription.prototype.unsubscribe = function() {
            if (this.handle != null) {
                this.handle.unsubscribe();
                this.handle = null;
            }
        };
        clientkernelEventbus.Subscription.prototype.__class__ = clientkernelEventbus.Subscription;
        if (typeof haxe == 'undefined') {
            var haxe = {};
        };

        haxe.IMap = function() {}
        haxe.IMap.prototype = {

        };
        if (typeof js == 'undefined') {
            var js = {};
        };
        if (!js._Boot) {
            js._Boot = {};
        };
        js._Boot.HaxeError = function(val) {
            Error.call(this);
            this.val = val;
            this.message = String(val);
            if (Error.captureStackTrace) Error.captureStackTrace(this, js._Boot.HaxeError);
        };
        js._Boot.HaxeError.__name__ = ["js", "_Boot", "HaxeError"];
        js._Boot.HaxeError.__super__ = Error;
        for (var k in Error.prototype) js._Boot.HaxeError.prototype[k] = Error.prototype[k];
        js._Boot.HaxeError.prototype.val = null;
        js._Boot.HaxeError.prototype.__class__ = js._Boot.HaxeError;
        return {
            exports: clientkernelEventbus,
            activator: clientkernelEventbus.Activator
        };
    });
//# sourceURL=eventbus.js