connectable.js 1012 B

123456789101112131415161718192021222324252627
  1. import { Subject } from '../Subject';
  2. import { Observable } from '../Observable';
  3. import { defer } from './defer';
  4. var DEFAULT_CONFIG = {
  5. connector: function () { return new Subject(); },
  6. resetOnDisconnect: true,
  7. };
  8. export function connectable(source, config) {
  9. if (config === void 0) { config = DEFAULT_CONFIG; }
  10. var connection = null;
  11. var connector = config.connector, _a = config.resetOnDisconnect, resetOnDisconnect = _a === void 0 ? true : _a;
  12. var subject = connector();
  13. var result = new Observable(function (subscriber) {
  14. return subject.subscribe(subscriber);
  15. });
  16. result.connect = function () {
  17. if (!connection || connection.closed) {
  18. connection = defer(function () { return source; }).subscribe(subject);
  19. if (resetOnDisconnect) {
  20. connection.add(function () { return (subject = connector()); });
  21. }
  22. }
  23. return connection;
  24. };
  25. return result;
  26. }
  27. //# sourceMappingURL=connectable.js.map