Skip to content

Instantly share code, notes, and snippets.

@jasonaden
Last active January 19, 2018 23:31
Show Gist options
  • Select an option

  • Save jasonaden/58b9481f48174f6e5f5b24c7e66cc077 to your computer and use it in GitHub Desktop.

Select an option

Save jasonaden/58b9481f48174f6e5f5b24c7e66cc077 to your computer and use it in GitHub Desktop.
Updates to RxJS Types

Observer:

  • Seems like it's okay. There's PartialObserver<T> and Observer<T> as the main exports.
  • Subscription is currently a class implementation. ISubscription is closest to the spec, and AnonymousSubscription is any object returning an unsubscribe() method.
  • ISubscription should be renamed to Subscription. This means the current Subscription class (which is used almost exclusively in type position) would be renamed to RxSubscription. See description below in Observable for how RxSubscription and Subscription would be implemented and used.

Subscriber:

  • Per the comments in this file, this class is rarely used outside RxJS. It extends Subscription (discussed above) and implements Observer. In the Observable spec, this type is called SubscriberFunction and the public API is close, but the Rx implementation adds the unsubscribe() method.
  • We should probably define the SubscriberFunction type and use it wherever possible. Should be discussed with Ben to find out if this type should have unsubscribe() by default.
  • Maybe we should split this out so it's not a subclass of Subscription. This would mean .subscribe would return a real subscription rather than just being cast to Subscription.
  • I don't think we need to make changes here. These types are used internally within operators and are not part of the public API.

Subscribable:

  • Part of Observable.ts defining the subscribe() method. Should be updated to return the new Subscription as currently it returns AnonymousSubscription.

Observable:

  • Move current Observable to RxObservable. Create new, minimalistic Observable interface. Make sure both are compatible.
    • Requires making RxObservable properties/methods optional. Probably should re-work how this is defined to be quicker and create fewer hidden classes. Will be something like this:
export interface Observable<T> {

    constructor(subscribe?: SubscriberFunction);

    // Subscribes to the sequence with an observer
    subscribe(observer: PartialObserver<T>) : Subscription;

    // Subscribes to the sequence with callbacks
    subscribe(onNext?: Function,
              onError?: Function,
              onComplete?: Function) : Subscription;

    // Returns itself
    [Symbol.observable]() : Observable;

    // Converts items to an Observable
    static of(...items) : Observable;

    // Converts an observable or iterable to an Observable
    static from(observable) : Observable;
}

export class RxObservable implements Observable {
  /** @internal */
  protected _isScalar?: boolean = false;
  /** @internal */
  protected source: Observable<any>;
  // ... other private/protected stuff
  
  
  constructor(subscribe?: (this: Observable<T>, subscriber: Subscriber<T>) => TeardownLogic) {
    // ... implementation
  }
  
  subscribe() {} 
  // ... all public methods implemented as usual.
}

The end result of the above should be that you can assign an RxObservable to an Observable and vice-versa. Currently the compiler will complain assigning Rx's Observable to a standard (spec version) of Observable.

We can then export the RxObservable as Observable so we don't break existing code. In the Rx source, we would use RxObservable in value position and Observable in type position.

Subject:

  • Subject is Rx-specific already, so doesn't need any major changes. It will need to be updated to extend and implement the right class/interface based on above changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment