"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 FileChangeNotificationService = (function () {
    function FileChangeNotificationService(path) {
        var _this = this;
        this.path = path;
        this.rpc = new RPC;
        this.events = new Leaf.EventEmitter();
        this.connector = new ConnectionKeepProcedure(this.rpc);
        this.connector.start(path);
        this.rpc.remoteEvents.listenBy(this, "change", function () {
            _this.events.emit("change");
        });
    }
    return FileChangeNotificationService;
}());
exports.FileChangeNotificationService = FileChangeNotificationService;
var RPC = (function (_super) {
    __extends(RPC, _super);
    function RPC() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    return RPC;
}(Leaf.RPCComposer));
var ConnectionKeepProcedure = (function (_super) {
    __extends(ConnectionKeepProcedure, _super);
    function ConnectionKeepProcedure(rpc) {
        var _this = _super.call(this) || this;
        _this.rpc = rpc;
        _this.path = window.location.toString().replace("http", "ws");
        _this.waitInterval = 1000;
        _this.debug();
        return _this;
    }
    ConnectionKeepProcedure.prototype.start = function (path) {
        if (path) {
            this.path = path;
        }
        if (this.state === "void") {
            this.setState("connecting");
        }
    };
    ConnectionKeepProcedure.prototype.atConnecting = function (stale) {
        var _this = this;
        this.data.connection = new WebSocket(this.path);
        var openHandler = function () {
            _this.data.connection.removeEventListener("close", closeHandler);
            _this.setState("setup");
        };
        var closeHandler = function () {
            if (stale())
                return;
            _this.data.connection.removeEventListener("open", openHandler);
            _this.data.connection.removeEventListener("close", closeHandler);
            _this.setState("waitReconnect");
        };
        this.data.connection.addEventListener("open", openHandler);
        this.data.connection.addEventListener("close", closeHandler);
    };
    ConnectionKeepProcedure.prototype.atSetup = function (stale) {
        var conn = new Leaf.RPCBrowserWebSocketConnection(this.data.connection);
        this.rpc.setConnection(conn);
        this.setState("working");
    };
    ConnectionKeepProcedure.prototype.atWorking = function (stale) {
        var _this = this;
        var closeHandler = function () {
            if (stale())
                return;
            _this.data.connection.removeEventListener("close", closeHandler);
            _this.setState("close");
        };
        this.data.connection.addEventListener("close", closeHandler);
    };
    ConnectionKeepProcedure.prototype.atClose = function (stale) {
        this.setState("waitReconnect");
    };
    ConnectionKeepProcedure.prototype.atWaitReconnect = function (stale) {
        var _this = this;
        setTimeout(function () {
            if (stale())
                return;
            _this.setState("connecting");
        }, this.waitInterval);
    };
    return ConnectionKeepProcedure;
}(Leaf.States));
