startWith.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. var ArrayObservable_1 = require('../observable/ArrayObservable');
  3. var ScalarObservable_1 = require('../observable/ScalarObservable');
  4. var EmptyObservable_1 = require('../observable/EmptyObservable');
  5. var concat_1 = require('../observable/concat');
  6. var isScheduler_1 = require('../util/isScheduler');
  7. /* tslint:enable:max-line-length */
  8. /**
  9. * Returns an Observable that emits the items you specify as arguments before it begins to emit
  10. * items emitted by the source Observable.
  11. *
  12. * <img src="./img/startWith.png" width="100%">
  13. *
  14. * @param {...T} values - Items you want the modified Observable to emit first.
  15. * @param {Scheduler} [scheduler] - A {@link IScheduler} to use for scheduling
  16. * the emissions of the `next` notifications.
  17. * @return {Observable} An Observable that emits the items in the specified Iterable and then emits the items
  18. * emitted by the source Observable.
  19. * @method startWith
  20. * @owner Observable
  21. */
  22. function startWith() {
  23. var array = [];
  24. for (var _i = 0; _i < arguments.length; _i++) {
  25. array[_i - 0] = arguments[_i];
  26. }
  27. return function (source) {
  28. var scheduler = array[array.length - 1];
  29. if (isScheduler_1.isScheduler(scheduler)) {
  30. array.pop();
  31. }
  32. else {
  33. scheduler = null;
  34. }
  35. var len = array.length;
  36. if (len === 1) {
  37. return concat_1.concat(new ScalarObservable_1.ScalarObservable(array[0], scheduler), source);
  38. }
  39. else if (len > 1) {
  40. return concat_1.concat(new ArrayObservable_1.ArrayObservable(array, scheduler), source);
  41. }
  42. else {
  43. return concat_1.concat(new EmptyObservable_1.EmptyObservable(scheduler), source);
  44. }
  45. };
  46. }
  47. exports.startWith = startWith;
  48. //# sourceMappingURL=startWith.js.map