rxjs.ts 12.3 KB
Newer Older
patcharaporn's avatar
patcharaporn committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
/**
 * @license
 * Copyright Google Inc. All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */

import 'rxjs/add/observable/bindCallback';
import 'rxjs/add/observable/bindNodeCallback';
import 'rxjs/add/observable/defer';
import 'rxjs/add/observable/forkJoin';
import 'rxjs/add/observable/fromEventPattern';
import 'rxjs/add/operator/multicast';

import {Observable} from 'rxjs/Observable';
import {asap} from 'rxjs/scheduler/asap';
import {Subscriber} from 'rxjs/Subscriber';
import {Subscription} from 'rxjs/Subscription';
import {rxSubscriber} from 'rxjs/symbol/rxSubscriber';

(Zone as any).__load_patch('rxjs', (global: any, Zone: ZoneType, api: any) => {
  const symbol: (symbolString: string) => string = (Zone as any).__symbol__;
  const subscribeSource = 'rxjs.subscribe';
  const nextSource = 'rxjs.Subscriber.next';
  const errorSource = 'rxjs.Subscriber.error';
  const completeSource = 'rxjs.Subscriber.complete';
  const unsubscribeSource = 'rxjs.Subscriber.unsubscribe';
  const teardownSource = 'rxjs.Subscriber.teardownLogic';

  const empty = {
    closed: true,
    next(value: any): void{},
    error(err: any): void{throw err;},
    complete(): void{}
  };

  function toSubscriber<T>(
      nextOrObserver?: any, error?: (error: any) => void, complete?: () => void): Subscriber<T> {
    if (nextOrObserver) {
      if (nextOrObserver instanceof Subscriber) {
        return (<Subscriber<T>>nextOrObserver);
      }

      if (nextOrObserver[rxSubscriber]) {
        return nextOrObserver[rxSubscriber]();
      }
    }

    if (!nextOrObserver && !error && !complete) {
      return new Subscriber(empty);
    }

    return new Subscriber(nextOrObserver, error, complete);
  }

  const patchObservable = function() {
    const ObservablePrototype: any = Observable.prototype;
    const symbolSubscribe = symbol('subscribe');
    const _symbolSubscribe = symbol('_subscribe');
    const _subscribe = ObservablePrototype[_symbolSubscribe] = ObservablePrototype._subscribe;
    const subscribe = ObservablePrototype[symbolSubscribe] = ObservablePrototype.subscribe;

    Object.defineProperties(Observable.prototype, {
      _zone: {value: null, writable: true, configurable: true},
      _zoneSource: {value: null, writable: true, configurable: true},
      _zoneSubscribe: {value: null, writable: true, configurable: true},
      source: {
        configurable: true,
        get: function(this: Observable<any>) {
          return (this as any)._zoneSource;
        },
        set: function(this: Observable<any>, source: any) {
          (this as any)._zone = Zone.current;
          (this as any)._zoneSource = source;
        }
      },
      _subscribe: {
        configurable: true,
        get: function(this: Observable<any>) {
          if ((this as any)._zoneSubscribe) {
            return (this as any)._zoneSubscribe;
          } else if (this.constructor === Observable) {
            return _subscribe;
          }
          const proto = Object.getPrototypeOf(this);
          return proto && proto._subscribe;
        },
        set: function(this: Observable<any>, subscribe: any) {
          (this as any)._zone = Zone.current;
          (this as any)._zoneSubscribe = subscribe;
        }
      },
      subscribe: {
        writable: true,
        configurable: true,
        value: function(this: Observable<any>, observerOrNext: any, error: any, complete: any) {
          // Only grab a zone if we Zone exists and it is different from the current zone.
          const _zone = (this as any)._zone;
          if (_zone && _zone !== Zone.current) {
            // Current Zone is different from the intended zone.
            // Restore the zone before invoking the subscribe callback.
            return _zone.run(subscribe, this, [toSubscriber(observerOrNext, error, complete)]);
          }
          return subscribe.call(this, observerOrNext, error, complete);
        }
      }
    });
  };

  const patchSubscription = function() {
    const unsubscribeSymbol = symbol('unsubscribe');
    const unsubscribe = (Subscription.prototype as any)[unsubscribeSymbol] =
        Subscription.prototype.unsubscribe;
    Object.defineProperties(Subscription.prototype, {
      _zone: {value: null, writable: true, configurable: true},
      _zoneUnsubscribe: {value: null, writable: true, configurable: true},
      _unsubscribe: {
        get: function(this: Subscription) {
          if ((this as any)._zoneUnsubscribe) {
            return (this as any)._zoneUnsubscribe;
          }
          const proto = Object.getPrototypeOf(this);
          return proto && proto._unsubscribe;
        },
        set: function(this: Subscription, unsubscribe: any) {
          (this as any)._zone = Zone.current;
          (this as any)._zoneUnsubscribe = unsubscribe;
        }
      },
      unsubscribe: {
        writable: true,
        configurable: true,
        value: function(this: Subscription) {
          // Only grab a zone if we Zone exists and it is different from the current zone.
          const _zone: Zone = (this as any)._zone;
          if (_zone && _zone !== Zone.current) {
            // Current Zone is different from the intended zone.
            // Restore the zone before invoking the subscribe callback.
            _zone.run(unsubscribe, this);
          } else {
            unsubscribe.apply(this);
          }
        }
      }
    });
  };

  const patchSubscriber = function() {
    const next = Subscriber.prototype.next;
    const error = Subscriber.prototype.error;
    const complete = Subscriber.prototype.complete;

    Object.defineProperty(Subscriber.prototype, 'destination', {
      configurable: true,
      get: function(this: Subscriber<any>) {
        return (this as any)._zoneDestination;
      },
      set: function(this: Subscriber<any>, destination: any) {
        (this as any)._zone = Zone.current;
        (this as any)._zoneDestination = destination;
      }
    });

    // patch Subscriber.next to make sure it run
    // into SubscriptionZone
    Subscriber.prototype.next = function() {
      const currentZone = Zone.current;
      const subscriptionZone = this._zone;

      // for performance concern, check Zone.current
      // equal with this._zone(SubscriptionZone) or not
      if (subscriptionZone && subscriptionZone !== currentZone) {
        return subscriptionZone.run(next, this, arguments, nextSource);
      } else {
        return next.apply(this, arguments);
      }
    };

    Subscriber.prototype.error = function() {
      const currentZone = Zone.current;
      const subscriptionZone = this._zone;

      // for performance concern, check Zone.current
      // equal with this._zone(SubscriptionZone) or not
      if (subscriptionZone && subscriptionZone !== currentZone) {
        return subscriptionZone.run(error, this, arguments, errorSource);
      } else {
        return error.apply(this, arguments);
      }
    };

    Subscriber.prototype.complete = function() {
      const currentZone = Zone.current;
      const subscriptionZone = this._zone;

      // for performance concern, check Zone.current
      // equal with this._zone(SubscriptionZone) or not
      if (subscriptionZone && subscriptionZone !== currentZone) {
        return subscriptionZone.run(complete, this, arguments, completeSource);
      } else {
        return complete.apply(this, arguments);
      }
    };
  };

  const patchObservableInstance = function(observable: any) {
    observable._zone = Zone.current;
  };

  const patchObservableFactoryCreator = function(obj: any, factoryName: string) {
    const symbolFactory: string = symbol(factoryName);
    if (obj[symbolFactory]) {
      return;
    }
    const factoryCreator: any = obj[symbolFactory] = obj[factoryName];
    if (!factoryCreator) {
      return;
    }
    obj[factoryName] = function() {
      const factory: any = factoryCreator.apply(this, arguments);
      return function() {
        const observable = factory.apply(this, arguments);
        patchObservableInstance(observable);
        return observable;
      };
    };
  };

  const patchObservableFactory = function(obj: any, factoryName: string) {
    const symbolFactory: string = symbol(factoryName);
    if (obj[symbolFactory]) {
      return;
    }
    const factory: any = obj[symbolFactory] = obj[factoryName];
    if (!factory) {
      return;
    }
    obj[factoryName] = function() {
      const observable = factory.apply(this, arguments);
      patchObservableInstance(observable);
      return observable;
    };
  };

  const patchObservableFactoryArgs = function(obj: any, factoryName: string) {
    const symbolFactory: string = symbol(factoryName);
    if (obj[symbolFactory]) {
      return;
    }
    const factory: any = obj[symbolFactory] = obj[factoryName];
    if (!factory) {
      return;
    }
    obj[factoryName] = function() {
      const initZone = Zone.current;
      const args = Array.prototype.slice.call(arguments);
      for (let i = 0; i < args.length; i++) {
        const arg = args[i];
        if (typeof arg === 'function') {
          args[i] = function() {
            const argArgs = Array.prototype.slice.call(arguments);
            const runningZone = Zone.current;
            if (initZone && runningZone && initZone !== runningZone) {
              return initZone.run(arg, this, argArgs);
            } else {
              return arg.apply(this, argArgs);
            }
          };
        }
      }

      const observable = factory.apply(this, args);
      patchObservableInstance(observable);
      return observable;
    };
  };

  const patchMulticast = function() {
    const obj: any = Observable.prototype;
    const factoryName: string = 'multicast';
    const symbolFactory: string = symbol(factoryName);
    if (obj[symbolFactory]) {
      return;
    }
    const factory: any = obj[symbolFactory] = obj[factoryName];
    if (!factory) {
      return;
    }
    obj[factoryName] = function() {
      const _zone: any = Zone.current;
      const args = Array.prototype.slice.call(arguments);
      let subjectOrSubjectFactory: any = args.length > 0 ? args[0] : undefined;
      if (typeof subjectOrSubjectFactory !== 'function') {
        const originalFactory: any = subjectOrSubjectFactory;
        subjectOrSubjectFactory = function() {
          return originalFactory;
        };
      }
      args[0] = function() {
        let subject: any;
        if (_zone && _zone !== Zone.current) {
          subject = _zone.run(subjectOrSubjectFactory, this, arguments);
        } else {
          subject = subjectOrSubjectFactory.apply(this, arguments);
        }
        if (subject && _zone) {
          subject._zone = _zone;
        }
        return subject;
      };
      const observable = factory.apply(this, args);
      patchObservableInstance(observable);
      return observable;
    };
  };

  const patchImmediate = function(asap: any) {
    if (!asap) {
      return;
    }

    const scheduleSymbol = symbol('scheduleSymbol');
    const flushSymbol = symbol('flushSymbol');
    const zoneSymbol = symbol('zone');
    if (asap[scheduleSymbol]) {
      return;
    }

    const schedule = asap[scheduleSymbol] = asap.schedule;
    asap.schedule = function() {
      const args = Array.prototype.slice.call(arguments);
      const work = args.length > 0 ? args[0] : undefined;
      const delay = args.length > 1 ? args[1] : 0;
      const state = (args.length > 2 ? args[2] : undefined) || {};
      state[zoneSymbol] = Zone.current;

      const patchedWork = function() {
        const workArgs = Array.prototype.slice.call(arguments);
        const action = workArgs.length > 0 ? workArgs[0] : undefined;
        const scheduleZone = action && action[zoneSymbol];
        if (scheduleZone && scheduleZone !== Zone.current) {
          return scheduleZone.runGuarded(work, this, arguments);
        } else {
          return work.apply(this, arguments);
        }
      };
      return schedule.apply(this, [patchedWork, delay, state]);
    };
  };

  patchObservable();
  patchSubscription();
  patchSubscriber();
  patchObservableFactoryCreator(Observable, 'bindCallback');
  patchObservableFactoryCreator(Observable, 'bindNodeCallback');
  patchObservableFactory(Observable, 'defer');
  patchObservableFactory(Observable, 'forkJoin');
  patchObservableFactoryArgs(Observable, 'fromEventPattern');
  patchMulticast();
  patchImmediate(asap);
});