Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Observable.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { canReportError } from './util/canReportError';
  2. import { toSubscriber } from './util/toSubscriber';
  3. import { observable as Symbol_observable } from '../internal/symbol/observable';
  4. import { pipeFromArray } from './util/pipe';
  5. import { config } from './config';
  6. export class Observable {
  7. constructor(subscribe) {
  8. this._isScalar = false;
  9. if (subscribe) {
  10. this._subscribe = subscribe;
  11. }
  12. }
  13. lift(operator) {
  14. const observable = new Observable();
  15. observable.source = this;
  16. observable.operator = operator;
  17. return observable;
  18. }
  19. subscribe(observerOrNext, error, complete) {
  20. const { operator } = this;
  21. const sink = toSubscriber(observerOrNext, error, complete);
  22. if (operator) {
  23. operator.call(sink, this.source);
  24. }
  25. else {
  26. sink.add(this.source || (config.useDeprecatedSynchronousErrorHandling && !sink.syncErrorThrowable) ?
  27. this._subscribe(sink) :
  28. this._trySubscribe(sink));
  29. }
  30. if (config.useDeprecatedSynchronousErrorHandling) {
  31. if (sink.syncErrorThrowable) {
  32. sink.syncErrorThrowable = false;
  33. if (sink.syncErrorThrown) {
  34. throw sink.syncErrorValue;
  35. }
  36. }
  37. }
  38. return sink;
  39. }
  40. _trySubscribe(sink) {
  41. try {
  42. return this._subscribe(sink);
  43. }
  44. catch (err) {
  45. if (config.useDeprecatedSynchronousErrorHandling) {
  46. sink.syncErrorThrown = true;
  47. sink.syncErrorValue = err;
  48. }
  49. if (canReportError(sink)) {
  50. sink.error(err);
  51. }
  52. else {
  53. console.warn(err);
  54. }
  55. }
  56. }
  57. forEach(next, promiseCtor) {
  58. promiseCtor = getPromiseCtor(promiseCtor);
  59. return new promiseCtor((resolve, reject) => {
  60. let subscription;
  61. subscription = this.subscribe((value) => {
  62. try {
  63. next(value);
  64. }
  65. catch (err) {
  66. reject(err);
  67. if (subscription) {
  68. subscription.unsubscribe();
  69. }
  70. }
  71. }, reject, resolve);
  72. });
  73. }
  74. _subscribe(subscriber) {
  75. const { source } = this;
  76. return source && source.subscribe(subscriber);
  77. }
  78. [Symbol_observable]() {
  79. return this;
  80. }
  81. pipe(...operations) {
  82. if (operations.length === 0) {
  83. return this;
  84. }
  85. return pipeFromArray(operations)(this);
  86. }
  87. toPromise(promiseCtor) {
  88. promiseCtor = getPromiseCtor(promiseCtor);
  89. return new promiseCtor((resolve, reject) => {
  90. let value;
  91. this.subscribe((x) => value = x, (err) => reject(err), () => resolve(value));
  92. });
  93. }
  94. }
  95. Observable.create = (subscribe) => {
  96. return new Observable(subscribe);
  97. };
  98. function getPromiseCtor(promiseCtor) {
  99. if (!promiseCtor) {
  100. promiseCtor = config.Promise || Promise;
  101. }
  102. if (!promiseCtor) {
  103. throw new Error('no Promise impl found');
  104. }
  105. return promiseCtor;
  106. }
  107. //# sourceMappingURL=Observable.js.map