rxjs.js 2.1 KB
Newer Older
Johannes Edmeier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2014-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17
import 'rxjs/add/observable/defer';
Johannes Edmeier committed
18
import 'rxjs/add/observable/empty';
19
import 'rxjs/add/observable/from';
Johannes Edmeier committed
20 21
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/timer';
22
import 'rxjs/add/operator/concat';
Johannes Edmeier committed
23 24
import 'rxjs/add/operator/concatAll';
import 'rxjs/add/operator/concatMap';
25
import 'rxjs/add/operator/delay';
Johannes Edmeier committed
26
import 'rxjs/add/operator/do';
27
import 'rxjs/add/operator/filter';
28
import 'rxjs/add/operator/ignoreElements';
29 30 31 32 33 34
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/retryWhen';

import {Observable} from 'rxjs/Observable';
import {animationFrame} from 'rxjs/scheduler/animationFrame';

35 36 37 38 39 40 41 42
Observable.prototype.doOnSubscribe = function (onSubscribe) {
  let source = this;
  return Observable.defer(() => {
    onSubscribe();
    return source;
  });
};

43 44 45 46 47 48 49 50 51 52 53 54 55 56
Observable.prototype.doFirst = function (doFirst) {
  let source = this;
  let triggered = false;
  return Observable.defer(() => {
    triggered = false;
    return source;
  }).do(n => {
    if (!triggered) {
      triggered = true;
      doFirst(n);
    }
  });
};

57 58 59 60 61 62 63 64 65
Observable.prototype.listen = function (callbackFn) {
  let handle = null;
  return this.doOnSubscribe(() => handle = setTimeout(() => callbackFn('executing'), 150))
    .do({
      complete: () => {
        handle && clearTimeout(handle);
        callbackFn('completed');
      },
      error: (error) => {
66
        console.warn('Operation failed:', error);
67 68 69 70 71 72
        handle && clearTimeout(handle);
        callbackFn('failed');
      }
    });
};

73 74 75 76
export {
  Observable,
  animationFrame
};