constructor()

in remote/communication-host.js [35:120]


    constructor(host, options) {
        super();

        if (!isChannelHostProxy(host)) {
            throw new Error("host must be a IChannelHostProxy object.");
        }

        /** 
         * @private
         * @type {Array.<IRoute>}
         */
        this.routes = [];

        /**
         * @private
         * @readonly
         * @type {Object.<string, Donuts.Remote.ICommunicator>}
         */
        this.communicators = Object.create(null);

        /** 
         * @private
         * @type {Donuts.Remote.ICommunicatorConstructorOptions} 
         */
        this.communicatorOptions = options;

        /** 
         * @private
         * @type {Donuts.Remote.IChannelHostProxy} 
         */
        this.host = host;

        /**
         * @public
         * @readonly
         * @type {Donuts.Remote.IConnectionInfo}
         */
        this.connectionInfo = Object.create(null);

        Object.assign(this.connectionInfo, this.host.connectionInfo);
        this.connectionInfo.communicatorOptions = this.communicatorOptions || this.connectionInfo.communicatorOptions;

        /**
         * @type {Donuts.Remote.ChannelHostProxyConnectionHandler}
         */
        this.onConnection = (hostProxy, channelProxy) => {
            const communicator = new Communicator(channelProxy, this.communicatorOptions);

            channelProxy.setHandler("close", () => {
                delete this.communicators[communicator.id];
            });

            for (const route of this.routes) {
                communicator.map(route.pattern, route.asyncHandler);
            }

            this.communicators[communicator.id] = communicator;
            this.emit("connection", this, communicator);
        }

        /**
        * @type {Donuts.Remote.ChannelHostProxyErrorHandler}
        */
        this.onError = (hostProxy, error) => {
            this.emit("error", this, error);
        }

        /**
         * @type {Donuts.Remote.ChannelHostProxyEventHandler}
         */
        this.onClose = (hostProxy) => {
            this.emit("close", this);
        };

        /** 
         * @type {Donuts.Remote.ChannelHostProxyEventHandler}
         */
        this.onListening = (hostProxy) => {
            this.emit("listening", this);
        }

        this.host.setHandler("close", this.onClose);
        this.host.setHandler("connection", this.onConnection);
        this.host.setHandler("error", this.onError);
        this.host.setHandler("listening", this.onListening);
    }