Skip to content

Commit

Permalink
Added EitherObservable
Browse files Browse the repository at this point in the history
  • Loading branch information
ILikePizza555 committed Mar 16, 2019
1 parent 087a90d commit 3e4c3ad
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/EitherObservable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Observable, Operator, TeardownLogic, Subscriber } from "rxjs"

/**
* Represents a disjoint union of two Observables.
*
* An instance of `EitherObservable` is either an instance of `LeftObservable` or `RightObservable`.
* Conviention is that `LeftObservable` is used for errors and `RightObservable` is used for successes.
*
* `EitherObservable` is right-biased, `Right` is the default case to operate on.
*/
export type EitherObservable<E, A> = LeftObservable<E, A> | RightObservable<E, A>

/**
* Interface to ensure that LeftObservable and RightObservable have the same methods.
*/
interface IEitherObservable<E, A> extends Observable<A> {
readonly tag: "left" | "right";
}

export class LeftObservable<E, A> implements IEitherObservable<E, A> {
readonly tag: "left" = "left";

/** From RxJS docs: Internal implementation detail. */
public _isScalar = false;

/** Previous Observable in the chain. */
source: Observable<any>;
operator: Operator<any, A>;

private readonly _subscribe?: (this: Observable<A>, subscriber: Subscriber<A>) => TeardownLogic

constructor() {}
}

export class RightObservable<E, A> implements IEitherObservable<E, A> {
readonly tag: "right" = "right";

/** From RxJS docs: Internal implementation detail. */
public _isScalar = false;
}

0 comments on commit 3e4c3ad

Please sign in to comment.