"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _promise = require("babel-runtime/core-js/promise");

var _promise2 = _interopRequireDefault(_promise);

var _from = require("babel-runtime/core-js/array/from");

var _from2 = _interopRequireDefault(_from);

var _set = require("babel-runtime/core-js/set");

var _set2 = _interopRequireDefault(_set);

var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require("babel-runtime/helpers/createClass");

var _createClass3 = _interopRequireDefault(_createClass2);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * Copyright (c) 2002-2018 "Neo4j,"
 * Neo4j Sweden AB [http://neo4j.com]
 *
 * This file is part of Neo4j.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var HttpSessionTracker = function () {
  function HttpSessionTracker() {
    (0, _classCallCheck3.default)(this, HttpSessionTracker);

    this._openSessions = new _set2.default();
  }

  /**
   * Record given session as open.
   * @param {HttpSession} session the newly open session.
   */


  (0, _createClass3.default)(HttpSessionTracker, [{
    key: "sessionOpened",
    value: function sessionOpened(session) {
      this._openSessions.add(session);
    }

    /**
     * Record given session as close.
     * @param {HttpSession} session the just closed session.
     */

  }, {
    key: "sessionClosed",
    value: function sessionClosed(session) {
      this._openSessions.delete(session);
    }

    /**
     * Close this tracker and all open sessions.
     */

  }, {
    key: "close",
    value: function close() {
      var sessions = (0, _from2.default)(this._openSessions);
      this._openSessions.clear();
      return _promise2.default.all(sessions.map(function (session) {
        return closeSession(session);
      }));
    }
  }]);
  return HttpSessionTracker;
}();

/**
 * Close given session and get a promise back.
 * @param {HttpSession} session the session to close.
 * @return {Promise<void>} promise resolved when session is closed.
 */


exports.default = HttpSessionTracker;
function closeSession(session) {
  return new _promise2.default(function (resolve) {
    session.close(function () {
      resolve();
    });
  });
}