connectable.js 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.connectable = void 0;
  4. var Subject_1 = require("../Subject");
  5. var Observable_1 = require("../Observable");
  6. var defer_1 = require("./defer");
  7. var DEFAULT_CONFIG = {
  8. connector: function () { return new Subject_1.Subject(); },
  9. resetOnDisconnect: true,
  10. };
  11. function connectable(source, config) {
  12. if (config === void 0) { config = DEFAULT_CONFIG; }
  13. var connection = null;
  14. var connector = config.connector, _a = config.resetOnDisconnect, resetOnDisconnect = _a === void 0 ? true : _a;
  15. var subject = connector();
  16. var result = new Observable_1.Observable(function (subscriber) {
  17. return subject.subscribe(subscriber);
  18. });
  19. result.connect = function () {
  20. if (!connection || connection.closed) {
  21. connection = defer_1.defer(function () { return source; }).subscribe(subject);
  22. if (resetOnDisconnect) {
  23. connection.add(function () { return (subject = connector()); });
  24. }
  25. }
  26. return connection;
  27. };
  28. return result;
  29. }
  30. exports.connectable = connectable;
  31. //# sourceMappingURL=connectable.js.map