"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");

var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");

_Object$defineProperty(exports, "__esModule", {
  value: true
});

exports.StreamPromise = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime-corejs2/regenerator"));

require("regenerator-runtime/runtime");

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

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

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

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/getPrototypeOf"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/inherits"));

var _wrapNativeSuper2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/wrapNativeSuper"));

var _stream = require("stream");

/**
 *
 */

/**
 *
 */
var StreamPromise =
/*#__PURE__*/
function (_Promise) {
  (0, _inherits2["default"])(StreamPromise, _Promise);

  function StreamPromise() {
    (0, _classCallCheck2["default"])(this, StreamPromise);
    return (0, _possibleConstructorReturn2["default"])(this, (0, _getPrototypeOf2["default"])(StreamPromise).apply(this, arguments));
  }

  (0, _createClass2["default"])(StreamPromise, [{
    key: "stream",
    value: function stream() {
      return new _stream.Duplex();
    } // dummy

  }], [{
    key: "create",
    value: function create(asyncFn) {
      var streamCallback = function streamCallback(_s) {};

      var streamReady = new _promise["default"](function (resolve) {
        streamCallback = resolve;
      });
      var promise = asyncFn(streamCallback);
      var streamPromise = new StreamPromise(function (resolve, reject) {
        promise.then(resolve, reject);
      });

      streamPromise.stream = function () {
        return new _stream.Transform({
          transform: function transform(data, encoding, cb) {
            (function _callee() {
              var stream;
              return _regenerator["default"].async(function _callee$(_context) {
                while (1) {
                  switch (_context.prev = _context.next) {
                    case 0:
                      _context.next = 2;
                      return _regenerator["default"].awrap(streamReady);

                    case 2:
                      stream = _context.sent;
                      stream.write(data, encoding);
                      cb();

                    case 5:
                    case "end":
                      return _context.stop();
                  }
                }
              });
            })();
          },
          flush: function flush(cb) {
            var _this = this;

            (function _callee2() {
              var stream;
              return _regenerator["default"].async(function _callee2$(_context2) {
                while (1) {
                  switch (_context2.prev = _context2.next) {
                    case 0:
                      _context2.next = 2;
                      return _regenerator["default"].awrap(streamReady);

                    case 2:
                      stream = _context2.sent;
                      stream.end();
                      stream.on('data', function (data) {
                        _this.push(data);
                      }).on('end', function () {
                        _this.push(null);

                        cb();
                      });

                    case 5:
                    case "end":
                      return _context2.stop();
                  }
                }
              });
            })();
          }
        });
      };

      return streamPromise;
    }
  }]);
  return StreamPromise;
}((0, _wrapNativeSuper2["default"])(_promise["default"]));

exports.StreamPromise = StreamPromise;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy91dGlsL3Byb21pc2UudHMiXSwibmFtZXMiOlsiU3RyZWFtUHJvbWlzZSIsIkR1cGxleCIsImFzeW5jRm4iLCJzdHJlYW1DYWxsYmFjayIsIl9zIiwic3RyZWFtUmVhZHkiLCJyZXNvbHZlIiwicHJvbWlzZSIsInN0cmVhbVByb21pc2UiLCJyZWplY3QiLCJ0aGVuIiwic3RyZWFtIiwiVHJhbnNmb3JtIiwidHJhbnNmb3JtIiwiZGF0YSIsImVuY29kaW5nIiwiY2IiLCJ3cml0ZSIsImZsdXNoIiwiZW5kIiwib24iLCJwdXNoIl0sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFHQTs7QUFIQTs7OztBQVlBOzs7SUFHYUEsYTs7Ozs7Ozs7Ozs7OzZCQUNGO0FBQ1AsYUFBTyxJQUFJQyxjQUFKLEVBQVA7QUFDRCxLLENBQUM7Ozs7MkJB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WFtUmVhZHkgPSBuZXcgUHJvbWlzZTxEdXBsZXg+KChyZXNvbHZlKSA9PiB7XG4gICAgICBzdHJlYW1DYWxsYmFjayA9IHJlc29sdmU7XG4gICAgfSk7XG4gICAgY29uc3QgcHJvbWlzZSA9IGFzeW5jRm4oc3RyZWFtQ2FsbGJhY2spO1xuICAgIGNvbnN0IHN0cmVhbVByb21pc2UgPSBuZXcgU3RyZWFtUHJvbWlzZTxUPigocmVzb2x2ZSwgcmVqZWN0KSA9PiB7XG4gICAgICBwcm9taXNlLnRoZW4ocmVzb2x2ZSwgcmVqZWN0KTtcbiAgICB9KTtcbiAgICBzdHJlYW1Qcm9taXNlLnN0cmVhbSA9ICgpID0+XG4gICAgICBuZXcgVHJhbnNmb3JtKHtcbiAgICAgICAgdHJhbnNmb3JtKGRhdGEsIGVuY29kaW5nLCBjYikge1xuICAgICAgICAgIChhc3luYyAoKSA9PiB7XG4gICAgICAgICAgICBjb25zdCBzdHJlYW0gPSBhd2FpdCBzdHJlYW1SZWFkeTtcbiAgICAgICAgICAgIHN0cmVhbS53cml0ZShkYXRhLCBlbmNvZGluZyk7XG4gICAgICAgICAgICBjYigpO1xuICAgICAgICAgIH0pKCk7XG4gICAgICAgIH0sXG4gICAgICAgIGZsdXNoKGNiKSB7XG4gICAgICAgICAgKGFzeW5jICgpID0+IHtcbiAgICAgICAgICAgIGNvbnN0IHN0cmVhbSA9IGF3YWl0IHN0cmVhbVJlYWR5O1xuICAgICAgICAgICAgc3RyZWFtLmVuZCgpO1xuICAgICAgICAgICAgc3RyZWFtXG4gICAgICAgICAgICAgIC5vbignZGF0YScsIChkYXRhKSA9PiB7XG4gICAgICAgICAgICAgICAgdGhpcy5wdXNoKGRhdGEpO1xuICAgICAgICAgICAgICB9KVxuICAgICAgICAgICAgICAub24oJ2VuZCcsICgpID0+IHtcbiAgICAgICAgICAgICAgICB0aGlzLnB1c2gobnVsbCk7XG4gICAgICAgICAgICAgICAgY2IoKTtcbiAgICAgICAgICAgICAgfSk7XG4gICAgICAgICAgfSkoKTtcbiAgICAgICAgfSxcbiAgICAgIH0pO1xuICAgIHJldHVybiBzdHJlYW1Qcm9taXNlO1xuICB9XG59XG4iXX0=