"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var Channel_1 = require("./Channel");
var Task_1 = require("@dojo/core/async/Task");
var WebSocketChannel = (function (_super) {
    __extends(WebSocketChannel, _super);
    function WebSocketChannel(options) {
        var _this = _super.call(this, options) || this;
        _this._socket = new WebSocket("ws://localhost:" + options.port);
        _this._ready = new Task_1.default(function (resolve) {
            _this._socket.addEventListener('open', resolve);
        });
        _this._socket.addEventListener('message', function (event) {
            _this._handleMessage(JSON.parse(event.data));
        });
        _this._sendQueue = {};
        return _this;
    }
    WebSocketChannel.prototype._sendData = function (name, data) {
        var _this = this;
        try {
            var id_1 = String(this._sequence++);
            var sessionId = this.sessionId;
            var message_1 = { id: id_1, sessionId: sessionId, name: name, data: data };
            return this._ready.then(function () {
                return new Task_1.default(function (resolve) {
                    _this._socket.send(JSON.stringify(message_1));
                    _this._sendQueue[id_1] = resolve;
                });
            });
        }
        catch (error) {
            return Task_1.default.reject(error);
        }
    };
    WebSocketChannel.prototype._handleMessage = function (message) {
        var id = message.id;
        this._sendQueue[id]();
        this._sendQueue[id] = null;
    };
    return WebSocketChannel;
}(Channel_1.default));
exports.default = WebSocketChannel;
//# sourceMappingURL=WebSocketChannel.js.map