startWith.js 1.6 KB

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